Package com.google.javascript.jscomp
Class Compiler
java.lang.Object
com.google.javascript.jscomp.AbstractCompiler
com.google.javascript.jscomp.Compiler
- All Implemented Interfaces:
SourceExcerptProvider
Compiler (and the other classes in this package) does the following:
- parses JS code
- checks for undefined variables
- performs optimizations such as constant folding and constants inlining
- renames variables (to short names)
- outputs compact JavaScript code
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classStores a buffer of text to which more can be appended.static classStores the internal compiler state just before optimization is performed.Nested classes/interfaces inherited from interface com.google.javascript.jscomp.SourceExcerptProvider
SourceExcerptProvider.ExcerptFormatter, SourceExcerptProvider.SourceExcerpt -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DiagnosticTypestatic final DiagnosticTypeError strings used for reporting JSErrorsprotected final com.google.javascript.jscomp.CodeChangeHandler -
Constructor Summary
ConstructorsConstructorDescriptionCompiler()Creates a Compiler that reports errors and warnings to its logger.Compiler(ErrorManager errorManager) Creates a Compiler that uses a custom error manager.Compiler(PrintStream stream) Creates a Compiler that reports errors and warnings to an output stream. -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanvoidaddNewScript(JsAst ast) Adds a new Script AST to the compile state.voidcheck()compile(JSSourceFile[] externs, JSModule[] modules, CompilerOptions options) Deprecated.Convert your arrays to lists and use the list-based compileModules method.compile(JSSourceFile[] externs, JSSourceFile[] inputs, CompilerOptions options) Deprecated.Convert your arrays to lists and use the list-based compile method.compile(JSSourceFile extern, JSModule[] modules, CompilerOptions options) Deprecated.Convert your arrays to lists and use the list-based compileModules method.compile(SourceFile extern, JSSourceFile[] input, CompilerOptions options) Deprecated.Convert your arrays to lists and use the list-based API.compile(SourceFile extern, SourceFile input, CompilerOptions options) <T1 extends SourceFile,T2 extends SourceFile>
Resultcompile(List<T1> externs, List<T2> inputs, CompilerOptions options) Compiles a list of inputs.<T extends SourceFile>
ResultcompileModules(List<T> externs, List<JSModule> modules, CompilerOptions options) Compiles a list of modules.voidDisable threads.Gets the DOT graph of the AST generated at the end of compilation.Gets the current coding convention.protected DiagnosticGroupsThe warning classes that are available from the command-line, and are suppressible by the@suppressannotation.intGets the number of errors.getErrorLevel(JSError error) Gets the error manager.JSError[]Returns the array of errors (never null).Looks up an input (possibly an externs input) by input id.Returns an unmodifiable view of the compiler inputs indexed by id.JSError[]Returns an array constructed from errors + temporary warnings.com.google.javascript.rhino.head.ast.AstRootgetOldParseTreeByName(String sourceName) doublestatic StringReturns the compiler date baked into the jar.static StringReturns the compiler version baked into the jar.Returns the result of the compilation.Get an interpreter for type analysis.getRoot()getSourceLine(String sourceName, int lineNumber) Get the line indicated by the line number.getSourceRegion(String sourceName, int lineNumber) Get a region around the indicated line number.getState()Returns the current internal state, excluding the input files and modules.Gets the top scope.com.google.javascript.jscomp.MemoizedScopeCreatorGets a central registry of type information from the compiled JS.intGets the number of warnings.JSError[]Returns the array of warnings (never null).booleanConsults theErrorManagerto see if we've encountered errors that should halt compilation.voidinit(JSSourceFile[] externs, JSModule[] modules, CompilerOptions options) Deprecated.Convert your arrays to lists and use the list-based API.voidinit(JSSourceFile[] externs, JSSourceFile[] inputs, CompilerOptions options) Deprecated.Convert your arrays to lists and use the list-based API.<T1 extends SourceFile,T2 extends SourceFile>
voidinit(List<T1> externs, List<T2> inputs, CompilerOptions options) Initializes the instance state needed for a compile job.<T extends SourceFile>
voidinitModules(List<T> externs, List<JSModule> modules, CompilerOptions options) Initializes the instance state needed for a compile job if the sources are in modules.voidinitOptions(CompilerOptions options) Initialize the compiler options.booleanbooleanprotected CompilerOptionsAllow subclasses to override the default CompileOptions object.newExternInput(String name) voidvoidoptimize()voidparse()parse(SourceFile file) voidReprocesses the current defines over the AST.voidRebuilds the internal list of inputs by iterating over all modules.protected voidWhen the CompilerOptions and its WarningsGuard overlap, reconcile any discrepencies.protected voidRemoves an input file from AST.voidreplaceScript(JsAst ast) Replaces one file in a hot-swap mode.voidReport an error or warning.voidAll passes should call reportCodeChange() when they alter the JS tree structure.voidsetErrorManager(ErrorManager errorManager) Sets the error manager.static voidsetLoggingLevel(Level level) Sets the logging level for the com.google.javascript.jscomp package.voidsetOldParseTree(String sourceName, com.google.javascript.rhino.head.ast.AstRoot oldAst) voidsetPassConfig(PassConfig passes) voidSets the internal state to the capture given.toSource()Converts the main parse tree back to JS code.voidtoSource(Compiler.CodeBuilder cb, int inputSeqNum, Node root) Writes out JS code from a root node.Converts the parse tree for a module back to JS code.String[]Converts the parse tree for each input back to JS code.String[]toSourceArray(JSModule module) Converts the parse tree for each input in a module back to JS code.
-
Field Details
-
tracker
-
OPTIMIZE_LOOP_ERROR
Error strings used for reporting JSErrors -
MOTION_ITERATIONS_ERROR
-
recentChange
protected final com.google.javascript.jscomp.CodeChangeHandler recentChange
-
-
Constructor Details
-
Compiler
public Compiler()Creates a Compiler that reports errors and warnings to its logger. -
Compiler
Creates a Compiler that reports errors and warnings to an output stream. -
Compiler
Creates a Compiler that uses a custom error manager.
-
-
Method Details
-
setErrorManager
Sets the error manager.- Parameters:
errorManager- the error manager, it cannot benull
-
initOptions
Initialize the compiler options. Only necessary if you're not doing a normal compile() job. -
reconcileOptionsWithGuards
protected void reconcileOptionsWithGuards()When the CompilerOptions and its WarningsGuard overlap, reconcile any discrepencies. -
init
@Deprecated public void init(JSSourceFile[] externs, JSSourceFile[] inputs, CompilerOptions options) Deprecated.Convert your arrays to lists and use the list-based API.Initializes the instance state needed for a compile job. -
init
public <T1 extends SourceFile,T2 extends SourceFile> void init(List<T1> externs, List<T2> inputs, CompilerOptions options) Initializes the instance state needed for a compile job. -
init
Deprecated.Convert your arrays to lists and use the list-based API.Initializes the instance state needed for a compile job if the sources are in modules. -
initModules
public <T extends SourceFile> void initModules(List<T> externs, List<JSModule> modules, CompilerOptions options) Initializes the instance state needed for a compile job if the sources are in modules. -
rebuildInputsFromModules
public void rebuildInputsFromModules()Rebuilds the internal list of inputs by iterating over all modules. This is necessary if inputs have been added to or removed from a module after theinit(List, List, CompilerOptions)call. -
compile
-
compile
Deprecated.Convert your arrays to lists and use the list-based API. -
compile
Deprecated.Convert your arrays to lists and use the list-based compileModules method. -
compile
@Deprecated public Result compile(JSSourceFile[] externs, JSSourceFile[] inputs, CompilerOptions options) Deprecated.Convert your arrays to lists and use the list-based compile method.Compiles a list of inputs. -
compile
public <T1 extends SourceFile,T2 extends SourceFile> Result compile(List<T1> externs, List<T2> inputs, CompilerOptions options) Compiles a list of inputs. -
compile
@Deprecated public Result compile(JSSourceFile[] externs, JSModule[] modules, CompilerOptions options) Deprecated.Convert your arrays to lists and use the list-based compileModules method.Compiles a list of modules. -
compileModules
public <T extends SourceFile> Result compileModules(List<T> externs, List<JSModule> modules, CompilerOptions options) Compiles a list of modules. -
disableThreads
public void disableThreads()Disable threads. This is for clients that run on AppEngine and don't have threads. -
parse
public void parse() -
setPassConfig
- Parameters:
passes- The PassConfig to use with this Compiler.- Throws:
NullPointerException- if passes is nullIllegalStateException- if this.passes has already been assigned
-
check
public void check() -
getResult
Returns the result of the compilation. -
getMessages
Returns an array constructed from errors + temporary warnings. -
getErrors
Returns the array of errors (never null). -
getWarnings
Returns the array of warnings (never null). -
getRoot
-
getInput
Description copied from class:AbstractCompilerLooks up an input (possibly an externs input) by input id. May return null.- Specified by:
getInputin classAbstractCompiler
-
removeExternInput
Removes an input file from AST.- Parameters:
id- The id of the input to be removed.
-
newExternInput
-
getTypeRegistry
Description copied from class:AbstractCompilerGets a central registry of type information from the compiled JS.- Specified by:
getTypeRegistryin classAbstractCompiler
-
getTypedScopeCreator
public com.google.javascript.jscomp.MemoizedScopeCreator getTypedScopeCreator() -
buildKnownSymbolTable
-
getTopScope
Description copied from class:AbstractCompilerGets the top scope.- Specified by:
getTopScopein classAbstractCompiler
-
getReverseAbstractInterpreter
Description copied from class:AbstractCompilerGet an interpreter for type analysis.- Specified by:
getReverseAbstractInterpreterin classAbstractCompiler
-
parse
-
newCompilerOptions
Allow subclasses to override the default CompileOptions object. -
toSource
Converts the main parse tree back to JS code. -
toSourceArray
Converts the parse tree for each input back to JS code. -
toSource
Converts the parse tree for a module back to JS code. -
toSourceArray
Converts the parse tree for each input in a module back to JS code. -
toSource
Writes out JS code from a root node. If printing input delimiters, this method will attach a comment to the start of the text indicating which input the output derived from. If there were any preserve annotations within the root's source, they will also be printed in a block comment at the beginning of the output. -
optimize
public void optimize() -
processDefines
public void processDefines()Reprocesses the current defines over the AST. This is used by GwtCompiler to generate N outputs for different targets from the same (checked) AST. For each target, we apply the target-specific defines by callingprocessDefinesand thenoptimizeto optimize the AST specifically for that target. -
normalize
public void normalize() -
reportCodeChange
public void reportCodeChange()All passes should call reportCodeChange() when they alter the JS tree structure. This is verified by CompilerTestCase. This allows us to optimize to a fixed point.- Specified by:
reportCodeChangein classAbstractCompiler
-
getCodingConvention
Description copied from class:AbstractCompilerGets the current coding convention.- Specified by:
getCodingConventionin classAbstractCompiler
-
isIdeMode
public boolean isIdeMode() -
acceptEcmaScript5
public boolean acceptEcmaScript5() -
languageMode
-
acceptConstKeyword
public boolean acceptConstKeyword() -
isTypeCheckingEnabled
public boolean isTypeCheckingEnabled() -
getDiagnosticGroups
The warning classes that are available from the command-line, and are suppressible by the@suppressannotation. -
report
Description copied from class:AbstractCompilerReport an error or warning.- Specified by:
reportin classAbstractCompiler
-
getErrorLevel
-
getErrorCount
public int getErrorCount()Gets the number of errors. -
getWarningCount
public int getWarningCount()Gets the number of warnings. -
hasErrors
public boolean hasErrors()Consults theErrorManagerto see if we've encountered errors that should halt compilation.If
CompilerOptions.ideModeistrue, this function always returnsfalsewithout consulting the error manager. The error manager will continue to be told about new errors and warnings, but the compiler will complete compilation of all inputs. -
getSourceLine
Description copied from interface:SourceExcerptProviderGet the line indicated by the line number. This call will return only the specific line.lineNumber- the line number, 1 being the first line of the file- Returns:
- the line indicated, or
nullif it does not exist
-
getSourceRegion
Description copied from interface:SourceExcerptProviderGet a region around the indicated line number. The exact definition of a region is implementation specific, but it must contain the line indicated by the line number. A region must not start or end by a carriage return.lineNumber- the line number, 1 being the first line of the file- Returns:
- the region around the line number indicated, or
null if it does not exist
-
getSourceMap
-
setLoggingLevel
Sets the logging level for the com.google.javascript.jscomp package. -
getAstDotGraph
Gets the DOT graph of the AST generated at the end of compilation.- Throws:
IOException
-
getErrorManager
Description copied from class:AbstractCompilerGets the error manager.- Specified by:
getErrorManagerin classAbstractCompiler
-
getInputsById
Returns an unmodifiable view of the compiler inputs indexed by id. -
getState
Returns the current internal state, excluding the input files and modules. -
setState
Sets the internal state to the capture given. Note that this assumes that the input files are already set up. -
getProgress
public double getProgress()- Specified by:
getProgressin classAbstractCompiler- Returns:
- a number in [0,1] range indicating an approximate progress of the last compile. Note this should only be used as a hint and no assumptions should be made on accuracy, even a completed compile may choose not to set this to 1.0 at the end.
-
replaceScript
Replaces one file in a hot-swap mode. The given JsAst should be made from a new version of a file that already was present in the last compile call. If the file is new, this will silently ignored.- Parameters:
ast- the ast of the file that is being replaced
-
addNewScript
Adds a new Script AST to the compile state. If a script for the same file already exists the script will not be added, instead a call to #replaceScript should be used.- Parameters:
ast- the ast of the new file
-
getReleaseVersion
Returns the compiler version baked into the jar. -
getReleaseDate
Returns the compiler date baked into the jar. -
setOldParseTree
-
getOldParseTreeByName
-