本文整理汇总了Java中com.google.javascript.jscomp.CompilerOptions.setEmitUseStrict方法的典型用法代码示例。如果您正苦于以下问题:Java CompilerOptions.setEmitUseStrict方法的具体用法?Java CompilerOptions.setEmitUseStrict怎么用?Java CompilerOptions.setEmitUseStrict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.jscomp.CompilerOptions
的用法示例。
在下文中一共展示了CompilerOptions.setEmitUseStrict方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testPropType
import com.google.javascript.jscomp.CompilerOptions; //导入方法依赖的package包/类
private void testPropType(String reactPropType, String typeExpression) {
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();
// So that source dumps still have JSDoc
options.preserveTypeAnnotations = true;
compiler.initOptions(options);
// Avoid extra "use strict" boilerplate in output.
options.setEmitUseStrict(false);
compiler.disableThreads(); // Makes errors easier to track down.
Node reactPropTypeNode = compiler.parse(
SourceFile.fromCode("/src/test.js", reactPropType))
.getFirstChild().getFirstChild();
assertArrayEquals(new JSError[]{}, compiler.getErrors());
Node typeNode = PropTypesExtractor.convertPropType(reactPropTypeNode)
.typeNode;
// Easiest way to stringify the type node is to print it out as JSDoc.
JSDocInfoBuilder jsDocInfoBuilder = new JSDocInfoBuilder(true);
jsDocInfoBuilder.recordType(new JSTypeExpression(
typeNode, "/src/test.js"));
Node tempNode = IR.var(IR.name("temp"));
tempNode.setJSDocInfo(jsDocInfoBuilder.build());
String tempCode = compiler.toSource(tempNode);
assertEquals("/** @type {" + typeExpression + "} */ var temp", tempCode);
}
示例2: getOptions
import com.google.javascript.jscomp.CompilerOptions; //导入方法依赖的package包/类
@Override
protected CompilerOptions getOptions() {
CompilerOptions options = super.getOptions();
options.setPrettyPrint(true);
options.setPreserveTypeAnnotations(true);
options.setPreferSingleQuotes(true);
options.setEmitUseStrict(false);
return options;
}
示例3: outputSource
import com.google.javascript.jscomp.CompilerOptions; //导入方法依赖的package包/类
/**
* Outputs the source equivalent of the abstract syntax tree, optionally generating a sourcemap.
*
* @param node The JavaScript abstract syntax tree.
* @param outputFormat The source output format options.
* @param inputFileName The source file name to associate with the input node, used for sourcemap
* generation.
* @param inputSourceMap The content of the input sourcemap.
* @param sourceMapOutputFileName The name of the output sourcemap.
* @return The equivalent JavaScript source.
*/
private static String outputSource(
Node node,
ImmutableSet<OutputFormat> outputFormat,
String inputFileName,
String inputSourceMap,
String sourceMapOutputFileName) {
CompilerOptions options = new CompilerOptions();
options.setPrettyPrint(outputFormat.contains(OutputFormat.PRETTY));
options.setPreferSingleQuotes(outputFormat.contains(OutputFormat.SINGLE_QUOTE_STRINGS));
// The Closure Compiler treats the 'use strict' directive as a property of a node. CodeBuilder
// doesn't consider directives during its code generation. Instead, it inserts the 'use strict'
// directive if it is in a strict language mode.
Set<String> directives = node.getDirectives();
if ((directives != null) && directives.contains("use strict")) {
options.setLanguage(CompilerOptions.LanguageMode.ECMASCRIPT_2015);
options.setEmitUseStrict(true);
}
options.skipAllCompilerPasses();
if (inputSourceMap != null) {
SourceFile sourceMapSourceFile = SourceFile.fromCode("input.sourcemap", inputSourceMap);
ImmutableMap<String, SourceMapInput> inputSourceMaps =
ImmutableMap.of(inputFileName, new SourceMapInput(sourceMapSourceFile));
options.setInputSourceMaps(inputSourceMaps);
options.setApplyInputSourceMaps(true);
// Simply setting the path to any non-null value will trigger source map generation.
// Since sourceMapOutputPath is handled by AbstractCommandLineRunner and not the Compiler
// itself, we manually output the final sourcemap below.
options.setSourceMapOutputPath("/dev/null");
}
Compiler compiler = new Compiler();
compiler.disableThreads();
compiler.initOptions(options);
compiler.initBasedOnOptions();
Compiler.CodeBuilder cb = new Compiler.CodeBuilder();
compiler.toSource(cb, 0, node);
if (inputFileName != null && inputSourceMap != null && sourceMapOutputFileName != null) {
try {
FileOutputStream fileOut = new FileOutputStream(sourceMapOutputFileName);
OutputStreamWriter out = new OutputStreamWriter(fileOut, UTF_8);
compiler.getSourceMap().appendTo(out, "renamed.js");
out.close();
} catch (Exception e) {
System.err.println(e + "Error writing output sourcemap.");
}
}
return cb.toString();
}