From 04b5372f237b48a21809fb89b1ecb419c56dd91a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Thu, 16 Apr 2020 17:16:29 +0200 Subject: [PATCH] Fix issue with Rhino localization --- .../lang/ecmascript/ast/EcmascriptTreeBuilder.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptTreeBuilder.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptTreeBuilder.java index e60282d493..cb44494be3 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptTreeBuilder.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptTreeBuilder.java @@ -11,6 +11,7 @@ import java.util.List; import java.util.Map; import java.util.Stack; +import org.mozilla.javascript.ScriptRuntime; import org.mozilla.javascript.ast.ArrayComprehension; import org.mozilla.javascript.ast.ArrayComprehensionLoop; import org.mozilla.javascript.ast.ArrayLiteral; @@ -71,6 +72,7 @@ import net.sourceforge.pmd.lang.ast.SourceCodePositioner; public final class EcmascriptTreeBuilder implements NodeVisitor { private static final Map, Constructor>> NODE_TYPE_TO_NODE_ADAPTER_TYPE = new HashMap<>(); + private static String trailingCommaLocalizedMessage = null; static { register(ArrayComprehension.class, ASTArrayComprehension.class); @@ -221,12 +223,19 @@ public final class EcmascriptTreeBuilder implements NodeVisitor { int nodeStart = node.getNode().getAbsolutePosition(); int nodeEnd = nodeStart + node.getNode().getLength() - 1; for (ParseProblem parseProblem : parseProblems) { + + if (trailingCommaLocalizedMessage == null) { + // This will fetch the localized message, at most once, and only if there are + // some problems (so we avoid parsing the message bundle for nothing) + // See https://github.com/pmd/pmd/issues/384 + trailingCommaLocalizedMessage = ScriptRuntime.getMessage0("msg.extra.trailing.comma"); + } + // The node overlaps the comma (i.e. end of the problem)? int problemStart = parseProblem.getFileOffset(); int commaPosition = problemStart + parseProblem.getLength() - 1; if (nodeStart <= commaPosition && commaPosition <= nodeEnd) { - if ("Trailing comma is not legal in an ECMA-262 object initializer" - .equals(parseProblem.getMessage())) { + if (trailingCommaLocalizedMessage.equals(parseProblem.getMessage())) { // Report on the shortest code block containing the // problem (i.e. inner most code in nested structures). EcmascriptNode currentNode = (EcmascriptNode) parseProblemToNode.get(parseProblem);