Search in sources :

Example 6 with JSError

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

the class CompilerBasedTransformer method transform.

@Override
public Source transform(Source input) {
    CompileResult result = compilerSupplier.compile(input.path(), input.code());
    if (result.errors.length > 0) {
        // TODO(sdh): how to handle this?  Currently we throw an ISE with the message,
        // but this may not be the most appropriate option.  It might make sense to
        // add console.log() statements to any JS that comes out, particularly for
        // warnings.
        MessageFormatter formatter = ErrorFormat.SOURCELESS.toFormatter(null, false);
        StringBuilder message = new StringBuilder().append(getTranformationName()).append(" failed.\n");
        for (JSError error : result.errors) {
            message.append(formatter.formatError(error));
        }
        throw new IllegalStateException(message.toString());
    }
    if (!result.transformed) {
        return input;
    }
    Source.Builder builder = input.toBuilder().setCode(result.source).setSourceMap(result.sourceMap);
    if (getRuntime().isPresent()) {
        builder.addRuntime(compilerSupplier.runtime(getRuntime().get()));
    }
    return builder.build();
}
Also used : MessageFormatter(com.google.javascript.jscomp.MessageFormatter) JSError(com.google.javascript.jscomp.JSError)

Example 7 with JSError

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

the class ErrorToFixMapperTest method testImplicitNullability3.

@Test
public void testImplicitNullability3() {
    String originalCode = LINE_JOINER.join("/**", " * Some non-ASCII characters: αβγδε", " * @param {Object} o", " */", "function f(o) {}");
    compiler.compile(// Externs
    ImmutableList.<SourceFile>of(), ImmutableList.of(SourceFile.fromCode("test", originalCode)), options);
    assertThat(compiler.getErrors()).isEmpty();
    JSError[] warnings = compiler.getWarnings();
    assertThat(warnings).hasLength(1);
    JSError warning = warnings[0];
    List<SuggestedFix> fixes = ErrorToFixMapper.getFixesForJsError(warning, compiler);
    assertThat(fixes).hasSize(2);
    // First fix is to add "!"
    String newCode = ApplySuggestedFixes.applySuggestedFixesToCode(ImmutableList.of(fixes.get(0)), ImmutableMap.of("test", originalCode)).get("test");
    String expected = LINE_JOINER.join("/**", " * Some non-ASCII characters: αβγδε", " * @param {?Object} o", " */", "function f(o) {}");
    assertThat(newCode).isEqualTo(expected);
    // Second fix is to add "?"
    newCode = ApplySuggestedFixes.applySuggestedFixesToCode(ImmutableList.of(fixes.get(1)), ImmutableMap.of("test", originalCode)).get("test");
    expected = LINE_JOINER.join("/**", " * Some non-ASCII characters: αβγδε", " * @param {!Object} o", " */", "function f(o) {}");
    assertThat(newCode).isEqualTo(expected);
}
Also used : JSError(com.google.javascript.jscomp.JSError) Test(org.junit.Test)

Example 8 with JSError

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

the class ErrorToFixMapperTest method testImplicitNullability1.

@Test
public void testImplicitNullability1() {
    String originalCode = "/** @type {Object} */ var o;";
    compiler.compile(// Externs
    ImmutableList.<SourceFile>of(), ImmutableList.of(SourceFile.fromCode("test", originalCode)), options);
    assertThat(compiler.getErrors()).isEmpty();
    JSError[] warnings = compiler.getWarnings();
    assertThat(warnings).hasLength(1);
    JSError warning = warnings[0];
    List<SuggestedFix> fixes = ErrorToFixMapper.getFixesForJsError(warning, compiler);
    assertThat(fixes).hasSize(2);
    // First fix is to add "!"
    String newCode = ApplySuggestedFixes.applySuggestedFixesToCode(ImmutableList.of(fixes.get(0)), ImmutableMap.of("test", originalCode)).get("test");
    assertThat(newCode).isEqualTo("/** @type {?Object} */ var o;");
    // Second fix is to add "?"
    newCode = ApplySuggestedFixes.applySuggestedFixesToCode(ImmutableList.of(fixes.get(1)), ImmutableMap.of("test", originalCode)).get("test");
    assertThat(newCode).isEqualTo("/** @type {!Object} */ var o;");
}
Also used : JSError(com.google.javascript.jscomp.JSError) Test(org.junit.Test)

