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
@GwtIncompatible("JsMessage, java.util.regex") public abstract class JsMessageVisitor extends NodeTraversal.AbstractPostOrderCallback implements CompilerPass
Traverses across parsed tree and finds I18N messages. Then it passes it toprocessJsMessage(JsMessage, JsMessageDefinition)
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
JsMessageVisitor.ObjectLiteralMap
Represents the contents of a object literal Node in the AST.
-
Field Summary
Fields Modifier and Type Field Description static DiagnosticType
BAD_FALLBACK_SYNTAX
static DiagnosticType
FALLBACK_ARG_ERROR
static DiagnosticType
MESSAGE_NOT_INITIALIZED_CORRECTLY
static DiagnosticType
MESSAGE_TREE_MALFORMED
-
Constructor Summary
Constructors Modifier Constructor Description protected
JsMessageVisitor(AbstractCompiler compiler, JsMessage.IdGenerator idGenerator)
Creates JS message visitor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
checkNode(@Nullable Node node, Token type)
Checks a node's type.static JsMessageVisitor.ObjectLiteralMap
extractObjectLiteralMap(@Nullable Node objLit)
Returns an object to represent an object literal Node from the AST.static @Nullable java.lang.String
getExternalMessageId(java.lang.String messageKey)
Extracts an external message ID from the message key, if it contains one.static boolean
isScopedAliasesPrefix(java.lang.String name)
static com.google.common.collect.ImmutableList<JsMessage.Part>
parseJsMessageTextIntoParts(java.lang.String originalMsgText)
void
process(Node externs, Node root)
Process the JS with root node root.protected abstract void
processJsMessage(JsMessage message, JsMessageDefinition definition)
Processes found JS message.static java.lang.String
removeScopedAliasesPrefix(java.lang.String name)
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 Detail
-
MESSAGE_TREE_MALFORMED
public static final DiagnosticType MESSAGE_TREE_MALFORMED
-
MESSAGE_NOT_INITIALIZED_CORRECTLY
public static final DiagnosticType MESSAGE_NOT_INITIALIZED_CORRECTLY
-
BAD_FALLBACK_SYNTAX
public static final DiagnosticType BAD_FALLBACK_SYNTAX
-
FALLBACK_ARG_ERROR
public static final DiagnosticType FALLBACK_ARG_ERROR
-
-
Constructor Detail
-
JsMessageVisitor
protected JsMessageVisitor(AbstractCompiler compiler, JsMessage.IdGenerator idGenerator)
Creates JS message visitor.- Parameters:
compiler
- the compiler instanceidGenerator
- generator that used for creating unique ID for the message
-
-
Method Detail
-
process
public void process(Node externs, Node root)
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
public void visit(NodeTraversal traversal, Node node, Node unused)
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. 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
public static @Nullable java.lang.String getExternalMessageId(java.lang.String messageKey)
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(java.lang.String originalMsgText) throws JsMessage.PlaceholderFormatException
-
processJsMessage
protected abstract void processJsMessage(JsMessage message, JsMessageDefinition definition)
Processes found JS message. 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
- Parameters:
message
- the found messagedefinition
- the definition of the object and usually contains all additional message information like message node/parent's node
-
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
public static boolean isScopedAliasesPrefix(java.lang.String name)
-
removeScopedAliasesPrefix
public static java.lang.String removeScopedAliasesPrefix(java.lang.String name)
-
-