Repository: edvardchen/eslint-plugin-i18next
Pull request #9 (2019-10-03 02:43:20)

feat: add more ignored attributes and callee
Add more common attributes and functions to the lists and add tests around those + add tests to make sure attributes for accessibility are not ignored.


Patch:
diff --git a/lib/rules/no-literal-string.js b/lib/rules/no-literal-string.js
index 9f90908..b48a43d 100644
--- a/lib/rules/no-literal-string.js
+++ b/lib/rules/no-literal-string.js
@@ -84,9 +84,11 @@ module.exports = {
 
     const ignoredAttributes = (option && option.ignoreAttribute) || [];
     const atts = [
-      'className', 'style', 'styleName', 'src', 'type', 'id',
+      'className', 'style', 'styleName', 'src', 'type', 'id', 'target', 'href', 'for', 'form', 'rel', 'tabIndex',
+      'aria-labelledby', 'role',
       // SVG related attributes.
-      'viewBox', 'cx', 'cy', 'r', 'rx', 'ry', 'd', 'x1', 'y1', 'x2', 'y2', 'fill', 'stroke', 'strokeWidth', 'points',
+      'viewBox', 'cx', 'cy', 'r', 'rx', 'ry', 'd', 'x1', 'y1', 'x2', 'y2', 'fill', 'fillRule', 'stroke', 'strokeWidth',
+      'strokeLinejoin', 'strokeLinecap', 'points', 'transform', 'width', 'height',
 
       ...ignoredAttributes
     ];
@@ -294,6 +296,9 @@ module.exports = {
 
 const popularCallee = [
   'addEventListener',
+  'removeEventListener',
+  'postMessage',
+  'getElementById',
   //
   // ─── VUEX CALLEE ────────────────────────────────────────────────────────────────
   //
@@ -302,7 +307,9 @@ const popularCallee = [
   // ────────────────────────────────────────────────────────────────────────────────
 
   'includes',
-  'indexOf'
+  'indexOf',
+  'endsWith',
+  'startsWith'
 ];
 function generateCalleeWhitelists(option) {
   const ignoreCallee = (option && option.ignoreCallee) || [];


Test patch:
diff --git a/tests/lib/rules/no-literal-string.js b/tests/lib/rules/no-literal-string.js
index 3a44031..2dd0941 100644
--- a/tests/lib/rules/no-literal-string.js
+++ b/tests/lib/rules/no-literal-string.js
@@ -35,9 +35,14 @@ ruleTester.run('no-literal-string', rule, {
     { code: 'import name from "hello";' },
     { code: 'a.indexOf("ios")' },
     { code: 'a.includes("ios")' },
+    { code: 'a.startsWith("ios")' },
+    { code: 'a.endsWith("@gmail.com")' },
     { code: 'export * from "hello_export_all";' },
     { code: 'export { a } from "hello_export";' },
     { code: 'document.addEventListener("click", (event) => { event.preventDefault() })' },
+    { code: 'document.removeEventListener("click", (event) => { event.preventDefault() })' },
+    { code: 'window.postMessage("message", "*")' },
+    { code: 'document.getElementById("some-id")' },
     { code: 'require("hello");' },
     { code: 'const a = require(["hello"]);' },
     { code: 'const a = require(["hel" + "lo"]);' },
@@ -62,7 +67,12 @@ ruleTester.run('no-literal-string', rule, {
     { code: '<svg viewBox="0 0 20 40"></svg>' },
     { code: '<line x1="0" y1="0" x2="10" y2="20" />' },
     { code: '<path d="M10 10" />' },
-    { code: '<circle cx="10" cy="10" r="2" fill="red" />' },
+    { code: '<circle width="16px" height="16px" cx="10" cy="10" r="2" fill="red" />' },
+    { code: '<a href="https://google.com" target="_blank" rel="noreferrer noopener"></a>' },
+    { code: '<div id="some-id" tabIndex="0" aria-labelledby="label-id"></div>' },
+    { code: '<div role="button"></div>' },
+    { code: '<img src="./image.png" />' },
+    { code: '<button type="button" for="form-id" />' },
     { code: '<div foo="bar" />', options: [{ ignoreAttribute: ['foo'] }] }
   ],
 
@@ -80,7 +90,9 @@ ruleTester.run('no-literal-string', rule, {
     // JSX
     { code: '<div>foo</div>', errors },
     { code: '<div>FOO</div>', errors },
-    { code: '<div foo="bar" />', errors }
+    { code: '<div foo="bar" />', errors },
+    { code: '<img src="./image.png" alt="some-image" />', errors },
+    { code: '<button aria-label="Close" type="button" />', errors }
   ]
 });