Skip to content

Commit

Permalink
Split IDE mode setting into a few different settings since it actuall…
Browse files Browse the repository at this point in the history
…y implies a few different things.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=123039034
  • Loading branch information
tbreisacher authored and blickly committed May 24, 2016
1 parent 2324f2a commit 75537d9
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 63 deletions.
Expand Up @@ -1195,7 +1195,7 @@ int processResults(Result result, List<JSModule> modules, B options) throws IOEx
outputBundle(); outputBundle();
outputModuleGraphJson(); outputModuleGraphJson();
return 0; return 0;
} else if (!options.checksOnly && result.success) { } else if (options.outputJs && result.success) {
outputModuleGraphJson(); outputModuleGraphJson();
if (modules == null) { if (modules == null) {
outputSingleBinary(options); outputSingleBinary(options);
Expand Down
5 changes: 0 additions & 5 deletions src/com/google/javascript/jscomp/AbstractCompiler.java
Expand Up @@ -264,11 +264,6 @@ LifeCycleStage getLifeCycleStage() {
/** Passes that do cross-scope modifications use this (eg, InlineVariables) */ /** Passes that do cross-scope modifications use this (eg, InlineVariables) */
abstract void reportChangeToEnclosingScope(Node n); abstract void reportChangeToEnclosingScope(Node n);


/**
* Returns true if compiling in IDE mode.
*/
abstract boolean isIdeMode();

/** /**
* Represents the different contexts for which the compiler could have * Represents the different contexts for which the compiler could have
* distinct configurations. * distinct configurations.
Expand Down
1 change: 1 addition & 0 deletions src/com/google/javascript/jscomp/CommandLineRunner.java
Expand Up @@ -1496,6 +1496,7 @@ protected CompilerOptions createOptions() {
options.setEnvironment(flags.environment); options.setEnvironment(flags.environment);


options.setChecksOnly(flags.checksOnly); options.setChecksOnly(flags.checksOnly);
options.setOutputJs(!flags.checksOnly);


if (flags.useTypesForOptimization) { if (flags.useTypesForOptimization) {
level.setTypeBasedOptimizationOptions(options); level.setTypeBasedOptimizationOptions(options);
Expand Down
17 changes: 6 additions & 11 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -712,8 +712,7 @@ private void compileInternal() {
return; return;
} }


// IDE-mode is defined to stop here, before the heavy rewriting begins. if (!options.checksOnly) {
if (!options.checksOnly && !options.ideMode) {
optimize(); optimize();
} }
} }
Expand Down Expand Up @@ -2112,11 +2111,6 @@ public CodingConvention getCodingConvention() {
return convention; return convention;
} }


@Override
public boolean isIdeMode() {
return options.ideMode;
}

@Override @Override
Config getParserConfig(ConfigContext context) { Config getParserConfig(ConfigContext context) {
if (parserConfig == null) { if (parserConfig == null) {
Expand Down Expand Up @@ -2160,7 +2154,8 @@ Config getParserConfig(ConfigContext context) {


protected Config createConfig(Config.LanguageMode mode) { protected Config createConfig(Config.LanguageMode mode) {
Config config = ParserRunner.createConfig(mode, options.extraAnnotationNames); Config config = ParserRunner.createConfig(mode, options.extraAnnotationNames);
config.setIdeMode(isIdeMode()); config.setPreserveDetailedSourceInfo(options.preservesDetailedSourceInfo());
config.setKeepGoing(options.canKeepGoing());
config.setParseJsDocDocumentation(options.isParseJsDocDocumentation()); config.setParseJsDocDocumentation(options.isParseJsDocDocumentation());
config.setPreserveJsDocWhitespace(options.isPreserveJsDocWhitespace()); config.setPreserveJsDocWhitespace(options.isPreserveJsDocWhitespace());
return config; return config;
Expand Down Expand Up @@ -2236,7 +2231,7 @@ public int getWarningCount() {


@Override @Override
boolean hasHaltingErrors() { boolean hasHaltingErrors() {
return !isIdeMode() && getErrorCount() > 0; return !getOptions().canKeepGoing() && getErrorCount() > 0;
} }


/** /**
Expand Down Expand Up @@ -2671,7 +2666,7 @@ public static String getReleaseDate() {


@Override @Override
void addComments(String filename, List<Comment> comments) { void addComments(String filename, List<Comment> comments) {
if (!isIdeMode()) { if (!getOptions().preservesDetailedSourceInfo()) {
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
"addComments may only be called in IDE mode."); "addComments may only be called in IDE mode.");
} }
Expand All @@ -2680,7 +2675,7 @@ void addComments(String filename, List<Comment> comments) {


@Override @Override
public List<Comment> getComments(String filename) { public List<Comment> getComments(String filename) {
if (!isIdeMode()) { if (!getOptions().preservesDetailedSourceInfo()) {
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
"getComments may only be called in IDE mode."); "getComments may only be called in IDE mode.");
} }
Expand Down
76 changes: 59 additions & 17 deletions src/com/google/javascript/jscomp/CompilerOptions.java
Expand Up @@ -105,26 +105,15 @@ public void setInferConst(boolean value) {
*/ */
private boolean assumeStrictThis; private boolean assumeStrictThis;


/** private boolean allowHotswapReplaceScript = false;
* Configures the compiler for use as an IDE backend. In this mode: private boolean preserveDetailedSourceInfo = false;
* <ul> private boolean keepGoing = false;
* <li>No optimization passes will run.</li>
* <li>The last time custom passes are invoked is
* {@link CustomPassExecutionTime#BEFORE_OPTIMIZATIONS}</li>
* <li>The compiler will always try to process all inputs fully, even
* if it encounters errors.</li>
* <li>The compiler may record more information than is strictly
* needed for codegen.</li>
* </ul>
*/
public boolean ideMode;


private boolean parseJsDocDocumentation = false; private boolean parseJsDocDocumentation = false;
private boolean preserveJsDocWhitespace = false; private boolean preserveJsDocWhitespace = false;


/** /**
* Even if checkTypes is disabled, clients might want to still infer types. * Even if checkTypes is disabled, clients such as IDEs might want to still infer types.
* This is mostly used when ideMode is enabled.
*/ */
boolean inferTypes; boolean inferTypes;


Expand Down Expand Up @@ -730,6 +719,8 @@ public void setAppNameStr(String appNameStr) {


public boolean checksOnly; public boolean checksOnly;


public boolean outputJs;

public boolean generateExports; public boolean generateExports;


// TODO(dimvar): generate-exports should always run after typechecking. // TODO(dimvar): generate-exports should always run after typechecking.
Expand Down Expand Up @@ -1094,6 +1085,7 @@ public CompilerOptions() {
appNameStr = ""; appNameStr = "";
recordFunctionInformation = false; recordFunctionInformation = false;
checksOnly = false; checksOnly = false;
outputJs = true;
generateExports = false; generateExports = false;
generateExportsAfterTypeChecking = true; generateExportsAfterTypeChecking = true;
exportLocalPropertyDefinitions = false; exportLocalPropertyDefinitions = false;
Expand Down Expand Up @@ -1513,6 +1505,10 @@ public void setChecksOnly(boolean checksOnly) {
this.checksOnly = checksOnly; this.checksOnly = checksOnly;
} }


public void setOutputJs(boolean outputJs) {
this.outputJs = outputJs;
}

public void setGenerateExports(boolean generateExports) { public void setGenerateExports(boolean generateExports) {
this.generateExports = generateExports; this.generateExports = generateExports;
} }
Expand Down Expand Up @@ -1808,8 +1804,54 @@ public void setPropertyInvalidationErrors(
ImmutableMap.copyOf(propertyInvalidationErrors); ImmutableMap.copyOf(propertyInvalidationErrors);
} }


/**
* Configures the compiler for use as an IDE backend. In this mode:
* <ul>
* <li>No optimization passes will run.</li>
* <li>The last time custom passes are invoked is
* {@link CustomPassExecutionTime#BEFORE_OPTIMIZATIONS}</li>
* <li>The compiler will always try to process all inputs fully, even
* if it encounters errors.</li>
* <li>The compiler may record more information than is strictly
* needed for codegen.</li>
* </ul>
*
* @deprecated Some "IDE" clients will need some of these options but not
* others. Consider calling setChecksOnly, setAllowRecompilation, etc,
* explicitly, instead of calling this method which does a variety of
* different things.
*/
@Deprecated
public void setIdeMode(boolean ideMode) { public void setIdeMode(boolean ideMode) {
this.ideMode = ideMode; setChecksOnly(ideMode);
setKeepGoing(ideMode);
setAllowHotswapReplaceScript(ideMode);
setPreserveDetailedSourceInfo(ideMode);
setParseJsDocDocumentation(ideMode);
}

void setAllowHotswapReplaceScript(boolean allowRecompilation) {
this.allowHotswapReplaceScript = allowRecompilation;
}

boolean allowsHotswapReplaceScript() {
return allowHotswapReplaceScript;
}

public void setPreserveDetailedSourceInfo(boolean preserveDetailedSourceInfo) {
this.preserveDetailedSourceInfo = preserveDetailedSourceInfo;
}

boolean preservesDetailedSourceInfo() {
return preserveDetailedSourceInfo;
}

void setKeepGoing(boolean keepGoing) {
this.keepGoing = keepGoing;
}

boolean canKeepGoing() {
return keepGoing;
} }


/** /**
Expand All @@ -1829,7 +1871,7 @@ public void setParseJsDocDocumentation(boolean parseJsDocDocumentation) {
* @return True when JSDoc documentation will be parsed, false if not. * @return True when JSDoc documentation will be parsed, false if not.
*/ */
public boolean isParseJsDocDocumentation() { public boolean isParseJsDocDocumentation() {
return this.ideMode || this.parseJsDocDocumentation; return this.parseJsDocDocumentation;
} }


/** /**
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/ControlFlowAnalysis.java
Expand Up @@ -588,7 +588,7 @@ private void handleBreak(Node node) {
lastJump = cur; lastJump = cur;
} }
if (parent == null) { if (parent == null) {
if (compiler.isIdeMode()) { if (compiler.getOptions().canKeepGoing()) {
// In IDE mode, we expect that the data flow graph may // In IDE mode, we expect that the data flow graph may
// not be well-formed. // not be well-formed.
return; return;
Expand Down
8 changes: 4 additions & 4 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -156,7 +156,7 @@ public DefaultPassConfig(CompilerOptions options) {
// wrap them in a function call that is stripped later, this shouldn't // wrap them in a function call that is stripped later, this shouldn't
// be done in IDE mode where AST changes may be unexpected. // be done in IDE mode where AST changes may be unexpected.
protectHiddenSideEffects = options != null && protectHiddenSideEffects = options != null &&
options.protectHiddenSideEffects && !options.ideMode; options.protectHiddenSideEffects && !options.allowsHotswapReplaceScript();
} }


@Override @Override
Expand All @@ -178,7 +178,7 @@ PreprocessorSymbolTable getPreprocessorSymbolTable() {
} }


void maybeInitializePreprocessorSymbolTable(AbstractCompiler compiler) { void maybeInitializePreprocessorSymbolTable(AbstractCompiler compiler) {
if (options.ideMode) { if (options.preservesDetailedSourceInfo()) {
Node root = compiler.getRoot(); Node root = compiler.getRoot();
if (preprocessorSymbolTable == null || if (preprocessorSymbolTable == null ||
preprocessorSymbolTable.getRootNode() != root) { preprocessorSymbolTable.getRootNode() != root) {
Expand All @@ -188,7 +188,7 @@ void maybeInitializePreprocessorSymbolTable(AbstractCompiler compiler) {
} }


void maybeInitializeModuleRewriteState() { void maybeInitializeModuleRewriteState() {
if (options.ideMode && this.moduleRewriteState == null) { if (options.allowsHotswapReplaceScript() && this.moduleRewriteState == null) {
this.moduleRewriteState = new ClosureRewriteModule.GlobalRewriteState(); this.moduleRewriteState = new ClosureRewriteModule.GlobalRewriteState();
} }
} }
Expand Down Expand Up @@ -370,7 +370,7 @@ protected List<PassFactory> getChecks() {


// We assume that only IDE-mode clients will try to query the // We assume that only IDE-mode clients will try to query the
// typed scope creator after the compile job. // typed scope creator after the compile job.
if (!options.ideMode) { if (!options.preservesDetailedSourceInfo()) {
checks.add(clearTypedScopePass); checks.add(clearTypedScopePass);
} }
} }
Expand Down
5 changes: 3 additions & 2 deletions src/com/google/javascript/jscomp/JsAst.java
Expand Up @@ -150,7 +150,7 @@ private void parse(AbstractCompiler compiler) {
root = result.ast; root = result.ast;
features = result.features; features = result.features;


if (compiler.isIdeMode()) { if (compiler.getOptions().preservesDetailedSourceInfo()) {
compiler.addComments(sourceFile.getName(), result.comments); compiler.addComments(sourceFile.getName(), result.comments);
} }
} catch (IOException e) { } catch (IOException e) {
Expand All @@ -165,7 +165,8 @@ private void parse(AbstractCompiler compiler) {
// Note: we use the ErrorManager here rather than the ErrorReporter as // Note: we use the ErrorManager here rather than the ErrorReporter as
// we don't want to fail if the error was excluded by a warning guard, conversely // we don't want to fail if the error was excluded by a warning guard, conversely
// we do want to fail if a warning was promoted to an error. // we do want to fail if a warning was promoted to an error.
|| (errorManager.getErrorCount() > startErrorCount && !compiler.isIdeMode())) { || (errorManager.getErrorCount() > startErrorCount
&& !compiler.getOptions().canKeepGoing())) {
// There was a parse error or IOException, so use a dummy block. // There was a parse error or IOException, so use a dummy block.




Expand Down
6 changes: 5 additions & 1 deletion src/com/google/javascript/jscomp/Linter.java
Expand Up @@ -62,7 +62,11 @@ private static void lint(Path path, boolean fix) throws IOException {
// in LintPassConfig can all handle untranspiled ES6. // in LintPassConfig can all handle untranspiled ES6.
options.setSkipTranspilationAndCrash(true); options.setSkipTranspilationAndCrash(true);


options.setIdeMode(true); options.setPreserveDetailedSourceInfo(true);

// TODO(tbreisacher): Remove this so that people don't miss parse errors.
options.setKeepGoing(true);

options.setCodingConvention(new GoogleCodingConvention()); options.setCodingConvention(new GoogleCodingConvention());


// Even though we're not running the typechecker, enable the checkTypes DiagnosticGroup, since // Even though we're not running the typechecker, enable the checkTypes DiagnosticGroup, since
Expand Down
17 changes: 13 additions & 4 deletions src/com/google/javascript/jscomp/parsing/Config.java
Expand Up @@ -50,9 +50,14 @@ public enum LanguageMode {
boolean preserveJsDocWhitespace; boolean preserveJsDocWhitespace;


/** /**
* Whether we're in IDE mode. * Whether to keep detailed source location information such as the exact length of every node.
*/ */
boolean isIdeMode; boolean preserveDetailedSourceInfo;

/**
* Whether to keep going after encountering a parse error.
*/
boolean keepGoing;


/** /**
* Recognized JSDoc annotations, mapped from their name to their internal * Recognized JSDoc annotations, mapped from their name to their internal
Expand Down Expand Up @@ -96,8 +101,12 @@ private static Map<String, Annotation> buildAnnotationNames(
return annotationBuilder.build(); return annotationBuilder.build();
} }


public void setIdeMode(boolean isIdeMode) { public void setPreserveDetailedSourceInfo(boolean preserveDetailedSourceInfo) {
this.isIdeMode = isIdeMode; this.preserveDetailedSourceInfo = preserveDetailedSourceInfo;
}

public void setKeepGoing(boolean keepGoing) {
this.keepGoing = keepGoing;
} }


public void setParseJsDocDocumentation(boolean parseJsDocDocumentation) { public void setParseJsDocDocumentation(boolean parseJsDocDocumentation) {
Expand Down
8 changes: 4 additions & 4 deletions src/com/google/javascript/jscomp/parsing/IRFactory.java
Expand Up @@ -931,13 +931,13 @@ private JSDocInfo parseInlineTypeDoc(Comment node) {
// Set the length on the node if we're in IDE mode. // Set the length on the node if we're in IDE mode.
void maybeSetLength( void maybeSetLength(
Node node, SourcePosition start, SourcePosition end) { Node node, SourcePosition start, SourcePosition end) {
if (config.isIdeMode) { if (config.preserveDetailedSourceInfo) {
node.setLength(end.offset - start.offset); node.setLength(end.offset - start.offset);
} }
} }


void maybeSetLengthFrom(Node node, Node ref) { void maybeSetLengthFrom(Node node, Node ref) {
if (config.isIdeMode) { if (config.preserveDetailedSourceInfo) {
node.setLength(ref.getLength()); node.setLength(ref.getLength());
} }
} }
Expand Down Expand Up @@ -1253,9 +1253,9 @@ Node processFunction(FunctionDeclarationTree functionTree) {


Node bodyNode = transform(functionTree.functionBody); Node bodyNode = transform(functionTree.functionBody);
if (!isArrow && !isSignature && !bodyNode.isBlock()) { if (!isArrow && !isSignature && !bodyNode.isBlock()) {
// When in ideMode the parser tries to parse some constructs the // When in "keep going" mode the parser tries to parse some constructs the
// compiler doesn't support, repair it here. // compiler doesn't support, repair it here.
Preconditions.checkState(config.isIdeMode); Preconditions.checkState(config.keepGoing);
bodyNode = IR.block(); bodyNode = IR.block();
} }
parseDirectives(bodyNode); parseDirectives(bodyNode);
Expand Down
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/parsing/ParserRunner.java
Expand Up @@ -92,7 +92,7 @@ public static ParseResult parse(
// TODO(johnlenz): unify "SourceFile", "Es6ErrorReporter" and "Config" // TODO(johnlenz): unify "SourceFile", "Es6ErrorReporter" and "Config"
SourceFile file = new SourceFile(sourceFile.getName(), sourceString); SourceFile file = new SourceFile(sourceFile.getName(), sourceString);
Es6ErrorReporter es6ErrorReporter = Es6ErrorReporter es6ErrorReporter =
new Es6ErrorReporter(errorReporter, config.isIdeMode); new Es6ErrorReporter(errorReporter, config.keepGoing);
com.google.javascript.jscomp.parsing.parser.Parser.Config es6config = com.google.javascript.jscomp.parsing.parser.Parser.Config es6config =
new com.google.javascript.jscomp.parsing.parser.Parser.Config(mode( new com.google.javascript.jscomp.parsing.parser.Parser.Config(mode(
config.languageMode)); config.languageMode));
Expand All @@ -101,15 +101,15 @@ public static ParseResult parse(
Node root = null; Node root = null;
List<Comment> comments = ImmutableList.of(); List<Comment> comments = ImmutableList.of();
FeatureSet features = p.getFeatures(); FeatureSet features = p.getFeatures();
if (tree != null && (!es6ErrorReporter.hadError() || config.isIdeMode)) { if (tree != null && (!es6ErrorReporter.hadError() || config.preserveDetailedSourceInfo)) {
IRFactory factory = IRFactory factory =
IRFactory.transformTree(tree, sourceFile, sourceString, config, errorReporter); IRFactory.transformTree(tree, sourceFile, sourceString, config, errorReporter);
root = factory.getResultNode(); root = factory.getResultNode();
features = features.require(factory.getFeatures()); features = features.require(factory.getFeatures());
root.setIsSyntheticBlock(true); root.setIsSyntheticBlock(true);
root.putProp(Node.FEATURE_SET, features); root.putProp(Node.FEATURE_SET, features);


if (config.isIdeMode) { if (config.preserveDetailedSourceInfo) {
comments = p.getComments(); comments = p.getComments();
} }
} }
Expand Down
Expand Up @@ -250,7 +250,6 @@ private boolean checkParameterCount(Node expr, Keywords keyword) {
public boolean parseTypeTransformation() { public boolean parseTypeTransformation() {
Config config = Config config =
new Config(new HashSet<String>(), new HashSet<String>(), LanguageMode.ECMASCRIPT6); new Config(new HashSet<String>(), new HashSet<String>(), LanguageMode.ECMASCRIPT6);
config.setIdeMode(true);
// TODO(lpino): ParserRunner reports errors if the expression is not // TODO(lpino): ParserRunner reports errors if the expression is not
// ES6 valid. We need to abort the validation of the type transformation // ES6 valid. We need to abort the validation of the type transformation
// whenever an error is reported. // whenever an error is reported.
Expand Down
3 changes: 1 addition & 2 deletions test/com/google/debugging/sourcemap/SourceMapTestCase.java
Expand Up @@ -283,8 +283,7 @@ protected RunResult compile(
Compiler compiler = new Compiler(); Compiler compiler = new Compiler();
CompilerOptions options = getCompilerOptions(); CompilerOptions options = getCompilerOptions();


// Turn on IDE mode to get rid of optimizations. options.setChecksOnly(true);
options.ideMode = true;


List<SourceFile> inputs = List<SourceFile> inputs =
ImmutableList.of(SourceFile.fromCode(fileName1, js1)); ImmutableList.of(SourceFile.fromCode(fileName1, js1));
Expand Down

0 comments on commit 75537d9

Please sign in to comment.