Repository: ember-template-lint/ember-template-lint (js)

Issue:
Can't disable a plugin
If you create a plugin for ember-template-lint and then try to disable it, this won't work, for the following reasons:

1. if you disable only the rule that is associated with a plugin, the code here: https://github.com/rwjblue/ember-template-lint/blob/master/lib/rules/base.js#L72 will error out because plugins are not recognized as rules (only the rules that ship with `ember-template-lint` are recognized as rules.
2. If you disable all rules, the plugin's configuration does not seem to change in time for it to process the directive. I tried to log the events that were happening and their sequence, and this is what I found:

```
(output from the plugin, in its visitor() method, logging its config:)
this.config { min: 20, max: 100 }
-> rules/base.js, line 401 (https://github.com/rwjblue/ember-template-lint/blob/master/lib/rules/base.js#L401)
-> rules/base.js line 413 (https://github.com/rwjblue/ember-template-lint/blob/master/lib/rules/base.js#L401)
-> returning { tree: false, value: false }
-> rules/base.js line 337 (https://github.com/rwjblue/ember-template-lint/blob/master/lib/rules/base.js#L337)

/Users/psbanka/dev/copy-tango/app/pods/support/docs-search/template.hbs
  -:-  error  Template length of 12 is smaller than 20  length-validation
```

For some reason the pugins seem to be processed *before* instructions are processed and therefore their configs do not get updated.

Pull request:
Ensure custom rules can be disabled with inline comments.
Builds off of @patocallaghan's work in #718 (thank you!), but tweaks the implementation a bit to avoid re-parsing configuration.

Closes #718
Fixes #261

Patch:
diff --git a/lib/index.js b/lib/index.js
index aef45a98..1a1b6ee0 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -59,6 +59,7 @@ class Linter {
         console: this.console,
         log: addToResults,
         defaultSeverity: this._defaultSeverityForRule(pluginName, config.pending),
+        ruleNames: Object.keys(rules),
       });
 
       astPlugins.push(env => {
diff --git a/lib/rules/base.js b/lib/rules/base.js
index 1bea13f3..2eb6a232 100644
--- a/lib/rules/base.js
+++ b/lib/rules/base.js
@@ -43,6 +43,7 @@ module.exports = class BaseRule {
     this.ruleName = options.name;
     this._console = options.console || console;
     this._log = options.log;
+    this._ruleNames = options.ruleNames;
     this.severity = options.defaultSeverity;
 
     // To support DOM-scoped configuration instructions, we need to maintain
@@ -71,10 +72,7 @@ module.exports = class BaseRule {
   // The `name !== this.ruleName` check isn't strictly necessary, but allows
   // unit tests to register test rules
   _isRuleName(name) {
-    // must be required lazily so that rules can be populated
-    let rules = require('./index');
-
-    return rules[name] || name === this.ruleName || DEPRECATED_RULES.has(name);
+    return this._ruleNames.includes(name) || name === this.ruleName || DEPRECATED_RULES.has(name);
   }
 
   _refersToCurrentRule(name) {


Test patch:
diff --git a/test/acceptance-test.js b/test/acceptance-test.js
index 7e2e6bd3..7c01631e 100644
--- a/test/acceptance-test.js
+++ b/test/acceptance-test.js
@@ -392,6 +392,19 @@ describe('public api', function() {
 
       expect(result).to.deep.equal(expected);
     });
+
+    it('allow you to disable plugin rules inline', function() {
+      let templatePath = path.join(basePath, 'app', 'templates', 'disabled-rule.hbs');
+      let templateContents = fs.readFileSync(templatePath, { encoding: 'utf8' });
+      let expected = [];
+
+      let result = linter.verify({
+        source: templateContents,
+        moduleId: templatePath,
+      });
+
+      expect(result).to.deep.equal(expected);
+    });
   });
 
   describe('Linter using plugin with extends', function() {
diff --git a/test/fixtures/with-plugins/app/templates/disabled-rule.hbs b/test/fixtures/with-plugins/app/templates/disabled-rule.hbs
new file mode 100644
index 00000000..7b6df20b
--- /dev/null
+++ b/test/fixtures/with-plugins/app/templates/disabled-rule.hbs
@@ -0,0 +1,2 @@
+{{! template-lint-disable inline-component }}
+<h2>{{component value="Hej"}}</h2>
diff --git a/test/unit/base-plugin-test.js b/test/unit/base-plugin-test.js
index 854ea44c..55749038 100644
--- a/test/unit/base-plugin-test.js
+++ b/test/unit/base-plugin-test.js
@@ -3,6 +3,7 @@
 const expect = require('chai').expect;
 const _precompile = require('@glimmer/compiler').precompile;
 const Rule = require('./../../lib/rules/base');
+const ruleNames = Object.keys(require('../../lib/rules'));
 
 describe('base plugin', function() {
   function precompileTemplate(template, ast) {
@@ -45,7 +46,7 @@ describe('base plugin', function() {
   }
 
   function plugin(Rule, name, config) {
-    let plugin = new Rule({ name, config });
+    let plugin = new Rule({ name, config, ruleNames });
 
     return env => {
       plugin.templateEnvironmentData = env;