Example 9 with JSError

use of com.google.javascript.jscomp.JSError in project structr by structr.

the class MinifiedJavaScriptFile method minify.

static void minify(final MinifiedJavaScriptFile thisFile) throws FrameworkException, IOException {
    logger.info("Running minify: {}", thisFile.getUuid());
    final com.google.javascript.jscomp.Compiler compiler = new com.google.javascript.jscomp.Compiler();
    final SecurityContext securityContext = thisFile.getSecurityContext();
    final CompilerOptions options = new CompilerOptions();
    final CompilationLevel selectedLevel = CompilationLevel.valueOf(thisFile.getOptimizationLevel());
    selectedLevel.setOptionsForCompilationLevel(options);
    compiler.setErrorManager(new BasicErrorManager() {

        @Override
        public void println(final CheckLevel level, final JSError error) {
        }

        @Override
        protected void printSummary() {
            if (getTypedPercent() > 0) {
                if (getErrorCount() + getWarningCount() == 0) {
                    logger.info(SimpleFormat.format("%d error(s), %d warning(s), %.1f%% typed", getErrorCount(), getWarningCount(), getTypedPercent()));
                } else {
                    logger.warn(SimpleFormat.format("%d error(s), %d warning(s), %.1f%% typed", getErrorCount(), getWarningCount(), getTypedPercent()));
                }
            } else if (getErrorCount() + getWarningCount() > 0) {
                logger.warn(SimpleFormat.format("%d error(s), %d warning(s)", getErrorCount(), getWarningCount()));
            }
        }
    });
    compiler.compile(CommandLineRunner.getBuiltinExterns(options.getEnvironment()), MinifiedJavaScriptFile.getSourceFileList(thisFile), options);
    FileHelper.setFileData(thisFile, compiler.toSource().getBytes(), thisFile.getContentType());
    final PropertyMap changedProperties = new PropertyMap();
    changedProperties.put(StructrApp.key(MinifiedJavaScriptFile.class, "warnings"), StringUtils.join(compiler.getWarnings(), System.lineSeparator()));
    changedProperties.put(StructrApp.key(MinifiedJavaScriptFile.class, "errors"), StringUtils.join(compiler.getErrors(), System.lineSeparator()));
    thisFile.setProperties(securityContext, changedProperties);
}
Also used : CompilationLevel(com.google.javascript.jscomp.CompilationLevel) CheckLevel(com.google.javascript.jscomp.CheckLevel) JSError(com.google.javascript.jscomp.JSError) PropertyMap(org.structr.core.property.PropertyMap) BasicErrorManager(com.google.javascript.jscomp.BasicErrorManager) SecurityContext(org.structr.common.SecurityContext) CompilerOptions(com.google.javascript.jscomp.CompilerOptions)

Aggregations

JSError (com.google.javascript.jscomp.JSError)9 Test (org.junit.Test)3 MessageFormatter (com.google.javascript.jscomp.MessageFormatter)2 EntryPoint (com.google.gwt.core.client.EntryPoint)1 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 HTML (com.google.gwt.user.client.ui.HTML)1 Label (com.google.gwt.user.client.ui.Label)1 BasicErrorManager (com.google.javascript.jscomp.BasicErrorManager)1 CheckLevel (com.google.javascript.jscomp.CheckLevel)1 CompilationLevel (com.google.javascript.jscomp.CompilationLevel)1 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)1 DiagnosticType (com.google.javascript.jscomp.DiagnosticType)1 ArrayList (java.util.ArrayList)1 SecurityContext (org.structr.common.SecurityContext)1 PropertyMap (org.structr.core.property.PropertyMap)1