Search in sources :

Example 1 with LanguageMode

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

the class GwtRunner method applyOptionsFromFlags.

private static void applyOptionsFromFlags(CompilerOptions options, Flags flags) {
    CompilationLevel level = DEFAULT_COMPILATION_LEVEL;
    if (flags.compilationLevel != null) {
        level = CompilationLevel.fromString(Ascii.toUpperCase(flags.compilationLevel));
        if (level == null) {
            throw new RuntimeException("Bad value for compilationLevel: " + flags.compilationLevel);
        }
    }
    if (level == CompilationLevel.ADVANCED_OPTIMIZATIONS && !flags.renaming) {
        throw new RuntimeException("renaming cannot be disabled when ADVANCED_OPTIMIZATIONS is used");
    }
    level.setOptionsForCompilationLevel(options);
    if (flags.assumeFunctionWrapper) {
        level.setWrappedOutputOptimizations(options);
    }
    if (flags.useTypesForOptimization) {
        level.setTypeBasedOptimizationOptions(options);
    }
    WarningLevel warningLevel = WarningLevel.DEFAULT;
    if (flags.warningLevel != null) {
        warningLevel = WarningLevel.valueOf(flags.warningLevel);
    }
    warningLevel.setOptionsForWarningLevel(options);
    CompilerOptions.Environment environment = CompilerOptions.Environment.BROWSER;
    if (flags.env != null) {
        environment = CompilerOptions.Environment.valueOf(Ascii.toUpperCase(flags.env));
    }
    options.setEnvironment(environment);
    CompilerOptions.DependencyMode dependencyMode = CompilerOptions.DependencyMode.NONE;
    if (flags.dependencyMode != null) {
        dependencyMode = CompilerOptions.DependencyMode.valueOf(Ascii.toUpperCase(flags.dependencyMode));
    }
    List<ModuleIdentifier> entryPoints = createEntryPoints(getStringArray(flags, "entryPoint"));
    DependencyOptions dependencyOptions = createDependencyOptions(dependencyMode, entryPoints);
    if (dependencyOptions != null) {
        options.setDependencyOptions(dependencyOptions);
    }
    LanguageMode languageIn = LanguageMode.fromString(flags.languageIn);
    if (languageIn != null) {
        options.setLanguageIn(languageIn);
    }
    LanguageMode languageOut = LanguageMode.fromString(flags.languageOut);
    if (languageOut != null) {
        options.setLanguageOut(languageOut);
    }
    if (flags.createSourceMap) {
        options.setSourceMapOutputPath("%output%");
    }
    if (flags.defines != null) {
        // CompilerOptions also validates types, but uses Preconditions and therefore won't generate
        // a useful exception.
        flags.defines.validatePrimitiveTypes();
        options.setDefineReplacements(flags.defines.asMap());
    }
    if (flags.extraAnnotationNames != null) {
        options.setExtraAnnotationNames(Arrays.asList(flags.extraAnnotationNames));
    }
    if (flags.tracerMode != null) {
        options.setTracerMode(TracerMode.valueOf(flags.tracerMode));
    }
    if (flags.moduleResolutionMode != null) {
        options.setModuleResolutionMode(ResolutionMode.valueOf(flags.moduleResolutionMode));
    }
    if (flags.isolationMode != null && IsolationMode.valueOf(flags.isolationMode) == IsolationMode.IIFE) {
        flags.outputWrapper = "(function(){%output%}).call(this);";
    }
    options.setAngularPass(flags.angularPass);
    options.setApplyInputSourceMaps(flags.applyInputSourceMaps);
    options.setChecksOnly(flags.checksOnly);
    options.setDartPass(flags.dartPass);
    options.setExportLocalPropertyDefinitions(flags.exportLocalPropertyDefinitions);
    options.setGenerateExports(flags.generateExports);
    options.setNewTypeInference(flags.newTypeInf);
    if (flags.polymerPass) {
        options.setPolymerVersion(1);
    } else if (flags.polymerVersion != null) {
        options.setPolymerVersion(flags.polymerVersion.intValue());
    }
    options.setPreserveTypeAnnotations(flags.preserveTypeAnnotations);
    options.setClosurePass(flags.processClosurePrimitives);
    options.setProcessCommonJSModules(flags.processCommonJsModules);
    options.setRenamePrefixNamespace(flags.renamePrefixNamespace);
    if (!flags.renaming) {
        options.setVariableRenaming(VariableRenamingPolicy.OFF);
        options.setPropertyRenaming(PropertyRenamingPolicy.OFF);
    }
    options.setRewritePolyfills(flags.rewritePolyfills);
}
Also used : CompilationLevel(com.google.javascript.jscomp.CompilationLevel) LanguageMode(com.google.javascript.jscomp.CompilerOptions.LanguageMode) WarningLevel(com.google.javascript.jscomp.WarningLevel) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) DependencyOptions(com.google.javascript.jscomp.DependencyOptions) ModuleIdentifier(com.google.javascript.jscomp.ModuleIdentifier)

