Esempio n. 1
0
  @Nullable
  private static String lintIt(Context context, String fileName, ScriptableObject scope)
      throws IOException {
    if (Boolean.valueOf(System.getProperty("test.lint.skip"))) {
      return null;
    }

    Object[] args = {FileUtil.loadFile(new File(fileName), true), JSHINT_OPTIONS};
    Function function = (Function) ScriptableObject.getProperty(scope.getParentScope(), "JSHINT");
    Object status = function.call(context, scope.getParentScope(), scope.getParentScope(), args);
    if (!(Boolean) Context.jsToJava(status, Boolean.class)) {
      Object errors = function.get("errors", scope);
      StringBuilder sb = new StringBuilder(fileName);
      for (Object errorObj : ((NativeArray) errors)) {
        if (errorObj == null) {
          continue;
        }

        NativeObject e = (NativeObject) errorObj;
        int line = toInt(e.get("line"));
        int character = toInt(e.get("character"));
        if (line < 0 || character < 0) {
          continue;
        }
        Object reasonObj = e.get("reason");
        if (reasonObj instanceof String) {
          String reason = (String) reasonObj;
          if (IGNORED_JSHINT_WARNINGS.contains(reason)
              || reason.startsWith("Expected exactly one space between ')' and ")
              || reason.startsWith("Expected '}' to match '{' from line ")
              || reason.startsWith("Expected '{' and instead saw ")) {
            continue;
          }

          sb.append('\n').append(line).append(':').append(character).append(' ').append(reason);
        }
      }

      return sb.length() == fileName.length() ? null : sb.toString();
    }

    return null;
  }
Esempio n. 2
0
  @Override
  public void init() {
    try {
      this.debug = Boolean.parseBoolean(this.getInitParameter("debug"));

      if (!this.debug) this.pageInfoCache = new HashMap<String, PageInfo>();

      String routesParameter = this.getInitParameter("routes");
      if (routesParameter != null) {
        this.routes = new Properties();
        routes.load(new StringReader(routesParameter));
      }

      String sourceParameter = this.getInitParameter("source");
      if (sourceParameter == null) throw new RuntimeException("Missing init parameter 'source'");
      // automatically insert a reference to our PageServlet.js if not present
      String sourceDelimiter = ";";
      if (sourceParameter.indexOf("PageServlet.js") < 0)
        sourceParameter =
            "jar:file:WEB-INF/lib/glasspages.jar!/glasspages/PageServlet.js"
                + sourceDelimiter
                + sourceParameter;
      this.errorPath = this.getInitParameter("error");
      String[] sourceFiles = sourceParameter.split(sourceDelimiter);
      URL[] sourceUrls = new URL[sourceFiles.length];
      for (int i = 0; i < sourceFiles.length; i++) {
        String sourceFile = sourceFiles[i];
        if (sourceFile.indexOf(':') < 0) sourceFile = "file:" + sourceFile;
        try {
          sourceUrls[i] = new URL(sourceFile);
        } catch (MalformedURLException e) {
          throw new RuntimeException(e);
        }
      }
      this.contextCache = new ScriptContextCache(sourceUrls);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }