Repository: ember-template-lint/ember-template-lint
Pull request #573 (2018-12-03 14:21:08)

Allow RegExp's with no-implicit-this 
The rule that permits `rules['no-implicit-this'].allow` array to include only strings prevents efficient work with ember-test-selectors (it is necessary to put all of `data-test-*` attributes in allow array, but this list may be large)

rwjblue: This test for example:

https://github.com/ember-template-lint/ember-template-lint/blob/4b2cec3eaee6b41d0910ad76256c1275ecc75a1f/test/unit/rules/lint-no-implicit-this-test.js#L24-L27


Patch:
diff --git a/docs/rule/no-implicit-this.md b/docs/rule/no-implicit-this.md
index 0c7fae7e..2ed26e8a 100644
--- a/docs/rule/no-implicit-this.md
+++ b/docs/rule/no-implicit-this.md
@@ -95,4 +95,4 @@ much easier to implement since we have semantics that mean "property on class".
   * boolean - `true` to enable / `false` to disable
   * object -- An object with the following keys:
     * `allow` -- An array of component / helper names for that may be called
-      without arguments
+      without arguments (string or regular expression)
diff --git a/lib/rules/lint-no-implicit-this.js b/lib/rules/lint-no-implicit-this.js
index 5dc38319..82cd8f52 100644
--- a/lib/rules/lint-no-implicit-this.js
+++ b/lib/rules/lint-no-implicit-this.js
@@ -16,6 +16,14 @@ function isString(value) {
   return typeof value === 'string';
 }
 
+function isRegExp(value) {
+  return value instanceof RegExp;
+}
+
+function allowedFormat(value){
+  return isString(value) || isRegExp(value);
+}
+
 // Allow Ember's builtin argless syntaxes
 const ARGLESS_BUILTINS = [
   'debugger',
@@ -55,7 +63,7 @@ module.exports = class StrictPaths extends Rule {
       }
 
     case 'object':
-      if (Array.isArray(config.allow) && config.allow.every(isString)) {
+      if (Array.isArray(config.allow) && config.allow.every(allowedFormat)) {
         return {
           allow: [].concat(
             ARGLESS_BUILTINS,
@@ -94,8 +102,9 @@ module.exports = class StrictPaths extends Rule {
           let valid = path.data ||
                       path.this ||
                       this.isLocal(path) ||
-                      this.config.allow.indexOf(path.original) > -1;
-
+                      this.config.allow.some((item) => {
+                        return isRegExp(item) ? item.test(path.original) : item === path.original;
+                      });
 
           if (!valid) {
             this.log({


Test patch:
diff --git a/test/fixtures/no-implicit-this-allow-with-regexp/.template-lintrc.js b/test/fixtures/no-implicit-this-allow-with-regexp/.template-lintrc.js
new file mode 100644
index 00000000..0c829723
--- /dev/null
+++ b/test/fixtures/no-implicit-this-allow-with-regexp/.template-lintrc.js
@@ -0,0 +1,10 @@
+module.exports = {
+  rules: {
+    'no-implicit-this': {
+      allow: [
+        /^data-test-/,
+        'book'
+      ]
+    }
+  }
+};
diff --git a/test/fixtures/no-implicit-this-allow-with-regexp/app/templates/application.hbs b/test/fixtures/no-implicit-this-allow-with-regexp/app/templates/application.hbs
new file mode 100644
index 00000000..d77387f1
--- /dev/null
+++ b/test/fixtures/no-implicit-this-allow-with-regexp/app/templates/application.hbs
@@ -0,0 +1,2 @@
+{{input data-test-input}}
+{{book}}
\ No newline at end of file
diff --git a/test/unit/rules/lint-no-implicit-this-test.js b/test/unit/rules/lint-no-implicit-this-test.js
index efd98670..efcd534c 100644
--- a/test/unit/rules/lint-no-implicit-this-test.js
+++ b/test/unit/rules/lint-no-implicit-this-test.js
@@ -25,6 +25,10 @@ let good = [
   {
     config: { allow: ['book-details'] },
     template: '{{book-details}}'
+  },
+  {
+    config: { allow: [/^data-test-.+/] },
+    template: '{{foo-bar data-test-foo}}'
   }
 ];