/** * Returns a new {@link FeatureSet} including all features of both {@code this} and {@code other}. */ public FeatureSet union(FeatureSet other) { return new FeatureSet(union(features, other.features)); }
/** Returns a feature set combining all the features from {@code this} and {@code feature}. */ public FeatureSet with(Feature feature) { if (features.contains(feature)) { return this; } return new FeatureSet(add(features, feature)); }
/** * @param script The SCRIPT node representing a JS file * @return If the file has any features not in {@code supportedFeatures} */ static boolean doesScriptHaveUnsupportedFeatures(Node script, FeatureSet supportedFeatures) { FeatureSet features = NodeUtil.getFeatureSetOfScript(script); return features != null && !supportedFeatures.contains(features); }
public FeatureSet without(FeatureSet other) { return new FeatureSet(difference(features, other.features)); }
void visitTree(Node n) { visitSubtree(n, null); checkArgument(n.isScript()); FeatureSet features = (FeatureSet) n.getProp(Node.FEATURE_SET); if (features != null) { // Only add the "lang" load flag if it's not the default (es3), so that // legacy deps files will remain unchanged (i.e. load flags omitted). String version = features.version(); if (!version.equals("es3")) { loadFlags.put("lang", version); } } }
@Override public ImmutableMap<String, String> getLoadFlags() { if (loadFlags == null) { Map<String, String> loadFlagsBuilder = new TreeMap<String, String>(); loadFlagsBuilder.putAll(delegate.getLoadFlags()); FeatureSet features = ((JsAst) ast).getFeatures(compiler); if (features.hasEs6Modules()) { if (loadFlagsBuilder.containsKey("module")) { compiler.report(JSError.make(MODULE_CONFLICT, getName())); } loadFlagsBuilder.put("module", "es6"); } String version = features.version(); if (!version.equals("es3")) { loadFlagsBuilder.put("lang", version); } loadFlags = ImmutableMap.copyOf(loadFlagsBuilder); } return loadFlags; }
@Override public ImmutableMap<String, String> getLoadFlags() { if (loadFlags == null) { Map<String, String> loadFlagsBuilder = new TreeMap<>(); loadFlagsBuilder.putAll(delegate.getLoadFlags()); FeatureSet features = ast.getFeatures(compiler); if (features.has(Feature.MODULES)) { String previousModule = loadFlagsBuilder.get("module"); if (previousModule != null && !previousModule.equals("es6")) { compiler.report(JSError.make(ModuleLoader.MODULE_CONFLICT, getName())); } loadFlagsBuilder.put("module", "es6"); } String version = features.version(); if (!version.equals("es3")) { loadFlagsBuilder.put("lang", version); } loadFlags = ImmutableMap.copyOf(loadFlagsBuilder); // Don't preserve the full AST longer than necessary. It can consume a lot of memory. ast = null; } return loadFlags; }
FeatureSet used = FeatureSet.ES3; for (Node script : root.children()) { used = used.with(getScriptFeatures(script)); FeatureSet mustBeCompiledAway = used.without(compiler.getOptions().getOutputFeatureSet()); if (mustBeCompiledAway.contains(Feature.FOR_OF)) { Es6ToEs3Util.preloadEs6RuntimeFunction(compiler, "makeIterator"); if (mustBeCompiledAway.contains(Feature.ARRAY_DESTRUCTURING)) { Es6ToEs3Util.preloadEs6RuntimeFunction(compiler, "makeIterator"); if (mustBeCompiledAway.contains(Feature.ARRAY_PATTERN_REST)) { Es6ToEs3Util.preloadEs6RuntimeFunction(compiler, "arrayFromIterator"); if (mustBeCompiledAway.contains(Feature.SPREAD_EXPRESSIONS)) { Es6ToEs3Util.preloadEs6RuntimeFunction(compiler, "arrayfromiterable"); if (mustBeCompiledAway.contains(Feature.CLASS_EXTENDS)) { Es6ToEs3Util.preloadEs6RuntimeFunction(compiler, "inherits"); if (mustBeCompiledAway.contains(Feature.CLASS_GETTER_SETTER)) { compiler.ensureLibraryInjected("util/global", /* force= */ false); if (mustBeCompiledAway.contains(Feature.GENERATORS)) { compiler.ensureLibraryInjected("es6/generator_engine", /* force= */ false); if (mustBeCompiledAway.contains(Feature.ASYNC_FUNCTIONS)) { compiler.ensureLibraryInjected("es6/execute_async_generator", /* force= */ false);
/** * Returns the lowest {@link LanguageMode} that supports the specified feature. */ public static LanguageMode minimumRequiredFor(FeatureSet.Feature feature) { // relies on the LanguageMode enums being in the right order for (LanguageMode mode : LanguageMode.values()) { if (mode.featureSet.has(feature)) { return mode; } } throw new IllegalStateException("No input language mode supports feature: " + feature); }
/** Returns a string representation suitable for encoding in depgraph and deps.js files. */ public String version() { if (ES3.contains(this)) { return "es3"; } if (ES5.contains(this)) { return "es5"; } if (ES6_MODULES.contains(this)) { return "es6"; } if (ES7_MODULES.contains(this)) { return "es7"; } if (ES8_MODULES.contains(this)) { return "es8"; } if (ES2018_MODULES.contains(this)) { return "es9"; } if (ES_NEXT.contains(this)) { return "es_next"; } if (TYPESCRIPT.contains(this)) { return "ts"; } throw new IllegalStateException(this.toString()); }
/** Returns a feature set combining all the features from {@code this} and {@code other}. */ public FeatureSet require(FeatureSet other) { if (other.number > number || (other.unsupported && !unsupported) || (other.es6Modules && !es6Modules) || (other.typeScript && !typeScript)) { return new FeatureSet( Math.max(number, other.number), unsupported || other.unsupported, es6Modules || other.es6Modules, typeScript || other.typeScript); } return this; }
} else if (options.getLanguageIn().toFeatureSet().has(FeatureSet.Feature.MODULES)) { checks.add(rewriteScriptsToEs6Modules); if (options.getLanguageIn().toFeatureSet().has(FeatureSet.Feature.MODULES)) { checks.add(rewriteGoogJsImports); TranspilationPasses.addEs6ModulePass(checks, preprocessorSymbolTableFactory); if (options.dartPass && !options.getOutputFeatureSet().contains(ES6)) { checks.add(dartSuperAccessorsPass);
FeatureSet featuresSupportedByPass = factory.featureSet(); if (compiler.getOptions().shouldSkipUnsupportedPasses() && !featuresSupportedByPass.contains(featuresInAst)) { FeatureSet unsupportedFeatures = featuresInAst.without(featuresSupportedByPass); logger.warning("AST contains unsupported features: " + unsupportedFeatures); return;
Node scriptNode = NodeUtil.getEnclosingScript(parent); FeatureSet oldFeatures = (FeatureSet) scriptNode.getProp(Node.FEATURE_SET); FeatureSet newFeatures = oldFeatures.union(cls.features); if (!newFeatures.equals(oldFeatures)) { scriptNode.putProp(Node.FEATURE_SET, newFeatures); compiler.reportChangeToChangeScope(scriptNode);
private static EnumSet<Feature> union(Set<Feature> features, Set<Feature> newFeatures) { EnumSet<Feature> result = enumSetOf(features); result.addAll(newFeatures); return result; }
private static EnumSet<Feature> enumSetOf(Set<Feature> set) { return set.isEmpty() ? emptyEnumSet() : EnumSet.copyOf(set); }
public FeatureSet withoutTypes() { return new FeatureSet(difference(features, LangVersion.TYPESCRIPT.features())); }
void visitTree(Node n) { visitSubtree(n, null); Preconditions.checkArgument(n.isScript()); FeatureSet features = (FeatureSet) n.getProp(Node.FEATURE_SET); if (features != null) { // Only add the "lang" load flag if it's not the default (es3), so that // legacy deps files will remain unchanged (i.e. load flags omitted). String version = features.version(); if (!version.equals("es3")) { loadFlags.put("lang", version); } } }
boolean isSupportedForInputLanguageMode(Feature feature) { return config.languageMode().featureSet.has(feature); }
if (ES3.contains(this)) { return "es3"; if (ES5.contains(this)) { return "es5"; if (TYPE_CHECK_SUPPORTED.contains(this)) { return "typeCheckSupported"; if (ES6_MODULES.contains(this)) { return "es6"; if (ES7_MODULES.contains(this)) { return "es7"; if (ES8_MODULES.contains(this)) { return "es8"; if (ES2018_MODULES.contains(this)) { return "es9"; if (ES_NEXT.contains(this)) { return "es_next"; if (TYPESCRIPT.contains(this)) { return "ts"; throw new IllegalStateException(this.toString());