/** Returns a feature set combining all the features from {@code this} and {@code feature}. */ public FeatureSet require(Feature feature) { return require(feature.features); }
void maybeReportTrailingComma(Token commaToken) { if (commaToken != null) { features = features.require(Feature.TRAILING_COMMA); if (config.warnTrailingCommas) { // In ES3 mode warn about trailing commas which aren't accepted by // older browsers (such as IE8). errorReporter.reportWarning(commaToken.location.start, "Trailing comma is not legal in an ECMA-262 object initializer"); } } }
/** Reports if declaration requires an initializer, assuming initializer is absent. */ private void maybeReportNoInitializer(TokenType token, ParseTree lvalue) { if (token == TokenType.CONST) { features = features.require(Feature.CONST_DECLARATIONS); reportError("const variables must have an initializer"); } else if (lvalue.isPattern()) { features = features.require(Feature.DESTRUCTURING); reportError("destructuring must have an initializer"); } }
/** Reports an illegal getter and returns true if the language mode is too low. */ boolean maybeReportGetter(ParseTree node) { features = features.require(Feature.GETTER); if (config.languageMode == LanguageMode.ECMASCRIPT3) { errorReporter.error( GETTER_ERROR_MESSAGE, sourceName, lineno(node), 0); return true; } return false; }
/** Reports an illegal setter and returns true if the language mode is too low. */ boolean maybeReportSetter(ParseTree node) { features = features.require(Feature.SETTER); if (config.languageMode == LanguageMode.ECMASCRIPT3) { errorReporter.error( SETTER_ERROR_MESSAGE, sourceName, lineno(node), 0); return true; } return false; }
private ParseTree parsePattern(PatternKind kind) { features = features.require(Feature.DESTRUCTURING); switch (peekType()) { case OPEN_SQUARE: return parseArrayPattern(kind); case OPEN_CURLY: default: return parseObjectPattern(kind); } }
private TokenType maybeParseAccessibilityModifier() { if (peekAccessibilityModifier()) { features = features.require(FeatureSet.TYPESCRIPT); if (!config.is6Typed) { reportError("Accessibility modifier is only supported in ES6 typed mode"); } return nextToken().type; } else { return null; } }
void maybeWarnEs6Feature(ParseTree node, Feature feature) { features = features.require(feature); if (!isEs6Mode()) { errorReporter.warning( "this language feature is only supported in es6 mode: " + feature, sourceName, lineno(node), charno(node)); } }
void maybeWarnTypeSyntax(ParseTree node, Feature feature) { if (config.languageMode != LanguageMode.ECMASCRIPT6_TYPED) { errorReporter.warning( "type syntax is only supported in ES6 typed mode: " + feature, sourceName, lineno(node), charno(node)); } features = features.require(feature); recordTypeSyntax(node.location); }
private void maybeWarnReservedKeyword(IdentifierToken token) { String identifier = token.toString(); boolean isIdentifier = false; if (TokenStream.isKeyword(identifier)) { features = features.require(Feature.ES3_KEYWORDS_AS_IDENTIFIERS); isIdentifier = config.languageMode == LanguageMode.ECMASCRIPT3; } if (reservedKeywords != null && reservedKeywords.contains(identifier)) { isIdentifier = true; } if (isIdentifier) { errorReporter.error( "identifier is a reserved word", sourceName, lineno(token.location.start), charno(token.location.start)); } }
break; case '\n': features = features.require(Feature.STRING_CONTINUATION); if (isEs5OrBetterMode()) { errorReporter.warning(STRING_CONTINUATION_WARNING,
private void maybeWarnKeywordProperty(Node node) { if (TokenStream.isKeyword(node.getString())) { features = features.require(Feature.KEYWORDS_AS_PROPERTIES); if (config.languageMode == LanguageMode.ECMASCRIPT3) { errorReporter.warning(INVALID_ES3_PROP_NAME, sourceName, node.getLineno(), node.getCharno()); } } }
private ParseTree parseObjectPattern(PatternKind kind) { features = features.require(Feature.DESTRUCTURING); SourcePosition start = getTreeStartLocation(); ImmutableList.Builder<ParseTree> fields = ImmutableList.builder(); eat(TokenType.OPEN_CURLY); while (peekObjectPatternField()) { fields.add(parseObjectPatternField(kind)); if (peek(TokenType.COMMA)) { // Consume the comma separator eat(TokenType.COMMA); } else { // Otherwise we must be done break; } } eat(TokenType.CLOSE_CURLY); return new ObjectPatternTree(getTreeLocation(start), fields.build()); }
private ParseTree parseNewDotSomething() { // currently only "target" is valid after "new." features = features.require(Feature.NEW_TARGET); SourcePosition start = getTreeStartLocation(); eat(TokenType.NEW); eat(TokenType.PERIOD); eatPredefinedString("target"); return new NewTargetExpressionTree(getTreeLocation(start)); }
public static FeatureSet detectFeatures(String sourcePath, String sourceString) { SourceFile file = new SourceFile(sourcePath, sourceString); ErrorReporter reporter = IRFactory.NULL_REPORTER; com.google.javascript.jscomp.parsing.parser.Parser.Config config = new com.google.javascript.jscomp.parsing.parser.Parser.Config(mode( IRFactory.NULL_CONFIG.languageMode)); Parser p = new Parser(config, new Es6ErrorReporter(reporter, false), file); ProgramTree tree = p.parseProgram(); StaticSourceFile simpleSourceFile = new SimpleSourceFile(sourcePath, false); return IRFactory.detectFeatures(tree, simpleSourceFile, sourceString).require(p.getFeatures()); }
private ParseTree parseArrayPattern(PatternKind kind) { features = features.require(Feature.DESTRUCTURING); SourcePosition start = getTreeStartLocation(); ImmutableList.Builder<ParseTree> elements = ImmutableList.builder();
public static ParseResult parse( StaticSourceFile sourceFile, String sourceString, Config config, ErrorReporter errorReporter) { // TODO(johnlenz): unify "SourceFile", "Es6ErrorReporter" and "Config" SourceFile file = new SourceFile(sourceFile.getName(), sourceString); Es6ErrorReporter es6ErrorReporter = new Es6ErrorReporter(errorReporter, config.isIdeMode); com.google.javascript.jscomp.parsing.parser.Parser.Config es6config = new com.google.javascript.jscomp.parsing.parser.Parser.Config(mode( config.languageMode)); Parser p = new Parser(es6config, es6ErrorReporter, file); ProgramTree tree = p.parseProgram(); Node root = null; List<Comment> comments = ImmutableList.of(); FeatureSet features = p.getFeatures(); if (tree != null && (!es6ErrorReporter.hadError() || config.isIdeMode)) { IRFactory factory = IRFactory.transformTree(tree, sourceFile, sourceString, config, errorReporter); root = factory.getResultNode(); features = features.require(factory.getFeatures()); root.setIsSyntheticBlock(true); root.putProp(Node.FEATURE_SET, features); if (config.isIdeMode) { comments = p.getComments(); } } return new ParseResult(root, comments, features); }