public class Compiler extends AbstractCompiler implements ErrorHandler, SourceFileMapping
Modifier and Type | Class and Description |
---|---|
static class |
Compiler.CodeBuilder
Stores a buffer of text to which more can be appended.
|
static class |
Compiler.ExternalSourceLoader
Subclasses are responsible for loading sources that were not provided as explicit inputs to the
compiler.
|
SourceExcerptProvider.ExcerptFormatter, SourceExcerptProvider.SourceExcerpt
Modifier and Type | Field and Description |
---|---|
static java.util.logging.Logger |
logger
Logger for the whole com.google.javascript.jscomp domain -
setting configuration for this logger affects all loggers
in other classes within the compiler.
|
static DiagnosticType |
MOTION_ITERATIONS_ERROR |
static DiagnosticType |
OPTIMIZE_LOOP_ERROR
Error strings used for reporting JSErrors
|
protected com.google.javascript.jscomp.RecentChange |
recentChange |
PerformanceTracker |
tracker |
annotationMap
Constructor and Description |
---|
Compiler()
Creates a Compiler that reports errors and warnings to its logger.
|
Compiler(ErrorManager errorManager)
Creates a Compiler that uses a custom error manager.
|
Compiler(java.io.PrintStream outStream)
Creates a Compiler that reports errors and warnings to an output stream.
|
Modifier and Type | Method and Description |
---|---|
void |
addExportedNames(java.util.Set<java.lang.String> exportedNames)
Adds exported names to keep track.
|
void |
addInputSourceMap(java.lang.String sourceFileName,
SourceMapInput inputSourceMap)
Adds a
SourceMapInput for the given sourceFileName , to be used for error
reporting and source map combining. |
void |
addNewScript(JsAst ast)
Adds a new Script AST to the compile state.
|
SymbolTable |
buildKnownSymbolTable() |
void |
clearJSTypeRegistry() |
<T1 extends SourceFile,T2 extends SourceFile> |
compile(java.util.List<T1> externs,
java.util.List<T2> inputs,
CompilerOptions options)
Compiles a list of inputs.
|
Result |
compile(SourceFile extern,
SourceFile input,
CompilerOptions options)
Compiles a single source file and a single externs file.
|
<T extends SourceFile> |
compileModules(java.util.List<T> externs,
java.util.List<JSModule> modules,
CompilerOptions options)
Compiles a list of modules.
|
protected com.google.javascript.jscomp.CompilerExecutor |
createCompilerExecutor() |
protected Config |
createConfig(Config.LanguageMode mode,
Config.StrictMode strictMode) |
void |
disableThreads()
Disable threads.
|
void |
generateReport()
Generates a report of all warnings and errors found during compilation to stderr.
|
java.lang.String |
getAstDotGraph()
Gets the DOT graph of the AST generated at the end of compilation.
|
int |
getChangeStamp()
A monotonically increasing value to identify a change
|
CodingConvention |
getCodingConvention()
Gets the current coding convention.
|
java.util.List<Comment> |
getComments(java.lang.String filename)
Returns all the comments from the given file.
|
protected com.google.javascript.jscomp.CompilerExecutor |
getCompilerExecutor() |
com.google.javascript.jscomp.IdGenerator |
getCrossModuleIdGenerator()
Gets the id generator for cross-module motion.
|
DiagnosticGroups |
getDiagnosticGroups()
The warning classes that are available from the command-line, and are suppressible by the
@suppress annotation. |
int |
getErrorCount()
Gets the number of errors.
|
CheckLevel |
getErrorLevel(JSError error) |
ErrorManager |
getErrorManager()
Gets the error manager.
|
com.google.common.collect.ImmutableList<JSError> |
getErrors()
Returns the list of errors (never null).
|
java.util.Set<java.lang.String> |
getExportedNames()
Gets the names that have been exported.
|
CompilerInput |
getInput(InputId id)
Looks up an input (possibly an externs input) by input id.
|
java.util.Map<InputId,CompilerInput> |
getInputsById()
Returns an unmodifiable view of the compiler inputs indexed by id.
|
ModuleLoader |
getModuleLoader()
Gets the module loader.
|
ModuleMap |
getModuleMap() |
ModuleMetadataMap |
getModuleMetadataMap() |
java.lang.Iterable<JSModule> |
getModules()
Gets the JS source modules in dependency order.
|
CompilerOptions |
getOptions() |
double |
getProgress() |
static java.lang.String |
getReleaseDate()
Returns the compiler date baked into the jar.
|
static java.lang.String |
getReleaseVersion()
Returns the compiler version baked into the jar.
|
Result |
getResult()
Returns the result of the compilation.
|
ReverseAbstractInterpreter |
getReverseAbstractInterpreter()
Get an interpreter for type analysis.
|
Node |
getRoot()
Returns the root node of the AST, which includes both externs and source.
|
java.lang.CharSequence |
getSourceFileContentByName(java.lang.String sourceName) |
java.lang.String |
getSourceLine(java.lang.String sourceName,
int lineNumber)
Get the line indicated by the line number.
|
SourceMap |
getSourceMap() |
com.google.debugging.sourcemap.proto.Mapping.OriginalMapping |
getSourceMapping(java.lang.String sourceName,
int lineNumber,
int columnNumber)
Returns the original mapping for the file name, line number and column position found
in the source map.
|
Region |
getSourceRegion(java.lang.String sourceName,
int lineNumber)
Get a region around the indicated line number.
|
TypedScope |
getTopScope()
Gets the top scope.
|
com.google.javascript.jscomp.TypedScopeCreator |
getTypedScopeCreator()
Gets a memoized scope creator with type information.
|
JSTypeRegistry |
getTypeRegistry()
Gets a central registry of type information from the compiled JS.
|
int |
getWarningCount()
Gets the number of warnings.
|
com.google.common.collect.ImmutableList<JSError> |
getWarnings()
Returns the list of warnings (never null).
|
boolean |
hasErrors()
Consults the
ErrorManager to see if we've encountered errors
that should halt compilation. |
void |
incrementChangeStamp()
Called to indicate that the current change stamp has been used
|
<T1 extends SourceFile,T2 extends SourceFile> |
init(java.util.List<T1> externs,
java.util.List<T2> sources,
CompilerOptions options)
Initializes the instance state needed for a compile job.
|
void |
initBasedOnOptions()
Do any initialization that is dependent on the compiler options.
|
<T extends SourceFile> |
initModules(java.util.List<T> externs,
java.util.List<JSModule> modules,
CompilerOptions options)
Initializes the instance state needed for a compile job if the sources
are in modules.
|
void |
initOptions(CompilerOptions options)
Initializes the compiler options.
|
void |
initWebpackMap(com.google.common.collect.ImmutableMap<java.lang.String,java.lang.String> inputPathByWebpackId) |
void |
instrumentForCoverage()
Instrument code for coverage.
|
static java.lang.String |
joinPathParts(java.lang.String... pathParts)
Creates an OS specific path string from parts
|
void |
maybeSetTracker() |
protected CompilerOptions |
newCompilerOptions()
Allow subclasses to override the default CompileOptions object.
|
void |
parse()
Parses input files without doing progress tracking that is part of a full compile.
|
Node |
parse(SourceFile file) |
void |
parseForCompilation()
Parses input files in preparation for compilation.
|
void |
performPostCompilationTasks()
Performs all the bookkeeping required at the end of a compilation.
|
void |
printConfig(java.io.PrintStream printStream) |
void |
rebuildInputsFromModules()
Rebuilds the internal input map by iterating over all modules.
|
protected void |
reconcileOptionsWithGuards()
When the CompilerOptions and its WarningsGuard overlap, reconcile any discrepancies.
|
protected void |
removeExternInput(InputId id)
Removes an input file from AST.
|
void |
replaceScript(JsAst ast)
Replaces one file in a hot-swap mode.
|
void |
report(CheckLevel ignoredLevel,
JSError error) |
void |
report(JSError error)
Report an error or warning.
|
void |
reportChangeToChangeScope(Node changeScopeRoot)
Mark modifications in a scope that is different than the Compiler.currentScope use this (eg,
InlineVariables and many others)
|
void |
reportChangeToEnclosingScope(Node n)
Passes that make modifications in a scope that is different than the Compiler.currentScope use
this (eg, InlineVariables and many others)
|
void |
reportFunctionDeleted(Node n)
Mark a specific function node as known to be deleted.
|
void |
resetAndIntitializeSourceMap() |
void |
restoreState(java.io.InputStream inputStream) |
void |
saveState(java.io.OutputStream outputStream) |
void |
setAnonymousFunctionNameMap(VariableMap functionMap)
Sets the naming map for anonymous functions
|
void |
setCssNames(java.util.Map<java.lang.String,java.lang.Integer> cssNames)
Sets the css names found during compilation.
|
void |
setErrorManager(ErrorManager errorManager)
Sets the error manager.
|
void |
setIdGeneratorMap(java.lang.String serializedIdMappings)
Sets the id generator for cross-module motion.
|
static void |
setLoggingLevel(java.util.logging.Level level)
Sets the logging level for the com.google.javascript.jscomp package.
|
void |
setModuleMap(ModuleMap moduleMap) |
void |
setModuleMetadataMap(ModuleMetadataMap moduleMetadataMap) |
void |
setPassConfig(PassConfig passes) |
void |
setPropertyMap(VariableMap propertyMap)
Sets the property renaming map
|
void |
setStringMap(VariableMap stringMap)
Sets the string replacement map
|
void |
setTimeout(int timeout)
Sets the timeout when Compiler is run in a thread
|
void |
setVariableMap(VariableMap variableMap)
Sets the variable renaming map
|
void |
stage1Passes()
Perform compiler passes for stage 1 of compilation.
|
void |
stage2Passes()
Perform compiler passes for stage 2 of compilation.
|
java.lang.String |
toSource()
Converts the main parse tree back to JS code.
|
void |
toSource(Compiler.CodeBuilder cb,
int inputSeqNum,
Node root)
Writes out JS code from a root node.
|
java.lang.String |
toSource(JSModule module)
Converts the parse tree for a module back to JS code.
|
java.lang.String |
toSource(Node n)
Generates JavaScript source code for an AST, doesn't generate source
map info.
|
java.lang.String[] |
toSourceArray()
Converts the parse tree for each input back to JS code.
|
java.lang.String[] |
toSourceArray(JSModule module)
Converts the parse tree for each input in a module back to JS code.
|
void |
transpileAndDontCheck() |
void |
whitespaceOnlyPasses() |
createAstFactory, createOrReopenLog, getAstAnalyzer, isFillFileName
public PerformanceTracker tracker
public static final DiagnosticType OPTIMIZE_LOOP_ERROR
public static final DiagnosticType MOTION_ITERATIONS_ERROR
public static final java.util.logging.Logger logger
protected final com.google.javascript.jscomp.RecentChange recentChange
public Compiler()
public Compiler(java.io.PrintStream outStream)
public Compiler(ErrorManager errorManager)
public void setErrorManager(ErrorManager errorManager)
errorManager
- the error manager, it cannot be null
public void initOptions(CompilerOptions options)
public void printConfig(java.io.PrintStream printStream)
protected void reconcileOptionsWithGuards()
public final <T1 extends SourceFile,T2 extends SourceFile> void init(java.util.List<T1> externs, java.util.List<T2> sources, CompilerOptions options)
public <T extends SourceFile> void initModules(java.util.List<T> externs, java.util.List<JSModule> modules, CompilerOptions options)
public void initBasedOnOptions()
public static java.lang.String joinPathParts(java.lang.String... pathParts)
public void rebuildInputsFromModules()
init(java.util.List<T1>, java.util.List<T2>, com.google.javascript.jscomp.CompilerOptions)
or initModules(java.util.List<T>, java.util.List<com.google.javascript.jscomp.JSModule>, com.google.javascript.jscomp.CompilerOptions)
call.public Result compile(SourceFile extern, SourceFile input, CompilerOptions options)
public <T1 extends SourceFile,T2 extends SourceFile> Result compile(java.util.List<T1> externs, java.util.List<T2> inputs, CompilerOptions options)
This is a convenience method to wrap up all the work of compilation, including generating the error and warning report.
NOTE: All methods called here must be public, because client code must be able to replicate and customize this.
public void generateReport()
Client code must call this method explicitly if it doesn't use one of the convenience methods that do so automatically.
Always call this method, even if the compiler throws an exception. The report will include information about the exception.
public <T extends SourceFile> Result compileModules(java.util.List<T> externs, java.util.List<JSModule> modules, CompilerOptions options)
This is a convenience method to wrap up all the work of compilation, including generating the error and warning report.
NOTE: All methods called here must be public, because client code must be able to replicate and customize this.
public void stage1Passes()
Stage 1 consists primarily of error and type checking passes.
parseForCompilation()
must be called before this method is called.
The caller is responsible for also calling generateReport()
to generate a report of
warnings and errors to stderr. See the invocation in compile(com.google.javascript.jscomp.SourceFile, com.google.javascript.jscomp.SourceFile, com.google.javascript.jscomp.CompilerOptions)
for a good example.
public void stage2Passes()
Stage 2 consists primarily of optimization passes.
stage1Passes()
must be called before this method is called.
The caller is responsible for also calling generateReport()
to generate a report of
warnings and errors to stderr. See the invocation in compile(com.google.javascript.jscomp.SourceFile, com.google.javascript.jscomp.SourceFile, com.google.javascript.jscomp.CompilerOptions)
for a good example.
public void disableThreads()
public void setTimeout(int timeout)
timeout
- seconds to wait before timeoutpublic void performPostCompilationTasks()
This method must be called if the compilation makes it as far as doing checks.
DON'T call it if the compiler threw an exception.
DO call it even when hasErrors()
returns true.
public void instrumentForCoverage()
parseForCompilation()
must be called before this method is called.
The caller is responsible for also calling generateReport()
to generate a report of
warnings and errors to stderr. See the invocation in compile(com.google.javascript.jscomp.SourceFile, com.google.javascript.jscomp.SourceFile, com.google.javascript.jscomp.CompilerOptions)
for a good example.
This method is mutually exclusive with stage1Passes() and stage2Passes(). Either call those two methods or this one, but not both.
public void parseForCompilation()
Either init()
or initModules()
must be called first to set up the input
files to be read.
TODO(bradfordcsmith): Rename this to parse()
public void parse()
Either init()
or initModules()
must be called first to set up the input
files to be read.
TODO(bradfordcsmith): Rename this to parseIndependentOfCompilation() or similar.
public Node parse(SourceFile file)
public void setPassConfig(PassConfig passes)
passes
- The PassConfig to use with this Compiler.java.lang.NullPointerException
- if passes is nulljava.lang.IllegalStateException
- if this.passes has already been assignedpublic void whitespaceOnlyPasses()
public void transpileAndDontCheck()
public Result getResult()
public com.google.common.collect.ImmutableList<JSError> getErrors()
public com.google.common.collect.ImmutableList<JSError> getWarnings()
public Node getRoot()
AbstractCompiler
getRoot
in class AbstractCompiler
public CompilerInput getInput(InputId id)
AbstractCompiler
getInput
in interface CompilerInputProvider
getInput
in class AbstractCompiler
protected void removeExternInput(InputId id)
id
- The id of the input to be removed.@Nullable public java.lang.Iterable<JSModule> getModules()
Returns null if #init
or #initModules
hasn't been called yet. Otherwise, the
result is always non-empty, even in the degenerate case where there's only one module.
public void clearJSTypeRegistry()
clearJSTypeRegistry
in class AbstractCompiler
public JSTypeRegistry getTypeRegistry()
AbstractCompiler
getTypeRegistry
in class AbstractCompiler
public com.google.javascript.jscomp.TypedScopeCreator getTypedScopeCreator()
AbstractCompiler
public SymbolTable buildKnownSymbolTable()
public TypedScope getTopScope()
AbstractCompiler
getTopScope
in class AbstractCompiler
public ReverseAbstractInterpreter getReverseAbstractInterpreter()
AbstractCompiler
getReverseAbstractInterpreter
in class AbstractCompiler
public void maybeSetTracker()
protected CompilerOptions newCompilerOptions()
public java.lang.String toSource()
toSource
in class AbstractCompiler
public java.lang.String toSource(JSModule module)
public void toSource(Compiler.CodeBuilder cb, int inputSeqNum, Node root)
public java.lang.String toSource(Node n)
toSource
in class AbstractCompiler
public java.lang.String[] toSourceArray()
public java.lang.String[] toSourceArray(JSModule module)
public int getChangeStamp()
AbstractCompiler
public void incrementChangeStamp()
AbstractCompiler
public void reportChangeToChangeScope(Node changeScopeRoot)
AbstractCompiler
reportChangeToChangeScope
in class AbstractCompiler
public void reportFunctionDeleted(Node n)
AbstractCompiler
public void reportChangeToEnclosingScope(Node n)
AbstractCompiler
reportChangeToEnclosingScope
in class AbstractCompiler
public CodingConvention getCodingConvention()
AbstractCompiler
getCodingConvention
in class AbstractCompiler
protected Config createConfig(Config.LanguageMode mode, Config.StrictMode strictMode)
public DiagnosticGroups getDiagnosticGroups()
@suppress
annotation.public void report(JSError error)
AbstractCompiler
report
in class AbstractCompiler
public void report(CheckLevel ignoredLevel, JSError error)
report
in interface ErrorHandler
ignoredLevel
- the reporting levelerror
- the error to reportpublic CheckLevel getErrorLevel(JSError error)
public int getErrorCount()
public int getWarningCount()
public boolean hasErrors()
ErrorManager
to see if we've encountered errors
that should halt compilation.
If CompilerOptions.canContinueAfterErrors()
is true
, this function
always returns false
without 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.
public java.lang.CharSequence getSourceFileContentByName(java.lang.String sourceName)
public void addInputSourceMap(java.lang.String sourceFileName, SourceMapInput inputSourceMap)
AbstractCompiler
SourceMapInput
for the given sourceFileName
, to be used for error
reporting and source map combining.addInputSourceMap
in class AbstractCompiler
@Nullable public com.google.debugging.sourcemap.proto.Mapping.OriginalMapping getSourceMapping(java.lang.String sourceName, int lineNumber, int columnNumber)
SourceFileMapping
null
if none is found.getSourceMapping
in interface SourceExcerptProvider
getSourceMapping
in interface SourceFileMapping
lineNumber
- The line number, 1-based.columnNumber
- The column index, 1-based.public java.lang.String getSourceLine(java.lang.String sourceName, int lineNumber)
SourceExcerptProvider
getSourceLine
in interface SourceExcerptProvider
lineNumber
- the line number, 1 being the first line of the filenull
if it does not existpublic Region getSourceRegion(java.lang.String sourceName, int lineNumber)
SourceExcerptProvider
getSourceRegion
in interface SourceExcerptProvider
lineNumber
- the line number, 1 being the first line of the filenull
if it does not existpublic SourceMap getSourceMap()
public void setVariableMap(VariableMap variableMap)
AbstractCompiler
setVariableMap
in class AbstractCompiler
public void setPropertyMap(VariableMap propertyMap)
AbstractCompiler
setPropertyMap
in class AbstractCompiler
public void setStringMap(VariableMap stringMap)
AbstractCompiler
setStringMap
in class AbstractCompiler
public void setCssNames(java.util.Map<java.lang.String,java.lang.Integer> cssNames)
AbstractCompiler
setCssNames
in class AbstractCompiler
public void setIdGeneratorMap(java.lang.String serializedIdMappings)
AbstractCompiler
setIdGeneratorMap
in class AbstractCompiler
public com.google.javascript.jscomp.IdGenerator getCrossModuleIdGenerator()
AbstractCompiler
getCrossModuleIdGenerator
in class AbstractCompiler
public void setAnonymousFunctionNameMap(VariableMap functionMap)
AbstractCompiler
setAnonymousFunctionNameMap
in class AbstractCompiler
public void addExportedNames(java.util.Set<java.lang.String> exportedNames)
AbstractCompiler
addExportedNames
in class AbstractCompiler
public java.util.Set<java.lang.String> getExportedNames()
AbstractCompiler
getExportedNames
in class AbstractCompiler
public CompilerOptions getOptions()
public static void setLoggingLevel(java.util.logging.Level level)
public java.lang.String getAstDotGraph() throws java.io.IOException
java.io.IOException
public ErrorManager getErrorManager()
AbstractCompiler
getErrorManager
in class AbstractCompiler
public java.util.Map<InputId,CompilerInput> getInputsById()
public double getProgress()
getProgress
in class AbstractCompiler
public void replaceScript(JsAst ast)
ast
- the ast of the file that is being replacedpublic void addNewScript(JsAst ast)
ast
- the ast of the new file@GwtIncompatible(value="java.util.ResourceBundle") public static java.lang.String getReleaseVersion()
@GwtIncompatible(value="java.util.ResourceBundle") public static java.lang.String getReleaseDate()
public java.util.List<Comment> getComments(java.lang.String filename)
AbstractCompiler
public ModuleLoader getModuleLoader()
AbstractCompiler
public void initWebpackMap(com.google.common.collect.ImmutableMap<java.lang.String,java.lang.String> inputPathByWebpackId)
protected com.google.javascript.jscomp.CompilerExecutor createCompilerExecutor()
protected com.google.javascript.jscomp.CompilerExecutor getCompilerExecutor()
@GwtIncompatible(value="ObjectOutputStream") public void saveState(java.io.OutputStream outputStream) throws java.io.IOException
java.io.IOException
@GwtIncompatible(value="ObjectInputStream") public void restoreState(java.io.InputStream inputStream) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException
public ModuleMetadataMap getModuleMetadataMap()
getModuleMetadataMap
in class AbstractCompiler
public void setModuleMetadataMap(ModuleMetadataMap moduleMetadataMap)
setModuleMetadataMap
in class AbstractCompiler
public ModuleMap getModuleMap()
getModuleMap
in class AbstractCompiler
public void setModuleMap(ModuleMap moduleMap)
setModuleMap
in class AbstractCompiler
public void resetAndIntitializeSourceMap()
Copyright © 2009-2019 Google. All Rights Reserved.