Java Code Examples for com.google.javascript.jscomp.parsing.Config.LanguageMode#ECMASCRIPT3

The following examples show how to use com.google.javascript.jscomp.parsing.Config.LanguageMode#ECMASCRIPT3 . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: 1_JsDocInfoParser.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses a string containing a JsDoc type declaration, returning the
 * type if the parsing succeeded or {@code null} if it failed.
 */
public static Node parseTypeString(String typeString) {
  Config config = new Config(
      Sets.<String>newHashSet(),
      Sets.<String>newHashSet(),
      false,
      LanguageMode.ECMASCRIPT3,
      false);
  JsDocInfoParser parser = new JsDocInfoParser(
      new JsDocTokenStream(typeString),
      null,
      "typeparsing",
      config,
      NullErrorReporter.forNewRhino());

  return parser.parseTopLevelTypeExpression(parser.next());
}
 
Example 2
Source File: Closure_133_JsDocInfoParser_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Parses a string containing a JsDoc type declaration, returning the
 * type if the parsing succeeded or {@code null} if it failed.
 */
public static Node parseTypeString(String typeString) {
  Config config = new Config(
      Sets.<String>newHashSet(),
      Sets.<String>newHashSet(),
      false,
      LanguageMode.ECMASCRIPT3,
      false);
  JsDocInfoParser parser = new JsDocInfoParser(
      new JsDocTokenStream(typeString),
      null,
      null,
      config,
      NullErrorReporter.forNewRhino());

  return parser.parseTopLevelTypeExpression(parser.next());
}
 
Example 3
Source File: Closure_109_JsDocInfoParser_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Parses a string containing a JsDoc type declaration, returning the
 * type if the parsing succeeded or {@code null} if it failed.
 */
public static Node parseTypeString(String typeString) {
  Config config = new Config(
      Sets.<String>newHashSet(),
      Sets.<String>newHashSet(),
      false,
      LanguageMode.ECMASCRIPT3,
      false);
  JsDocInfoParser parser = new JsDocInfoParser(
      new JsDocTokenStream(typeString),
      null,
      null,
      config,
      NullErrorReporter.forNewRhino());

  return parser.parseTopLevelTypeExpression(parser.next());
}
 
Example 4
Source File: Closure_109_JsDocInfoParser_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Parses a string containing a JsDoc type declaration, returning the
 * type if the parsing succeeded or {@code null} if it failed.
 */
public static Node parseTypeString(String typeString) {
  Config config = new Config(
      Sets.<String>newHashSet(),
      Sets.<String>newHashSet(),
      false,
      LanguageMode.ECMASCRIPT3,
      false);
  JsDocInfoParser parser = new JsDocInfoParser(
      new JsDocTokenStream(typeString),
      null,
      null,
      config,
      NullErrorReporter.forNewRhino());

  return parser.parseTopLevelTypeExpression(parser.next());
}
 
Example 5
Source File: Closure_32_JsDocInfoParser_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Parses a string containing a JsDoc type declaration, returning the
 * type if the parsing succeeded or {@code null} if it failed.
 */
public static Node parseTypeString(String typeString) {
  Config config = new Config(
      Sets.<String>newHashSet(),
      Sets.<String>newHashSet(),
      false,
      LanguageMode.ECMASCRIPT3,
      false);
  JsDocInfoParser parser = new JsDocInfoParser(
      new JsDocTokenStream(typeString),
      null,
      null,
      config,
      NullErrorReporter.forNewRhino());

  return parser.parseTopLevelTypeExpression(parser.next());
}
 
Example 6
Source File: Nopol2017_0025_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Parses a string containing a JsDoc type declaration, returning the
 * type if the parsing succeeded or {@code null} if it failed.
 */
public static Node parseTypeString(String typeString) {
  Config config = new Config(
      Sets.<String>newHashSet(),
      Sets.<String>newHashSet(),
      false,
      LanguageMode.ECMASCRIPT3,
      false);
  JsDocInfoParser parser = new JsDocInfoParser(
      new JsDocTokenStream(typeString),
      null,
      null,
      config,
      NullErrorReporter.forNewRhino());

  return parser.parseTopLevelTypeExpression(parser.next());
}
 
