eslint-plugin-i18next
Advanced tools
@@ -46,2 +46,3 @@ /** | ||
| const { | ||
| parserServices, | ||
| options: [option] | ||
@@ -54,3 +55,3 @@ } = context; | ||
| const calleeWhitelists = generateCalleeWhitelists(option); | ||
| const message = 'disallow literal string'; | ||
| //---------------------------------------------------------------------- | ||
@@ -81,3 +82,3 @@ // Helpers | ||
| //---------------------------------------------------------------------- | ||
| return { | ||
| const scriptVisitor = { | ||
| Literal(node) { | ||
@@ -97,4 +98,8 @@ if (typeof node.value === 'string') { | ||
| case 'Property': { | ||
| // if node is key of property, skip | ||
| if (parent.key === node) return; | ||
| // name if key is Identifier; value if key is Literal | ||
| // dont care whether if this is computed or not | ||
| if (isUpperCase(parent.key.name)) return; | ||
| if (isUpperCase(parent.key.name || parent.key.value)) return; | ||
| break; | ||
@@ -105,10 +110,7 @@ } | ||
| default: | ||
| const LOOK_UP_LIMIT = 3; | ||
| const ancestors = context.getAncestors(node); | ||
| for ( | ||
| let i = ancestors.length - 1; | ||
| i > ancestors.length - 1 - LOOK_UP_LIMIT && i > -1; | ||
| i-- | ||
| ) { | ||
| const temp = ancestors[i]; | ||
| let LOOK_UP_LIMIT = 3; | ||
| let temp = parent; | ||
| while (temp && LOOK_UP_LIMIT > 0) { | ||
| LOOK_UP_LIMIT--; | ||
| if (temp.type === 'CallExpression') { | ||
@@ -118,2 +120,3 @@ if (isValidFunctionCall(temp)) return; | ||
| } | ||
| temp = temp.parent; | ||
| } | ||
@@ -125,6 +128,42 @@ break; | ||
| if (match(trimed)) return; | ||
| context.report({ node, message: 'disallow literal string' }); | ||
| context.report({ | ||
| node, | ||
| message, | ||
| fix(fixer) { | ||
| return fixer.replaceText(node, `i18next.t('${node.value}')`); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
| return ( | ||
| (parserServices.defineTemplateBodyVisitor && | ||
| parserServices.defineTemplateBodyVisitor( | ||
| { | ||
| VText(node) { | ||
| const trimed = node.value.trim(); | ||
| if (!trimed) return; | ||
| if (match(trimed)) return; | ||
| context.report({ | ||
| node, | ||
| message, | ||
| fix(fixer) { | ||
| return fixer.replaceText( | ||
| node, | ||
| node.value.replace( | ||
| /^(\s*)(.+?)(\s*)$/, // keep spaces | ||
| "$1{{i18next.t('$2')}}$3" | ||
| ) | ||
| ); | ||
| } | ||
| }); | ||
| }, | ||
| 'VExpressionContainer Literal'(node) { | ||
| scriptVisitor.Literal(node); | ||
| } | ||
| }, | ||
| scriptVisitor | ||
| )) || | ||
| scriptVisitor | ||
| ); | ||
| } | ||
@@ -131,0 +170,0 @@ }; |
+1
-1
| { | ||
| "name": "eslint-plugin-i18next", | ||
| "version": "1.0.5", | ||
| "version": "1.1.0", | ||
| "description": "ESLint plugin for i18n", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -41,4 +41,6 @@ /** | ||
| { code: 'var A_B = "world";' }, | ||
| { code: 'var a = {["A_B"]: "hello world"};' }, | ||
| { code: 'var a = {[A_B]: "hello world"};' }, | ||
| { code: 'var a = {A_B: "hello world"};' } | ||
| { code: 'var a = {A_B: "hello world"};' }, | ||
| { code: 'var a = {foo: "FOO"};' } | ||
| ], | ||
@@ -49,4 +51,5 @@ | ||
| { code: 'const a = call("Ffo");', errors }, | ||
| { code: 'var a = {foo: "bar"};', errors }, | ||
| { code: 'const a = "afoo";', options: [{ ignore: ['^foo'] }], errors } | ||
| ] | ||
| }); |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
13605
10.42%245
20.1%