Example 2 with LanguageMode

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

the class CommandLineRunner method createOptions.

@Override
protected CompilerOptions createOptions() {
    CompilerOptions options = new CompilerOptions();
    if (!flags.languageIn.isEmpty()) {
        CompilerOptions.LanguageMode languageMode = CompilerOptions.LanguageMode.fromString(flags.languageIn);
        if (languageMode == LanguageMode.UNSUPPORTED) {
            throw new FlagUsageException("Cannot specify the unsupported set of features for language_in.");
        }
        if (languageMode != null) {
            options.setLanguageIn(languageMode);
        } else {
            throw new FlagUsageException("Unknown language `" + flags.languageIn + "' specified.");
        }
    }
    CompilerOptions.LanguageMode languageMode = CompilerOptions.LanguageMode.fromString(flags.languageOut);
    if (languageMode == LanguageMode.UNSUPPORTED) {
        throw new FlagUsageException("Cannot specify the unsupported set of features for language_out.");
    }
    if (languageMode != null) {
        options.setLanguageOut(languageMode);
    } else {
        throw new FlagUsageException("Unknown language `" + flags.languageOut + "' specified.");
    }
    options.setCodingConvention(new ClosureCodingConvention());
    options.setExtraAnnotationNames(flags.extraAnnotationName);
    CompilationLevel level = flags.compilationLevelParsed;
    level.setOptionsForCompilationLevel(options);
    if (flags.debug) {
        level.setDebugOptionsForCompilationLevel(options);
    }
    options.setNumParallelThreads(flags.numParallelThreads);
    options.setEnvironment(flags.environment);
    options.setChecksOnly(flags.checksOnly);
    if (flags.checksOnly) {
        options.setOutputJs(CompilerOptions.OutputJs.NONE);
    }
    options.setIncrementalChecks(flags.incrementalCheckMode);
    options.setContinueAfterErrors(flags.continueAfterErrors);
    // TODO(b/144593112): remove this flag.
    options.setBadRewriteModulesBeforeTypecheckingThatWeWantToGetRidOf(true);
    if (flags.useTypesForOptimization) {
        level.setTypeBasedOptimizationOptions(options);
    }
    if (flags.assumeFunctionWrapper || flags.isolationMode == IsolationMode.IIFE || flags.chunkOutputType == ChunkOutputType.ES_MODULES) {
        level.setWrappedOutputOptimizations(options);
    }
    if (flags.typedAstOutputFile != null) {
        options.setTypedAstOutputFile(Paths.get(flags.typedAstOutputFile));
    }
    options.setGenerateExports(flags.generateExports);
    options.setExportLocalPropertyDefinitions(flags.exportLocalPropertyDefinitions);
    WarningLevel wLevel = flags.warningLevel;
    wLevel.setOptionsForWarningLevel(options);
    for (FormattingOption formattingOption : flags.formatting) {
        formattingOption.applyToOptions(options);
    }
    options.closurePass = flags.processClosurePrimitives;
    options.angularPass = flags.angularPass;
    options.polymerVersion = flags.polymerVersion;
    try {
        options.polymerExportPolicy = PolymerExportPolicy.valueOf(Ascii.toUpperCase(flags.polymerExportPolicy));
    } catch (IllegalArgumentException ex) {
        throw new FlagUsageException("Unknown PolymerExportPolicy `" + flags.polymerExportPolicy + "' specified.");
    }
    options.setChromePass(flags.chromePass);
    if (!flags.j2clPassMode.isEmpty()) {
        try {
            CompilerOptions.J2clPassMode j2clPassMode = CompilerOptions.J2clPassMode.valueOf(Ascii.toUpperCase(flags.j2clPassMode));
            options.setJ2clPass(j2clPassMode);
        } catch (IllegalArgumentException ex) {
            throw new FlagUsageException("Unknown J2clPassMode `" + flags.j2clPassMode + "' specified.");
        }
    }
    options.removeJ2clAsserts = flags.removeJ2cLAsserts;
    options.renamePrefix = flags.renamePrefix;
    options.renamePrefixNamespace = flags.renamePrefixNamespace;
    options.setPreserveTypeAnnotations(flags.preserveTypeAnnotations);
    options.setPreventLibraryInjection(!flags.injectLibraries);
    if (!flags.forceInjectLibraries.isEmpty()) {
        options.setForceLibraryInjection(flags.forceInjectLibraries);
    }
    options.rewritePolyfills = flags.rewritePolyfills && options.getLanguageIn().toFeatureSet().contains(FeatureSet.ES2015);
    options.setIsolatePolyfills(flags.isolatePolyfills);
    if (!flags.translationsFile.isEmpty()) {
        try {
            options.messageBundle = new XtbMessageBundle(new FileInputStream(flags.translationsFile), flags.translationsProject);
        } catch (IOException e) {
            throw new RuntimeException("Reading XTB file", e);
        }
    } else if (CompilationLevel.ADVANCED_OPTIMIZATIONS == level) {
        // In SIMPLE or WHITESPACE mode, if the user hasn't specified a
        // translations file, they might reasonably try to write their own
        // implementation of goog.getMsg that makes the substitution at
        // run-time.
        // 
        // In ADVANCED mode, goog.getMsg is going to be renamed anyway,
        // so we might as well inline it. But shut off the i18n warnings,
        // because the user didn't really ask for i18n.
        options.messageBundle = new EmptyMessageBundle();
        options.setWarningLevel(DiagnosticGroups.MSG_CONVENTIONS, CheckLevel.OFF);
    }
    options.setConformanceConfigs(loadConformanceConfigs(flags.conformanceConfigs));
    options.setPrintSourceAfterEachPass(flags.printSourceAfterEachPass);
    options.setTracerMode(flags.tracerMode);
    options.setStrictModeInput(flags.strictModeInput);
    options.setEmitUseStrict(flags.emitUseStrict);
    options.setSourceMapIncludeSourcesContent(flags.sourceMapIncludeSourcesContent);
    options.setModuleResolutionMode(flags.moduleResolutionMode);
    options.setBrowserResolverPrefixReplacements(ImmutableMap.copyOf(flags.browserResolverPrefixReplacements));
    if (flags.packageJsonEntryNames != null) {
        try {
            List<String> packageJsonEntryNames = flags.getPackageJsonEntryNames();
            options.setPackageJsonEntryNames(packageJsonEntryNames);
        } catch (CmdLineException e) {
            reportError("ERROR - invalid package_json_entry_names format specified.");
        }
    }
    if (!flags.renaming) {
        options.setVariableRenaming(VariableRenamingPolicy.OFF);
        options.setPropertyRenaming(PropertyRenamingPolicy.OFF);
    }
    if (flags.instrumentCodeParsed == InstrumentOption.PRODUCTION && Strings.isNullOrEmpty(flags.instrumentationMappingOutputFile)) {
        throw new FlagUsageException("Expected --instrument_mapping_report to be set when " + "--instrument_for_coverage_option is set to Production");
    }
    if (!Strings.isNullOrEmpty(flags.instrumentationMappingOutputFile) && flags.instrumentCodeParsed != InstrumentOption.PRODUCTION) {
        throw new FlagUsageException("Expected --instrument_for_coverage_option to be passed with PRODUCTION " + "when --instrument_mapping_report is set");
    }
    if (Strings.isNullOrEmpty(flags.productionInstrumentationArrayName) && flags.instrumentCodeParsed == InstrumentOption.PRODUCTION) {
        throw new FlagUsageException("Expected --production_instrumentation_array_name to be set when " + "--instrument_for_coverage_option is set to Production");
    }
    options.setInstrumentForCoverageOption(flags.instrumentCodeParsed);
    options.setProductionInstrumentationArrayName(flags.productionInstrumentationArrayName);
    options.setAllowDynamicImport(flags.allowDynamicImport);
    options.setDynamicImportAlias(flags.dynamicImportAlias);
    options.setAssumeStaticInheritanceIsNotUsed(flags.assumeStaticInheritanceIsNotUsed);
    if (flags.chunkOutputType == ChunkOutputType.ES_MODULES) {
        if (flags.renamePrefixNamespace != null) {
            throw new FlagUsageException("Expected --rename_prefix_namespace not to be specified when " + "--chunk_output_type is set to ES_MODULES.");
        }
        if (flags.emitUseStrict) {
            throw new FlagUsageException("Expected --emit_use_strict should not be specified when " + "--chunk_output_type is set to ES_MODULES.");
        }
        options.chunkOutputType = flags.chunkOutputType;
        options.setEmitUseStrict(false);
        if (level == CompilationLevel.ADVANCED_OPTIMIZATIONS) {
            options.setExtractPrototypeMemberDeclarations(ExtractPrototypeMemberDeclarationsMode.USE_CHUNK_TEMP);
        }
    }
    return options;
}
Also used : IOException(java.io.IOException) LanguageMode(com.google.javascript.jscomp.CompilerOptions.LanguageMode) FileInputStream(java.io.FileInputStream) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

LanguageMode (com.google.javascript.jscomp.CompilerOptions.LanguageMode)2 CompilationLevel (com.google.javascript.jscomp.CompilationLevel)1 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)1 DependencyOptions (com.google.javascript.jscomp.DependencyOptions)1 ModuleIdentifier (com.google.javascript.jscomp.ModuleIdentifier)1 WarningLevel (com.google.javascript.jscomp.WarningLevel)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 CmdLineException (org.kohsuke.args4j.CmdLineException)1