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();
}
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);
}
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;");
}
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);
}
Aggregations