forked from ember-template-lint/ember-template-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ember-template-lint#1457 from Turbo87/no-potential…
…-path-strings Implement `no-potential-path-strings` rule
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,34 @@ | ||
# no-potential-path-strings | ||
|
||
It might happen sometimes that `{{` and `}}` are forgotten when invoking a | ||
component, and the string that is passed was actually supposed to be a property | ||
path or argument. | ||
|
||
This rule warns about all arguments and attributes that start with `this.` or | ||
`@`, but are missing the surrounding `{{` and `}}` characters. | ||
|
||
## Examples | ||
|
||
This rule **forbids** the following: | ||
|
||
```hbs | ||
<img src="this.picture"> | ||
``` | ||
|
||
```hbs | ||
<img src="@img"> | ||
``` | ||
|
||
This rule **allows** the following: | ||
|
||
```hbs | ||
<img src={{this.picture}}> | ||
``` | ||
|
||
```hbs | ||
<img src={{@img}}> | ||
``` | ||
|
||
## Migration | ||
|
||
- Replace the surrounding `"` characters with `{{`/`}}` |
This file contains 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 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,28 @@ | ||
'use strict'; | ||
|
||
const Rule = require('./base'); | ||
|
||
module.exports = class NoPotentialPathStrings extends Rule { | ||
visitor() { | ||
return { | ||
AttrNode(node) { | ||
let { value } = node; | ||
if ( | ||
value.type === 'TextNode' && | ||
(value.chars.startsWith('@') || value.chars.startsWith('this.')) | ||
) { | ||
this.log({ | ||
message: NoPotentialPathStrings.generateErrorMessage(value.chars), | ||
line: value.loc && value.loc.start.line, | ||
column: value.loc && value.loc.start.column, | ||
source: this.sourceForNode(value), | ||
}); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
static generateErrorMessage(path) { | ||
return `Potential path in attribute string detected. Did you mean {{${path}}}?`; | ||
} | ||
}; |
This file contains 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,76 @@ | ||
'use strict'; | ||
|
||
const generateRuleTests = require('../../helpers/rule-test-harness'); | ||
|
||
const { generateErrorMessage } = require('../../../lib/rules/no-potential-path-strings'); | ||
|
||
generateRuleTests({ | ||
name: 'no-potential-path-strings', | ||
|
||
config: true, | ||
|
||
good: [ | ||
'<img src="foo.png">', | ||
'<img src={{picture}}>', | ||
'<img src={{this.picture}}>', | ||
'<img src={{@img}}>', | ||
'<SomeComponent @foo={{@bar}} />', | ||
], | ||
|
||
bad: [ | ||
{ | ||
template: '<img src="this.picture">', | ||
result: { | ||
message: generateErrorMessage('this.picture'), | ||
line: 1, | ||
column: 10, | ||
source: 'this.picture', | ||
}, | ||
}, | ||
{ | ||
template: '<img src=this.picture>', | ||
result: { | ||
message: generateErrorMessage('this.picture'), | ||
line: 1, | ||
column: 9, | ||
source: 'this.picture', | ||
}, | ||
}, | ||
{ | ||
template: '<img src="@img">', | ||
result: { | ||
message: generateErrorMessage('@img'), | ||
line: 1, | ||
column: 10, | ||
source: '@img', | ||
}, | ||
}, | ||
{ | ||
template: '<img src=@img>', | ||
result: { | ||
message: generateErrorMessage('@img'), | ||
line: 1, | ||
column: 9, | ||
source: '@img', | ||
}, | ||
}, | ||
{ | ||
template: '<SomeComponent @foo=@bar />', | ||
result: { | ||
message: generateErrorMessage('@bar'), | ||
line: 1, | ||
column: 20, | ||
source: '@bar', | ||
}, | ||
}, | ||
{ | ||
template: '<SomeComponent @foo=this.bar />', | ||
result: { | ||
message: generateErrorMessage('this.bar'), | ||
line: 1, | ||
column: 20, | ||
source: 'this.bar', | ||
}, | ||
}, | ||
], | ||
}); |