use of com.google.javascript.rhino.InputId in project closure-compiler by google.
the class Compiler method addNewScript.
/**
* Adds a new Script AST to the compile state. If a script for the same file
* already exists the script will not be added, instead a call to
* #replaceScript should be used.
*
* @param ast the ast of the new file
*/
public void addNewScript(JsAst ast) {
if (!addNewSourceAst(ast)) {
return;
}
Node emptyScript = new Node(Token.SCRIPT);
InputId inputId = ast.getInputId();
emptyScript.setInputId(inputId);
emptyScript.setStaticSourceFile(SourceFile.fromCode(inputId.getIdName(), ""));
processNewScript(ast, emptyScript);
}
use of com.google.javascript.rhino.InputId in project closure-compiler by google.
the class AstValidatorTest method testValidScript.
public void testValidScript() {
Node n = new Node(Token.SCRIPT);
expectInvalid(n, Check.SCRIPT);
n.setInputId(new InputId("something_input"));
n.setStaticSourceFile(new SimpleSourceFile("something", false));
expectValid(n, Check.SCRIPT);
expectInvalid(n, Check.STATEMENT);
expectInvalid(n, Check.EXPRESSION);
}
use of com.google.javascript.rhino.InputId in project closure-compiler by google.
the class LiveVariablesAnalysisTest method computeLiveness.
private static LiveVariablesAnalysis computeLiveness(String src) {
// Set up compiler
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();
options.setLanguage(LanguageMode.ECMASCRIPT_2015);
options.setCodingConvention(new GoogleCodingConvention());
compiler.initOptions(options);
compiler.setLifeCycleStage(LifeCycleStage.NORMALIZED);
// Set up test case
src = "function _FUNCTION(param1, param2 = 1, ...param3){" + src + "}";
Node n = compiler.parseTestCode(src).removeFirstChild();
checkState(n.isFunction(), n);
Node script = new Node(Token.SCRIPT, n);
script.setInputId(new InputId("test"));
assertThat(compiler.getErrors()).isEmpty();
// Create scopes
Es6SyntacticScopeCreator scopeCreator = new Es6SyntacticScopeCreator(compiler);
Scope scope = scopeCreator.createScope(n, Scope.createGlobalScope(script));
Scope childScope = scopeCreator.createScope(NodeUtil.getFunctionBody(n), scope);
// Control flow graph
ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, false, true);
cfa.process(null, n);
ControlFlowGraph<Node> cfg = cfa.getCfg();
// Compute liveness of variables
LiveVariablesAnalysis analysis = new LiveVariablesAnalysis(cfg, scope, childScope, compiler, new Es6SyntacticScopeCreator(compiler));
analysis.analyze();
return analysis;
}
use of com.google.javascript.rhino.InputId in project closure-compiler by google.
the class TypeCheckTest method parseAndTypeCheckWithScope.
private TypeCheckResult parseAndTypeCheckWithScope(String externs, String js) {
registry.clearNamedTypes();
registry.clearTemplateTypeNames();
compiler.init(ImmutableList.of(SourceFile.fromCode("[externs]", externs)), ImmutableList.of(SourceFile.fromCode("[testcode]", js)), compiler.getOptions());
compiler.setFeatureSet(compiler.getFeatureSet().without(Feature.MODULES));
Node jsNode = IR.root(compiler.getInput(new InputId("[testcode]")).getAstRoot(compiler));
Node externsNode = IR.root(compiler.getInput(new InputId("[externs]")).getAstRoot(compiler));
Node externAndJsRoot = IR.root(externsNode, jsNode);
compiler.jsRoot = jsNode;
compiler.externsRoot = externsNode;
compiler.externAndJsRoot = externAndJsRoot;
assertEquals("parsing error: " + Joiner.on(", ").join(compiler.getErrors()), 0, compiler.getErrorCount());
if (compiler.getOptions().needsTranspilationFrom(FeatureSet.ES6)) {
List<PassFactory> passes = new ArrayList<>();
TranspilationPasses.addEs2017Passes(passes);
TranspilationPasses.addEs2016Passes(passes);
TranspilationPasses.addEs6PreTypecheckPasses(passes);
PhaseOptimizer phaseopt = new PhaseOptimizer(compiler, null);
phaseopt.consume(passes);
phaseopt.process(externsNode, jsNode);
}
TypedScope s = makeTypeCheck().processForTesting(externsNode, jsNode);
return new TypeCheckResult(jsNode.getFirstChild(), s);
}
use of com.google.javascript.rhino.InputId in project closure-compiler by google.
the class DataFlowAnalysisTest method computeEscapedLocals.
// test computeEscaped helper method that returns the liveness analysis performed by the
// LiveVariablesAnalysis class
public Set<? extends Var> computeEscapedLocals(String... lines) {
// Set up compiler
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();
options.setLanguage(LanguageMode.ECMASCRIPT_2015);
options.setCodingConvention(new GoogleCodingConvention());
compiler.initOptions(options);
compiler.setLifeCycleStage(LifeCycleStage.NORMALIZED);
String src = CompilerTestCase.lines(lines);
Node n = compiler.parseTestCode(src).removeFirstChild();
Node script = new Node(Token.SCRIPT, n);
script.setInputId(new InputId("test"));
assertThat(compiler.getErrors()).isEmpty();
// Create scopes
Es6SyntacticScopeCreator scopeCreator = new Es6SyntacticScopeCreator(compiler);
Scope scope = scopeCreator.createScope(n, Scope.createGlobalScope(script));
Scope childScope;
if (script.getFirstChild().isFunction()) {
childScope = scopeCreator.createScope(NodeUtil.getFunctionBody(n), scope);
} else {
childScope = null;
}
// Control flow graph
ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, false, true);
cfa.process(null, script);
ControlFlowGraph<Node> cfg = cfa.getCfg();
// Compute liveness of variables
LiveVariablesAnalysis analysis = new LiveVariablesAnalysis(cfg, scope, childScope, compiler, scopeCreator);
analysis.analyze();
return analysis.getEscapedLocals();
}
Aggregations