From 57c5066cd70b3b34976acb84a31d4ee43ceab060 Mon Sep 17 00:00:00 2001 From: edvardchen <> Date: Wed, 10 Jul 2019 15:23:01 +0800 Subject: [PATCH] chore: reposition scriptVisitor selectors --- lib/rules/no-literal-string.js | 58 ++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/lib/rules/no-literal-string.js b/lib/rules/no-literal-string.js index 9210369..5e7391d 100644 --- a/lib/rules/no-literal-string.js +++ b/lib/rules/no-literal-string.js @@ -96,14 +96,9 @@ module.exports = { } const scriptVisitor = { - 'JSXAttribute Literal'(node) { - const parent = getNearestAncestor(node, 'JSXAttribute'); - - // allow
- if (isValidAttrName(parent.name.name)) { - visited.push(node); - } - }, + // + // ─── EXPORT AND IMPORT ─────────────────────────────────────────── + // 'ImportDeclaration Literal'(node) { // allow (import abc form 'abc') @@ -119,6 +114,37 @@ module.exports = { // allow export { named } from 'mod' visited.push(node); }, + // ───────────────────────────────────────────────────────────────── + + // + // ─── JSX ───────────────────────────────────────────────────────── + // + + 'JSXElement > Literal'(node) { + scriptVisitor.JSXText(node); + }, + + 'JSXAttribute Literal'(node) { + const parent = getNearestAncestor(node, 'JSXAttribute'); + + // allow + if (isValidAttrName(parent.name.name)) { + visited.push(node); + } + }, + + // @typescript-eslint/parser would parse string literal as JSXText node + JSXText(node) { + const trimed = node.value.trim(); + visited.push(node); + + if (!trimed || match(trimed)) { + return; + } + + context.report({ node, message }); + }, + // ───────────────────────────────────────────────────────────────── 'VariableDeclarator > Literal'(node) { // allow statements like const A_B = "test" @@ -143,22 +169,6 @@ module.exports = { if (isValidFunctionCall(parent)) visited.push(node); }, - 'JSXElement > Literal'(node) { - scriptVisitor.JSXText(node); - }, - - // @typescript-eslint/parser would parse string literal as JSXText node - JSXText(node) { - const trimed = node.value.trim(); - visited.push(node); - - if (!trimed || match(trimed)) { - return; - } - - context.report({ node, message }); - }, - 'Literal:exit'(node) { if (visited.includes(node)) return;