Example 7
Source File: Nopol2017_0025_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Parses a string containing a JsDoc type declaration, returning the
 * type if the parsing succeeded or {@code null} if it failed.
 */
public static Node parseTypeString(String typeString) {
  Config config = new Config(
      Sets.<String>newHashSet(),
      Sets.<String>newHashSet(),
      false,
      LanguageMode.ECMASCRIPT3,
      false);
  JsDocInfoParser parser = new JsDocInfoParser(
      new JsDocTokenStream(typeString),
      null,
      null,
      config,
      NullErrorReporter.forNewRhino());

  return parser.parseTopLevelTypeExpression(parser.next());
}
 
Example 8
Source File: Cardumen_0017_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Parses a string containing a JsDoc type declaration, returning the
 * type if the parsing succeeded or {@code null} if it failed.
 */
public static Node parseTypeString(String typeString) {
  Config config = new Config(
      Sets.<String>newHashSet(),
      Sets.<String>newHashSet(),
      false,
      LanguageMode.ECMASCRIPT3,
      false);
  JsDocInfoParser parser = new JsDocInfoParser(
      new JsDocTokenStream(typeString),
      null,
      null,
      config,
      NullErrorReporter.forNewRhino());

  return parser.parseTopLevelTypeExpression(parser.next());
}
 
Example 9
Source File: 1_JsDocInfoParser.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses a string containing a JsDoc type declaration, returning the
 * type if the parsing succeeded or {@code null} if it failed.
 */
public static Node parseTypeString(String typeString) {
  Config config = new Config(
      Sets.<String>newHashSet(),
      Sets.<String>newHashSet(),
      false,
      LanguageMode.ECMASCRIPT3,
      false);
  JsDocInfoParser parser = new JsDocInfoParser(
      new JsDocTokenStream(typeString),
      null,
      "typeparsing",
      config,
      NullErrorReporter.forNewRhino());

  return parser.parseTopLevelTypeExpression(parser.next());
}
 
Example 10
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void parseFull(String code, String... warnings) {
  CompilerEnvirons environment = new CompilerEnvirons();

  TestErrorReporter testErrorReporter = new TestErrorReporter(null, warnings);
  environment.setErrorReporter(testErrorReporter);

  environment.setRecordingComments(true);
  environment.setRecordingLocalJsDocComments(true);

  Parser p = new Parser(environment, testErrorReporter);
  AstRoot script = p.parse(code, null, 0);

  Config config =
      new Config(extraAnnotations, extraSuppressions,
          true, LanguageMode.ECMASCRIPT3, false);
  StaticSourceFile file = new SimpleSourceFile(script.getSourceName(), false);
  for (Comment comment : script.getComments()) {
    JsDocInfoParser jsdocParser =
      new JsDocInfoParser(
          new JsDocTokenStream(comment.getValue().substring(3),
              comment.getLineno()),
          comment,
          null,
          config,
          testErrorReporter);
    jsdocParser.parse();
    jsdocParser.retrieveAndResetParsedJSDocInfo();
  }

  assertTrue("some expected warnings were not reported",
      testErrorReporter.hasEncounteredAllWarnings());
}
 
Example 11
Source File: JsDocInfoParserTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private JSDocInfo parse(String comment, boolean parseDocumentation,
    boolean parseFileOverview, String... warnings) {
  TestErrorReporter errorReporter = new TestErrorReporter(null, warnings);

  Config config = new Config(extraAnnotations, extraSuppressions,
      parseDocumentation, LanguageMode.ECMASCRIPT3, false);
  StaticSourceFile file = new SimpleSourceFile("testcode", false);
  Node associatedNode = new Node(Token.SCRIPT);
  associatedNode.setInputId(new InputId(file.getName()));
  associatedNode.setStaticSourceFile(file);
  JsDocInfoParser jsdocParser = new JsDocInfoParser(
      stream(comment),
      new Comment(0, 0, CommentType.JSDOC, comment),
      associatedNode,
      config, errorReporter);

  if (fileLevelJsDocBuilder != null) {
    jsdocParser.setFileLevelJsDocBuilder(fileLevelJsDocBuilder);
  }

  jsdocParser.parse();

  assertTrue("expected warnings were not reported",
      errorReporter.hasEncounteredAllWarnings());

  if (parseFileOverview) {
    return jsdocParser.getFileOverviewJSDocInfo();
  } else {
    return jsdocParser.retrieveAndResetParsedJSDocInfo();
  }
}
 
