Skip to content

Commit

Permalink
chore: reposition scriptVisitor selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
edvardchen committed Jul 10, 2019
1 parent 1527eae commit 57c5066
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,9 @@ module.exports = {
}

const scriptVisitor = {
'JSXAttribute Literal'(node) {
const parent = getNearestAncestor(node, 'JSXAttribute');

// allow <div className="active" />
if (isValidAttrName(parent.name.name)) {
visited.push(node);
}
},
//
// ─── EXPORT AND IMPORT ───────────────────────────────────────────
//

'ImportDeclaration Literal'(node) {
// allow (import abc form 'abc')
Expand All @@ -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 <div className="active" />
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"
Expand All @@ -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;

Expand Down

0 comments on commit 57c5066

Please sign in to comment.