Repository: rwjblue/ember-template-lint
Pull request #306 (2017-10-21 22:22:04)

Deprecate `{{render}}` helper


Patch:
diff --git a/docs/rule/deprecations/deprecated-render-helper.md b/docs/rule/deprecations/deprecated-render-helper.md
new file mode 100644
index 0000000..e90b040
--- /dev/null
+++ b/docs/rule/deprecations/deprecated-render-helper.md
@@ -0,0 +1,21 @@
+## deprecated-render-helper
+
+In Ember 2.6 and newer, support for using the `{{render}}` helper has been deprecated.
+
+For example, this rule forbids the following:
+
+```hbs
+{{render 'this-is-bad'}}
+
+{{render 'also-bad' model}}
+```
+
+Instead, you should use:
+
+```hbs
+{{this-is-better}}
+
+{{saul-goodman model=model}}
+```
+
+More information is available at the [Deprecation Guide](https://emberjs.com/deprecations/v2.x/#toc_code-render-code-helper).
diff --git a/docs/rules.md b/docs/rules.md
index 9ececfd..38694a8 100644
--- a/docs/rules.md
+++ b/docs/rules.md
@@ -26,3 +26,4 @@
 
 * [deprecated-each-syntax](rule/deprecations/deprecated-each-syntax.md)
 * [deprecated-inline-view-helper](rule/deprecations/deprecated-inline-view-helper.md)
+* [deprecated-render-helper](rule/deprecations/deprecated-render-helper.md)
diff --git a/lib/config/recommended.js b/lib/config/recommended.js
index 8d760a6..5bcaf6f 100644
--- a/lib/config/recommended.js
+++ b/lib/config/recommended.js
@@ -14,6 +14,7 @@ module.exports = {
     'img-alt-attributes': true,
     'style-concatenation': true,
     'deprecated-inline-view-helper': true,
+    'deprecated-render-helper': true,
     'simple-unless': true,
     'unused-block-params': true
   }
diff --git a/lib/rules/deprecations/lint-deprecated-render-helper.js b/lib/rules/deprecations/lint-deprecated-render-helper.js
new file mode 100644
index 0000000..a95eae3
--- /dev/null
+++ b/lib/rules/deprecations/lint-deprecated-render-helper.js
@@ -0,0 +1,56 @@
+'use strict';
+
+const Rule = require('../base');
+
+const DEPRECATION_URL = 'https://emberjs.com/deprecations/v2.x/#toc_code-render-code-helper';
+
+const message = `The \`{{render}}\` helper is deprecated in favor of using components. Please see the deprecation guide at ${DEPRECATION_URL}.`;
+
+function logMessage(context, loc, actual, expected) {
+  return context.log({
+    message,
+    line: loc && loc.start.line,
+    column: loc && loc.start.column,
+    source: actual,
+    fix: {
+      text: expected
+    }
+  });
+}
+
+module.exports = class DeprecatedRenderHelper extends Rule {
+  visitor() {
+    return {
+      MustacheStatement(node) {
+        if (node.path.parts[0] === 'render') {
+          if (node.params.length === 1) {
+            this.processWithOneArgument(node);
+          } else if (node.params.length === 2) {
+            this.processWithTwoArguments(node);
+          }
+        }
+      }
+    };
+  }
+
+  processWithOneArgument(node) {
+    let loc = node.loc;
+    let originalValue = node.params[0].original;
+    let actual = `{{render '${originalValue}'}}`;
+    let expected = `{{${originalValue}}}`;
+
+    logMessage(this, loc, actual, expected);
+  }
+
+  processWithTwoArguments(node) {
+    let loc = node.loc;
+    let originalValue = node.params[0].original;
+    let model = node.params[1].original;
+    let actual = `{{render '${originalValue}' ${model}}}`;
+    let expected = `{{${originalValue} model=${model}}}`;
+
+    logMessage(this, loc, actual, expected);
+  }
+};
+
+module.exports.message = message;
diff --git a/lib/rules/index.js b/lib/rules/index.js
index 2cbbf33..7c527ce 100644
--- a/lib/rules/index.js
+++ b/lib/rules/index.js
@@ -15,6 +15,7 @@ module.exports = {
   'inline-link-to': require('./lint-inline-link-to'),
   'deprecated-each-syntax': require('./deprecations/lint-deprecated-each-syntax'),
   'deprecated-inline-view-helper': require('./deprecations/lint-deprecated-inline-view-helper'),
+  'deprecated-render-helper': require('./deprecations/lint-deprecated-render-helper'),
   'invalid-interactive': require('./lint-invalid-interactive'),
   'style-concatenation': require('./lint-style-concatenation'),
   'simple-unless': require('./lint-simple-unless'),


Test patch:
diff --git a/test/unit/rules/deprecations/lint-deprecated-render-helper-test.js b/test/unit/rules/deprecations/lint-deprecated-render-helper-test.js
new file mode 100644
index 0000000..ee5c6a1
--- /dev/null
+++ b/test/unit/rules/deprecations/lint-deprecated-render-helper-test.js
@@ -0,0 +1,51 @@
+'use strict';
+
+const generateRuleTests = require('../../../helpers/rule-test-harness');
+
+const message = require('../../../../lib/rules/deprecations/lint-deprecated-render-helper').message;
+
+generateRuleTests({
+  name: 'deprecated-render-helper',
+
+  config: true,
+
+  good: [
+    '{{valid-compoennt}}',
+    '{{input placeholder=(t "email") value=email}}',
+    '{{title "CrossCheck Web" prepent=true separator=" | "}}',
+    '{{hockey-player teamName="Boston Bruins"}}'
+  ],
+
+  bad: [
+    {
+      template: `{{render 'ken-griffey'}}`,
+
+      result: {
+        rule: 'deprecated-render-helper',
+        message,
+        moduleId: 'layout.hbs',
+        source: `{{render 'ken-griffey'}}`,
+        line: 1,
+        column: 0,
+        fix: {
+          text: '{{ken-griffey}}'
+        }
+      }
+    },
+    {
+      template: `{{render 'baseball-player' pitcher}}`,
+
+      result: {
+        rule: 'deprecated-render-helper',
+        message,
+        moduleId: 'layout.hbs',
+        source: `{{render 'baseball-player' pitcher}}`,
+        line: 1,
+        column: 0,
+        fix: {
+          text: '{{baseball-player model=pitcher}}'
+        }
+      }
+    }
+  ]
+});