Search in sources :

Example 41 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class MatchersTest method testJsDocType2.

@Test
public void testJsDocType2() {
    String input = "/** @type {number} */ let foo = 1;";
    Compiler compiler = getCompiler(input);
    Node root = compileToScriptRoot(compiler);
    Node node = root.getFirstFirstChild();
    assertTrue(Matchers.jsDocType("number").matches(node, new NodeMetadata(compiler)));
    assertFalse(Matchers.jsDocType("string").matches(node, new NodeMetadata(compiler)));
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Example 42 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class SuggestedFixTest method testDeleteArgumentLast.

@Test
public void testDeleteArgumentLast() {
    String originalCode = "f(a, b, c);";
    String expectedCode = "f(a, b);";
    Compiler compiler = getCompiler(originalCode);
    Node root = compileToScriptRoot(compiler);
    SuggestedFix fix = new SuggestedFix.Builder().deleteArgument(root.getFirstFirstChild(), 2).build();
    assertChanges(fix, "", originalCode, expectedCode);
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Example 43 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class SuggestedFixTest method testReplace_lowerPrecedence.

@Test
public void testReplace_lowerPrecedence() {
    String before = "2 * ";
    String after = "3";
    Compiler compiler = getCompiler(before + after + ";\n");
    Node root = compileToScriptRoot(compiler);
    Node newNode = IR.exprResult(IR.sub(IR.number(4), IR.number(1)));
    Node toReplace = root.getOnlyChild().getOnlyChild().getLastChild();
    SuggestedFix fix = new SuggestedFix.Builder().replace(toReplace, newNode, compiler).build();
    CodeReplacement replacement = CodeReplacement.create(before.length(), after.length(), "(4 - 1)");
    assertReplacement(fix, replacement);
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Example 44 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class SuggestedFixTest method testDelete_multipleConstDeclaration.

@Test
public void testDelete_multipleConstDeclaration() {
    String input = "const foo = 3, bar = 4, baz = 5;";
    Compiler compiler = getCompiler(input);
    Node root = compileToScriptRoot(compiler);
    // Delete the 1st variable on the line. Make sure the deletion includes the assignment and the
    // trailing comma.
    SuggestedFix fix = new SuggestedFix.Builder().delete(root.getFirstFirstChild()).build();
    CodeReplacement replacement = CodeReplacement.create(6, "foo = 3, ".length(), "");
    assertReplacement(fix, replacement);
    // Delete the 2nd variable.
    fix = new SuggestedFix.Builder().delete(root.getFirstChild().getSecondChild()).build();
    replacement = CodeReplacement.create(15, "bar = 4, ".length(), "");
    assertReplacement(fix, replacement);
    // Delete the last variable. Make sure it removes the leading comma.
    fix = new SuggestedFix.Builder().delete(root.getFirstChild().getLastChild()).build();
    replacement = CodeReplacement.create(22, ", baz = 5".length(), "");
    assertReplacement(fix, replacement);
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Example 45 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class SuggestedFixTest method assertAddGoogRequire.

private static void assertAddGoogRequire(String before, String after, String namespace) {
    Compiler compiler = getCompiler(before + after);
    Node root = compileToScriptRoot(compiler);
    Match match = new Match(root.getFirstChild(), new NodeMetadata(compiler));
    SuggestedFix fix = new SuggestedFix.Builder().addGoogRequire(match, namespace).build();
    CodeReplacement replacement = CodeReplacement.create(before.length(), 0, "goog.require('" + namespace + "');\n", namespace);
    assertReplacement(fix, replacement);
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Node(com.google.javascript.rhino.Node)

Aggregations

Compiler (com.google.javascript.jscomp.Compiler)107 Test (org.junit.Test)83 Node (com.google.javascript.rhino.Node)80 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)15 SourceFile (com.google.javascript.jscomp.SourceFile)11 IOException (java.io.IOException)6 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Result (com.google.javascript.jscomp.Result)3 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)2 BlackHoleErrorManager (com.google.javascript.jscomp.BlackHoleErrorManager)2 CompilerInput (com.google.javascript.jscomp.CompilerInput)2 JSModule (com.google.javascript.jscomp.JSModule)2 JSSourceFile (com.google.javascript.jscomp.JSSourceFile)2 CircularDependencyException (com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException)2 AbstractJSProject (net.vtst.ow.closure.compiler.deps.AbstractJSProject)2 JSProject (net.vtst.ow.closure.compiler.deps.JSProject)2 ErrorManager (com.google.javascript.jscomp.ErrorManager)1 JsAst (com.google.javascript.jscomp.JsAst)1 LazyParsedDependencyInfo (com.google.javascript.jscomp.LazyParsedDependencyInfo)1