-
-
Notifications
You must be signed in to change notification settings - Fork 681
⭐️New: Add vue/no-restricted-syntax
rule
#758
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 2 commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-restricted-syntax | ||
description: disallow specified syntax | ||
--- | ||
# vue/no-restricted-syntax | ||
> disallow specified syntax | ||
|
||
This rule is the same rule as core [no-restricted-syntax] rule but it applies to the expressions in `<template>`. | ||
|
||
|
||
## :wrench: Options | ||
|
||
Please see [no-restricted-syntax] for detailed options. | ||
|
||
You can include the AST created by [vue-eslint-parser] in the selector. | ||
To know more about certain nodes in produced AST, please go [vue-eslint-parser - AST docs]. | ||
|
||
### `"VElement > VExpressionContainer CallExpression"` | ||
|
||
Forbind call expressions on mustache interpolation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
<eslint-code-block :rules="{'vue/no-restricted-syntax': ['error', 'VElement > VExpressionContainer CallExpression']}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✔ GOOD --> | ||
<div> {{ foo }} </div> | ||
<div> {{ foo.bar }} </div> | ||
|
||
<!-- ✘ BAD --> | ||
<div> {{ foo() }} </div> | ||
<div> {{ foo.bar() }} </div> | ||
<div> {{ foo().bar }} </div> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :books: Further reading | ||
|
||
- [no-restricted-syntax] | ||
- [ESTree] | ||
- [vue-eslint-parser] | ||
|
||
[no-restricted-syntax]: https://eslint.org/docs/rules/no-restricted-syntax | ||
[ESTree]: https://github.com/estree/estree | ||
[vue-eslint-parser]: https://github.com/mysticatea/vue-eslint-parser | ||
[vue-eslint-parser - AST docs]: https://github.com/mysticatea/vue-eslint-parser/blob/master/docs/ast.md | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-restricted-syntax.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-restricted-syntax.js) |
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,9 @@ | ||
/** | ||
* @author Yosuke Ota | ||
*/ | ||
'use strict' | ||
|
||
const { wrapCoreRule } = require('../utils') | ||
|
||
// eslint-disable-next-line | ||
module.exports = wrapCoreRule(require('eslint/lib/rules/no-restricted-syntax')) |
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,118 @@ | ||
/** | ||
* @author Yosuke Ota | ||
*/ | ||
'use strict' | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../lib/rules/no-restricted-syntax') | ||
|
||
const tester = new RuleTester({ | ||
parser: 'vue-eslint-parser', | ||
parserOptions: { ecmaVersion: 2015 } | ||
}) | ||
|
||
tester.run('no-restricted-syntax', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
<template> | ||
<input :value="value"> | ||
</template>`, | ||
options: [ | ||
{ | ||
'selector': 'CallExpression', | ||
'message': 'Call expressions are not allowed.' | ||
} | ||
] | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
<template> | ||
<input :value="value()"> | ||
</template>`, | ||
options: [ | ||
{ | ||
'selector': 'CallExpression', | ||
'message': 'Call expressions are not allowed.' | ||
} | ||
], | ||
errors: [ | ||
{ | ||
message: 'Call expressions are not allowed.', | ||
line: 3, | ||
column: 26, | ||
endLine: 3, | ||
endColumn: 33 | ||
} | ||
] | ||
}, | ||
|
||
// Forbind call expressions on mustache interpolation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above ☝️ |
||
{ | ||
code: ` | ||
<template> | ||
<div> {{ foo() }} </div> | ||
<div> {{ foo.bar() }} </div> | ||
<div> {{ foo().bar }} </div> | ||
</template>`, | ||
options: [ | ||
{ | ||
'selector': 'VElement > VExpressionContainer CallExpression', | ||
'message': 'Call expressions are not allowed on mustache interpolation.' | ||
} | ||
], | ||
errors: [ | ||
{ | ||
message: 'Call expressions are not allowed on mustache interpolation.', | ||
line: 3, | ||
column: 20, | ||
endLine: 3, | ||
endColumn: 25 | ||
}, | ||
{ | ||
message: 'Call expressions are not allowed on mustache interpolation.', | ||
line: 4, | ||
column: 20, | ||
endLine: 4, | ||
endColumn: 29 | ||
}, | ||
{ | ||
message: 'Call expressions are not allowed on mustache interpolation.', | ||
line: 5, | ||
column: 20, | ||
endLine: 5, | ||
endColumn: 25 | ||
} | ||
] | ||
}, | ||
|
||
// Sample source code on issue 689 | ||
{ | ||
code: ` | ||
<template> | ||
<div :foo="$gettext(\`bar\`)">{{$gettext(\`bar\`)}}</div> | ||
</template>`, | ||
options: [ | ||
"CallExpression[callee.type='Identifier'][callee.name='$gettext'] TemplateLiteral" | ||
], | ||
errors: [ | ||
{ | ||
message: 'Using \'CallExpression[callee.type=\'Identifier\'][callee.name=\'$gettext\'] TemplateLiteral\' is not allowed.', | ||
line: 3, | ||
column: 29, | ||
endLine: 3, | ||
endColumn: 34 | ||
}, | ||
{ | ||
message: 'Using \'CallExpression[callee.type=\'Identifier\'][callee.name=\'$gettext\'] TemplateLiteral\' is not allowed.', | ||
line: 3, | ||
column: 48, | ||
endLine: 3, | ||
endColumn: 53 | ||
} | ||
] | ||
} | ||
] | ||
}) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put link to the AST docs here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is defined in line 50.