Repository: ember-template-lint/ember-template-lint
Pull request #908 (2019-11-05 14:36:09)

Update `no-bare-strings` to ignore contents within elements that are not translated (e.g. `<script>`,`<style>`, and `<pre>`).
https://github.com/ember-template-lint/ember-template-lint/pull/905


Patch:
diff --git a/lib/rules/lint-no-bare-strings.js b/lib/rules/lint-no-bare-strings.js
index 747bb1a1..d6b88bfe 100644
--- a/lib/rules/lint-no-bare-strings.js
+++ b/lib/rules/lint-no-bare-strings.js
@@ -38,6 +38,8 @@ const TAG_ATTRIBUTES = {
   img: ['alt'],
 };
 
+const IGNORED_ELEMENTS = new Set(['pre', 'script', 'style', 'template', 'textarea']);
+
 // Character entity reference chart: https://dev.w3.org/html5/html-author/charref
 const DEFAULT_CONFIG = {
   whitelist: [
@@ -132,10 +134,17 @@ function isValidConfigObjectFormat(config) {
 }
 
 function sanitizeConfigArray(whitelist = []) {
-  return whitelist.filter(option => option !== '');
+  return whitelist.filter(option => option !== '').sort((a, b) => b.length - a.length);
 }
 
 module.exports = class LogStaticStrings extends Rule {
+  constructor(options) {
+    super(options);
+    this._elementStack = [];
+  }
+  isWithinIgnoredElement() {
+    return this._elementStack.some(n => IGNORED_ELEMENTS.has(n.tag));
+  }
   parseConfig(config) {
     let configType = typeof config;
 
@@ -193,8 +202,14 @@ module.exports = class LogStaticStrings extends Rule {
         }
       },
 
-      ElementNode(node) {
-        this._currentElementNode = node;
+      ElementNode: {
+        enter(node) {
+          this._currentElementNode = node;
+          this._elementStack.push(node);
+        },
+        exit() {
+          this._elementStack.pop();
+        },
       },
 
       AttrNode: {
@@ -242,6 +257,9 @@ module.exports = class LogStaticStrings extends Rule {
   }
 
   _checkNodeAndLog(node, additionalDescription, loc) {
+    if (this._currentElementNode && this.isWithinIgnoredElement()) {
+      return;
+    }
     if (node.type === 'TextNode') {
       let bareStringText = this._getBareString(node.chars);
 


Test patch:
diff --git a/test/unit/rules/lint-no-bare-strings-test.js b/test/unit/rules/lint-no-bare-strings-test.js
index 473ba00a..7fcf0c94 100644
--- a/test/unit/rules/lint-no-bare-strings-test.js
+++ b/test/unit/rules/lint-no-bare-strings-test.js
@@ -29,13 +29,33 @@ generateRuleTests({
       config: ['/', '"'],
       template: '{{t "foo"}} / "{{name}}"',
     },
+    {
+      config: ['&', '&times;', '4', '3=12'],
+      template: '4 &times; 3=12',
+    },
+    {
+      config: ['&', '&times;', 'Tom', 'Jerry'],
+      template: 'Tom & Jerry',
+    },
+    {
+      config: ['&', '&times;'],
+      template: '& &times;',
+    },
     '{{t "foo"}}',
     '{{t "foo"}}, {{t "bar"}} ({{length}})',
     '(),.&+-=*/#%!?:[]{}',
     '&lpar;&rpar;&comma;&period;&amp;&nbsp;',
     '{{! template-lint-disable no-bare-strings }}',
     '{{! template-lint-disable }}',
-
+    '<script> fdff sf sf f </script>',
+    '<style> fdff sf sf f </style>',
+    '<pre> fdff sf sf f </pre>',
+    '<template> fdff sf sf f </template>',
+    '<script> fdff sf sf <div> aaa </div> f </script>',
+    '<style> fdff sf sf <div> aaa </div> f </style>',
+    '<pre> fdff sf sf <div> aaa </div> f </pre>',
+    '<template> fdff sf sf <div> aaa </div> f </template>',
+    '<textarea> this is an input</textarea>',
     // placeholder is a <input> specific attribute
     '<div placeholder="wat?"></div>',