Search in sources :

Example 11 with InputId

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);
}
Also used : Node(com.google.javascript.rhino.Node) InputId(com.google.javascript.rhino.InputId)

Example 12 with InputId

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);
}
Also used : Node(com.google.javascript.rhino.Node) InputId(com.google.javascript.rhino.InputId) SimpleSourceFile(com.google.javascript.rhino.SimpleSourceFile)

Example 13 with InputId

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;
}
Also used : Node(com.google.javascript.rhino.Node) InputId(com.google.javascript.rhino.InputId)

Example 14 with InputId

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);
}
Also used : Node(com.google.javascript.rhino.Node) InputId(com.google.javascript.rhino.InputId) ArrayList(java.util.ArrayList)

Example 15 with InputId

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();
}
Also used : GraphNode(com.google.javascript.jscomp.graph.GraphNode) Node(com.google.javascript.rhino.Node) InputId(com.google.javascript.rhino.InputId)

Aggregations

InputId (com.google.javascript.rhino.InputId)15 Node (com.google.javascript.rhino.Node)9 Compiler (com.google.javascript.jscomp.Compiler)1 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 GraphNode (com.google.javascript.jscomp.graph.GraphNode)1 Config (com.google.javascript.jscomp.parsing.Config)1 ParserRunner (com.google.javascript.jscomp.parsing.ParserRunner)1 Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)1 ErrorReporter (com.google.javascript.rhino.ErrorReporter)1 SimpleSourceFile (com.google.javascript.rhino.SimpleSourceFile)1 ArrayList (java.util.ArrayList)1