Package com.google.javascript.jscomp
Class JsMessageVisitor
java.lang.Object
com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback
com.google.javascript.jscomp.JsMessageVisitor
- All Implemented Interfaces:
CompilerPass
,NodeTraversal.Callback
public abstract class JsMessageVisitor
extends NodeTraversal.AbstractPostOrderCallback
implements CompilerPass
Locates JS code that is intended to declare localizable messages.
It passes each found message to either processJsMessageDefinition(JsMessageDefinition)
for goog.getMsg()
calls
or processIcuTemplateDefinition(IcuTemplateDefinition)
for
goog.i18n.messages.declareIcuTemplate()
calls.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Represents the contents of a object literal Node in the AST. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DiagnosticType
static final DiagnosticType
Expose a list of functions that are used in message extraction.static final DiagnosticType
static final DiagnosticType
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
JsMessageVisitor
(AbstractCompiler compiler, JsMessage.IdGenerator idGenerator) Creates JS message visitor. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Checks a node's type.extractObjectLiteralMap
(@Nullable Node objLit) Returns an object to represent an object literal Node from the AST.static @Nullable String
getExternalMessageId
(String messageKey) Extracts an external message ID from the message key, if it contains one.static boolean
isScopedAliasesPrefix
(String name) static com.google.common.collect.ImmutableList
<JsMessage.Part> parseJsMessageTextIntoParts
(String originalMsgText) void
Process the JS with root node root.protected abstract void
processIcuTemplateDefinition
(IcuTemplateDefinition definition) Processes a found call togoog.i18n.messages.declareIcuTemplate()
protected abstract void
processJsMessageDefinition
(JsMessageDefinition definition) Processes a found JS message that was defined withgoog.getMsg()
.static String
void
visit
(NodeTraversal traversal, Node node, Node unused) Visits a node in postorder (after its children).Methods inherited from class com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback
shouldTraverse
-
Field Details
-
FUNCTIONS_USED_IN_MESSAGE_EXTRACTION
Expose a list of functions that are used in message extraction. Standalone tools that run message extraction can use this list to filter out files that do not contain any of these functions. -
MESSAGE_TREE_MALFORMED
-
MESSAGE_NOT_INITIALIZED_CORRECTLY
-
BAD_FALLBACK_SYNTAX
-
FALLBACK_ARG_ERROR
-
-
Constructor Details
-
JsMessageVisitor
Creates JS message visitor.- Parameters:
compiler
- the compiler instanceidGenerator
- generator that used for creating unique ID for the message
-
-
Method Details
-
process
Description copied from interface:CompilerPass
Process the JS with root node root. Can modify the contents of each Node tree- Specified by:
process
in interfaceCompilerPass
- Parameters:
externs
- Top of external JS treeroot
- Top of JS tree
-
visit
Description copied from interface:NodeTraversal.Callback
Visits a node in postorder (after its children). A node is visited in postorder iffNodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
returned true for its parent and itself. In particular, the root node is never visited in postorder.Siblings are always visited left-to-right.
Implementations can have side-effects (e.g. modify the parse tree). Removing the current node is legal, but removing or reordering nodes above the current node may cause nodes to be visited twice or not at all.
- Specified by:
visit
in interfaceNodeTraversal.Callback
- Parameters:
traversal
- The current traversal.node
- The current node.unused
- The parent of the current node.
-
getExternalMessageId
Extracts an external message ID from the message key, if it contains one.- Parameters:
messageKey
- the message key (usually the variable or property name)- Returns:
- the external ID if it is found, otherwise `null`
-
extractObjectLiteralMap
public static JsMessageVisitor.ObjectLiteralMap extractObjectLiteralMap(@Nullable Node objLit) throws com.google.javascript.jscomp.JsMessageVisitor.MalformedException Returns an object to represent an object literal Node from the AST.The object literal's members must all be `STRING_KEY` nodes.
No duplicate string keys are allowed.
A `null` argument is treated as if it were an empty object literal.
- Throws:
com.google.javascript.jscomp.JsMessageVisitor.MalformedException
- If the Node does not meet the above requirements.
-
parseJsMessageTextIntoParts
public static com.google.common.collect.ImmutableList<JsMessage.Part> parseJsMessageTextIntoParts(String originalMsgText) throws JsMessage.PlaceholderFormatException -
processJsMessageDefinition
Processes a found JS message that was defined withgoog.getMsg()
.Several examples of "standard" processing routines are:
- extract all JS messages
- replace JS messages with localized versions for some specific language
- check that messages have correct syntax and present in localization bundle
-
processIcuTemplateDefinition
Processes a found call togoog.i18n.messages.declareIcuTemplate()
-
checkNode
protected void checkNode(@Nullable Node node, Token type) throws com.google.javascript.jscomp.JsMessageVisitor.MalformedException Checks a node's type.- Throws:
com.google.javascript.jscomp.JsMessageVisitor.MalformedException
- if the node is null or the wrong type
-
isScopedAliasesPrefix
-
removeScopedAliasesPrefix
-