Search in sources :

Example 1 with ParseResult

use of com.google.javascript.jscomp.parsing.ParserRunner.ParseResult in project closure-compiler by google.

the class ParserTest method parseError.

/**
 * Verify that the given code has the given parse errors.
 * @return If in IDE mode, returns a partial tree.
 */
private Node parseError(String source, String... errors) {
    TestErrorReporter testErrorReporter = new TestErrorReporter(errors, null);
    ParseResult result = ParserRunner.parse(new SimpleSourceFile("input", false), source, createConfig(), testErrorReporter);
    Node script = result.ast;
    // check expected features if specified
    assertFS(result.features).contains(expectedFeatures);
    // verifying that all errors were seen
    testErrorReporter.assertHasEncounteredAllErrors();
    testErrorReporter.assertHasEncounteredAllWarnings();
    return script;
}
Also used : TestErrorReporter(com.google.javascript.rhino.testing.TestErrorReporter) ParseResult(com.google.javascript.jscomp.parsing.ParserRunner.ParseResult) NodeSubject.assertNode(com.google.javascript.jscomp.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) SimpleSourceFile(com.google.javascript.rhino.SimpleSourceFile)

Example 2 with ParseResult

use of com.google.javascript.jscomp.parsing.ParserRunner.ParseResult in project closure-compiler by google.

the class ParserTest method testParseSourceMapRelativeURL.

public void testParseSourceMapRelativeURL() {
    String code = "var X = (function () {\n" + "    function X(input) {\n" + "        this.y = input;\n" + "    }\n" + "    return X;\n" + "}());\n" + "console.log(new X(1));\n" + "//# sourceMappingURL=somefile.js.map";
    ParseResult result = doParse(code);
    assertThat(result.sourceMapURL).isEqualTo("somefile.js.map");
}
Also used : ParseResult(com.google.javascript.jscomp.parsing.ParserRunner.ParseResult)

Example 3 with ParseResult

use of com.google.javascript.jscomp.parsing.ParserRunner.ParseResult in project closure-compiler by google.

the class TypeTransformationParser method parseTypeTransformation.

/**
 * Takes a type transformation expression, transforms it to an AST using
 * the ParserRunner of the JSCompiler and then verifies that it is a valid
 * AST.
 * @return true if the parsing was successful otherwise it returns false and
 * at least one warning is reported
 */
public boolean parseTypeTransformation() {
    Config config = Config.builder().setLanguageMode(Config.LanguageMode.ECMASCRIPT6).setStrictMode(Config.StrictMode.SLOPPY).build();
    // TODO(lpino): ParserRunner reports errors if the expression is not
    // ES6 valid. We need to abort the validation of the type transformation
    // whenever an error is reported.
    ParseResult result = ParserRunner.parse(sourceFile, typeTransformationString, config, errorReporter);
    Node ast = result.ast;
    // Check that the expression is a script with an expression result
    if (!ast.isScript() || !ast.getFirstChild().isExprResult()) {
        warnInvalidExpression("type transformation", ast);
        return false;
    }
    Node expr = ast.getFirstFirstChild();
    // The AST of the type transformation must correspond to a valid expression
    if (!validTypeTransformationExpression(expr)) {
        // No need to add a new warning because the validation does it
        return false;
    }
    fixLineNumbers(expr);
    // Store the result if the AST is valid
    typeTransformationAst = expr;
    return true;
}
Also used : ParseResult(com.google.javascript.jscomp.parsing.ParserRunner.ParseResult) Node(com.google.javascript.rhino.Node)

Example 4 with ParseResult

use of com.google.javascript.jscomp.parsing.ParserRunner.ParseResult in project closure-compiler by google.

the class ParserTest method testParseSourceMapAbsoluteURL.

/**
 * In the future, we may want absolute URLs to be mapable based on how the server exposes the
 * sources. See: b/62544959.
 */
public void testParseSourceMapAbsoluteURL() {
    String code = "console.log('asdf');\n" + "//# sourceMappingURL=/some/absolute/path/to/somefile.js.map";
    ParseResult result = doParse(code);
    assertThat(result.sourceMapURL).isEqualTo("/some/absolute/path/to/somefile.js.map");
}
Also used : ParseResult(com.google.javascript.jscomp.parsing.ParserRunner.ParseResult)

Example 5 with ParseResult

use of com.google.javascript.jscomp.parsing.ParserRunner.ParseResult in project closure-compiler by google.

the class ParserTest method testParseSourceMapAbsoluteURLHTTP.

/**
 * In the future, we may want absolute URLs to me mapable based on how the server exposes the
 * sources. See: b/62544959.
 */
public void testParseSourceMapAbsoluteURLHTTP() {
    String code = "console.log('asdf');\n" + "//# sourceMappingURL=http://google.com/some/absolute/path/to/somefile.js.map";
    ParseResult result = doParse(code);
    assertThat(result.sourceMapURL).isEqualTo("http://google.com/some/absolute/path/to/somefile.js.map");
}
Also used : ParseResult(com.google.javascript.jscomp.parsing.ParserRunner.ParseResult)

Aggregations

ParseResult (com.google.javascript.jscomp.parsing.ParserRunner.ParseResult)6 Node (com.google.javascript.rhino.Node)2 NodeSubject.assertNode (com.google.javascript.jscomp.testing.NodeSubject.assertNode)1 SimpleSourceFile (com.google.javascript.rhino.SimpleSourceFile)1 TestErrorReporter (com.google.javascript.rhino.testing.TestErrorReporter)1