-
-
Notifications
You must be signed in to change notification settings - Fork 209
Add new rule template-no-let-reference
#1939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # ember/template-no-let-reference | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| Disallows referencing let/var variables in templates. | ||
|
|
||
| ```js | ||
| // app/components/foo.gjs | ||
| let foo = 1; | ||
|
|
||
| function increment() { | ||
| foo++; | ||
| } | ||
|
|
||
| export default <template>{{foo}}</template>; | ||
| ``` | ||
|
|
||
| This does not "work" – it doesn't error, but it will essentially capture and compile in the value of the foo variable at some arbitrary point and never update again. Even if the component is torn down and a new instance is created/rendered, it will probably still hold on to the old value when the template was initially compiled. | ||
|
|
||
| So, generally speaking, one should avoid referencing let variables from within <template> and instead prefer to use const bindings. | ||
|
|
||
| ## Rule Detail | ||
|
|
||
| Use `const` variables instead of `let`. | ||
|
|
||
| ## Examples | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| let x = 1; | ||
| <template> | ||
| {{x}} | ||
| </template> | ||
| ``` | ||
|
|
||
| ```js | ||
| let Comp = x; // SomeComponent | ||
| <template> | ||
| <Comp /> | ||
| </template> | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| const x = 1; | ||
| <template> | ||
| {{x}} | ||
| </template> | ||
| ``` | ||
|
|
||
| ```js | ||
| const Comp = x; // SomeComponent | ||
| <template> | ||
| <Comp /> | ||
| </template> | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| const ERROR_MESSAGE = | ||
| 'update-able variables are not supported in templates, reference a const variable'; | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| ERROR_MESSAGE, | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: 'disallow referencing let variables in \\<template\\>', | ||
| category: 'Ember Octane', | ||
| recommended: false, | ||
| url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-let-reference.md', | ||
| }, | ||
| fixable: null, | ||
| schema: [], | ||
| }, | ||
|
|
||
| create: (context) => { | ||
| const sourceCode = context.sourceCode; | ||
|
|
||
| function checkIfWritableReferences(node, scope) { | ||
| const ref = scope.references.find((v) => v.identifier === node); | ||
| if (!ref) { | ||
| return; | ||
| } | ||
| if (ref.resolved?.identifiers.some((i) => ['let', 'var'].includes(i.parent.parent.kind))) { | ||
| context.report({ node, message: ERROR_MESSAGE }); | ||
patricklx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| return { | ||
| GlimmerPathExpression(node) { | ||
| checkIfWritableReferences(node.head, sourceCode.getScope(node)); | ||
| }, | ||
|
|
||
| GlimmerElementNode(node) { | ||
| // glimmer element is in its own scope, need tp get upper scope | ||
| const scope = sourceCode.getScope(node.parent); | ||
| checkIfWritableReferences(node, scope); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| //------------------------------------------------------------------------------ | ||
| // Requirements | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const rule = require('../../../lib/rules/template-no-let-reference'); | ||
| const RuleTester = require('eslint').RuleTester; | ||
|
|
||
| const { ERROR_MESSAGE } = rule; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Tests | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| parser: require.resolve('../../../lib/parsers/gjs-gts-parser.js'), | ||
| parserOptions: { ecmaVersion: 2020, sourceType: 'module' }, | ||
| }); | ||
| ruleTester.run('template-no-let-reference', rule, { | ||
| valid: [ | ||
| ` | ||
| const a = ''; | ||
| <template> | ||
| {{a}} | ||
| </template> | ||
| `, | ||
| ` | ||
| const a = ''; | ||
| <template> | ||
| <a></a> | ||
| </template> | ||
| `, | ||
| // make sure rule does not error out on missing references | ||
| ` | ||
| const a = ''; | ||
| <template> | ||
| {{ab}} | ||
| <ab></ab> | ||
| </template> | ||
| `, | ||
| ], | ||
|
|
||
| invalid: [ | ||
| { | ||
| code: ` | ||
| let a = ''; | ||
| <template> | ||
| {{a}} | ||
| </template> | ||
| `, | ||
| output: null, | ||
| errors: [{ type: 'VarHead', message: ERROR_MESSAGE }], | ||
| }, | ||
| { | ||
| code: ` | ||
| var a = ''; | ||
| <template> | ||
| <a></a> | ||
| </template> | ||
| `, | ||
| output: null, | ||
| errors: [{ type: 'GlimmerElementNode', message: ERROR_MESSAGE }], | ||
| }, | ||
| ], | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.