Example 12
Source File: Closure_42_IRFactory_s.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processObjectLiteral(ObjectLiteral literalNode) {
  if (literalNode.isDestructuring()) {
    reportDestructuringAssign(literalNode);
  }

  Node node = newNode(Token.OBJECTLIT);
  for (ObjectProperty el : literalNode.getElements()) {
    if (config.languageMode == LanguageMode.ECMASCRIPT3) {
      if (el.isGetter()) {
        reportGetter(el);
        continue;
      } else if (el.isSetter()) {
        reportSetter(el);
        continue;
      }
    }

    Node key = transformAsString(el.getLeft());
    Node value = transform(el.getRight());
    if (el.isGetter()) {
      key.setType(Token.GETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (getFnParamNode(value).hasChildren()) {
        reportGetterParam(el.getLeft());
      }
    } else if (el.isSetter()) {
      key.setType(Token.SETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (!getFnParamNode(value).hasOneChild()) {
        reportSetterParam(el.getLeft());
      }
    }
    key.addChildToFront(value);
    node.addChildToBack(key);
  }
  return node;
}
 
Example 13
Source File: Closure_42_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processObjectLiteral(ObjectLiteral literalNode) {
  if (literalNode.isDestructuring()) {
    reportDestructuringAssign(literalNode);
  }

  Node node = newNode(Token.OBJECTLIT);
  for (ObjectProperty el : literalNode.getElements()) {
    if (config.languageMode == LanguageMode.ECMASCRIPT3) {
      if (el.isGetter()) {
        reportGetter(el);
        continue;
      } else if (el.isSetter()) {
        reportSetter(el);
        continue;
      }
    }

    Node key = transformAsString(el.getLeft());
    Node value = transform(el.getRight());
    if (el.isGetter()) {
      key.setType(Token.GETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (getFnParamNode(value).hasChildren()) {
        reportGetterParam(el.getLeft());
      }
    } else if (el.isSetter()) {
      key.setType(Token.SETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (!getFnParamNode(value).hasOneChild()) {
        reportSetterParam(el.getLeft());
      }
    }
    key.addChildToFront(value);
    node.addChildToBack(key);
  }
  return node;
}
 
Example 14
Source File: ParserTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testSetter() {
  mode = LanguageMode.ECMASCRIPT3;
  parseError("var x = {set 1(x){}};",
      IRFactory.SETTER_ERROR_MESSAGE);
  parseError("var x = {set 'a'(x){}};",
      IRFactory.SETTER_ERROR_MESSAGE);
  parseError("var x = {set a(x){}};",
      IRFactory.SETTER_ERROR_MESSAGE);
  mode = LanguageMode.ECMASCRIPT5;
  parse("var x = {set 1(x){}};");
  parse("var x = {set 'a'(x){}};");
  parse("var x = {set a(x){}};");
  parseError("var x = {set a(){}};",
      "setters must have exactly one parameter");
}
 
Example 15
Source File: Closure_37_IRFactory_t.java    From coming with MIT License 5 votes vote down vote up
@Override
Node processObjectLiteral(ObjectLiteral literalNode) {
  if (literalNode.isDestructuring()) {
    reportDestructuringAssign(literalNode);
  }

  Node node = newNode(Token.OBJECTLIT);
  for (ObjectProperty el : literalNode.getElements()) {
    if (config.languageMode == LanguageMode.ECMASCRIPT3) {
      if (el.isGetter()) {
        reportGetter(el);
        continue;
      } else if (el.isSetter()) {
        reportSetter(el);
        continue;
      }
    }

    Node key = transformAsString(el.getLeft());
    Node value = transform(el.getRight());
    if (el.isGetter()) {
      key.setType(Token.GETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (getFnParamNode(value).hasChildren()) {
        reportGetterParam(el.getLeft());
      }
    } else if (el.isSetter()) {
      key.setType(Token.SETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (!getFnParamNode(value).hasOneChild()) {
        reportSetterParam(el.getLeft());
      }
    }
    key.addChildToFront(value);
    node.addChildToBack(key);
  }
  return node;
}
 
Example 16
Source File: IRFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
Node processObjectLiteral(ObjectLiteral literalNode) {
  if (literalNode.isDestructuring()) {
    reportDestructuringAssign(literalNode);
  }

  Node node = newNode(Token.OBJECTLIT);
  for (ObjectProperty el : literalNode.getElements()) {
    if (config.languageMode == LanguageMode.ECMASCRIPT3) {
      if (el.isGetter()) {
        reportGetter(el);
        continue;
      } else if (el.isSetter()) {
        reportSetter(el);
        continue;
      }
    }

    Node key = transformAsString(el.getLeft());
    key.setType(Token.STRING_KEY);

    Node value = transform(el.getRight());
    if (el.isGetter()) {
      key.setType(Token.GETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (getFnParamNode(value).hasChildren()) {
        reportGetterParam(el.getLeft());
      }
    } else if (el.isSetter()) {
      key.setType(Token.SETTER_DEF);
      Preconditions.checkState(value.isFunction());
      if (!getFnParamNode(value).hasOneChild()) {
        reportSetterParam(el.getLeft());
      }
    }
    key.addChildToFront(value);
    node.addChildToBack(key);
  }
  return node;
}
 
Example 17
Source File: ParserRunner.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parses the JavaScript text given by a reader.
 *
 * @param sourceString Source code from the file.
 * @param errorReporter An error.
 * @param logger A logger.
 * @return The AST of the given text.
 * @throws IOException
 */
public static ParseResult parse(StaticSourceFile sourceFile,
                                String sourceString,
                                Config config,
                                ErrorReporter errorReporter,
                                Logger logger) throws IOException {
  Context cx = Context.enter();
  cx.setErrorReporter(errorReporter);
  cx.setLanguageVersion(Context.VERSION_1_5);
  CompilerEnvirons compilerEnv = new CompilerEnvirons();
  compilerEnv.initFromContext(cx);
  compilerEnv.setRecordingComments(true);
  compilerEnv.setRecordingLocalJsDocComments(true);

  // ES5 specifically allows trailing commas
  compilerEnv.setWarnTrailingComma(
      config.languageMode == LanguageMode.ECMASCRIPT3);

  // Do our own identifier check for ECMASCRIPT 5
  boolean acceptEs5 =
      config.isIdeMode || config.languageMode != LanguageMode.ECMASCRIPT3;
  compilerEnv.setReservedKeywordAsIdentifier(acceptEs5);

  compilerEnv.setAllowMemberExprAsFunctionName(false);
  compilerEnv.setIdeMode(config.isIdeMode);
  compilerEnv.setRecoverFromErrors(config.isIdeMode);

  Parser p = new Parser(compilerEnv, errorReporter);
  AstRoot astRoot = null;
  try {
    astRoot = p.parse(sourceString, sourceFile.getName(), 1);
  } catch (EvaluatorException e) {
    logger.info(
        "Error parsing " + sourceFile.getName() + ": " + e.getMessage());
  } finally {
    Context.exit();
  }
  Node root = null;
  if (astRoot != null) {
    root = IRFactory.transformTree(
        astRoot, sourceFile, sourceString, config, errorReporter);
    root.setIsSyntheticBlock(true);
  }
  return new ParseResult(root, astRoot);
}
 
Example 18
Source File: Closure_122_IRFactory_s.java    From coming with MIT License 4 votes vote down vote up
private boolean isReservedKeyword(String identifier) {
  if (config.languageMode == LanguageMode.ECMASCRIPT3) {
    return TokenStream.isKeyword(identifier);
  }
  return reservedKeywords != null && reservedKeywords.contains(identifier);
}
 
Example 19
Source File: IRFactoryTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void setUp() throws Exception {
  super.setUp();
  mode = LanguageMode.ECMASCRIPT3;
}
 
Example 20
Source File: ParserTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void setUp() throws Exception {
  super.setUp();
  mode = LanguageMode.ECMASCRIPT3;
  isIdeMode = false;
}