-
-
Notifications
You must be signed in to change notification settings - Fork 669
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
Add new vue/enforce-style-attribute
rule
#2110
Merged
ota-meshi
merged 13 commits into
vuejs:master
from
mussinbenarbia:enforce-style-attribute
Jan 9, 2024
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d36bf2c
feat: add new `vue/enforce-style-attribute` rule (#2109)
mussinbenarbia e9ea5b1
Merge branch 'master' into enforce-style-attribute
FloEdelmann 06ba377
refactor: allow multiple attributes
79fface
style: apply formatter
43a0864
docs: update documentation to reflect new implementation
bfde06c
Merge branch 'master' into enforce-style-attribute
mussinbenarbia 62b053f
refactor: use sourceCode parser services instead of context
f1b495a
docs: move options section under rule details
0467dcc
docs: run docs build
9e5fd3a
refactor: set fixable as null and regen docs
43e3f63
refactor: rename allows option into allow
d33d671
docs: update option name in docs
7a1ebfb
refactor: rename rule option for better clarity
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 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,85 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/enforce-style-attribute | ||
description: enforce or forbid the use of the `scoped` and `module` attributes in SFC top level style tags | ||
--- | ||
# vue/enforce-style-attribute | ||
|
||
> enforce or forbid the use of the `scoped` and `module` attributes in SFC top level style tags | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule allows you to selectively allow attributes on your top level style tags and warns when using an attribute that is not allowed. | ||
|
||
### `"scoped"` | ||
|
||
<eslint-code-block :rules="{'vue/enforce-style-attribute': ['error', { allow: ['scoped'] }]}"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<style scoped></style> | ||
<style lang="scss" src="../path/to/style.scss" scoped></style> | ||
|
||
<!-- ✗ BAD --> | ||
<style module></style> | ||
|
||
<!-- ✗ BAD --> | ||
<style></style> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `"module"` | ||
|
||
<eslint-code-block :rules="{'vue/enforce-style-attribute': ['error', { allow: ['module'] }]}"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<style module></style> | ||
|
||
<!-- ✗ BAD --> | ||
<style scoped></style> | ||
|
||
<!-- ✗ BAD --> | ||
<style></style> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `"no-attributes"` | ||
|
||
<eslint-code-block :rules="{'vue/enforce-style-attribute': ['error', { allow: ['no-attributes']}]}"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<style></style> | ||
|
||
<!-- ✗ BAD --> | ||
<style scoped></style> | ||
|
||
<!-- ✗ BAD --> | ||
<style module></style> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/enforce-style-attribute": [ | ||
"error", | ||
{ "allow": ["scoped", "module", "no-attributes"] } | ||
] | ||
} | ||
``` | ||
|
||
- `"allow"` (`["scoped" | "module" | "no-attributes"]`) Array of attributes to allow on a top level style tag. Default: `["scoped"]` | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/enforce-style-attribute.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/enforce-style-attribute.js) |
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
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,153 @@ | ||
/** | ||
* @author Mussin Benarbia | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const { isVElement } = require('../utils') | ||
|
||
/** | ||
* check whether a tag has the `scoped` attribute | ||
* @param {VElement} componentBlock | ||
*/ | ||
function isScoped(componentBlock) { | ||
return componentBlock.startTag.attributes.some( | ||
(attribute) => !attribute.directive && attribute.key.name === 'scoped' | ||
) | ||
} | ||
|
||
/** | ||
* check whether a tag has the `module` attribute | ||
* @param {VElement} componentBlock | ||
*/ | ||
function isModule(componentBlock) { | ||
return componentBlock.startTag.attributes.some( | ||
(attribute) => !attribute.directive && attribute.key.name === 'module' | ||
) | ||
} | ||
|
||
/** | ||
* check if a tag doesn't have either the `scoped` nor `module` attribute | ||
* @param {VElement} componentBlock | ||
*/ | ||
function hasNoAttributes(componentBlock) { | ||
return !isScoped(componentBlock) && !isModule(componentBlock) | ||
} | ||
|
||
function getUserDefinedAllowedAttrs(context) { | ||
if (context.options[0] && context.options[0].allow) { | ||
return context.options[0].allow | ||
} | ||
return [] | ||
} | ||
|
||
const defaultAllowedAttrs = ['scoped'] | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'enforce or forbid the use of the `scoped` and `module` attributes in SFC top level style tags', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/enforce-style-attribute.html' | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
allow: { | ||
type: 'array', | ||
minItems: 1, | ||
uniqueItems: true, | ||
items: { | ||
type: 'string', | ||
enum: ['no-attributes', 'scoped', 'module'] | ||
} | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
], | ||
messages: { | ||
notAllowedScoped: | ||
'The scoped attribute is not allowed. Allowed: {{ allowedAttrsString }}.', | ||
notAllowedModule: | ||
'The module attribute is not allowed. Allowed: {{ allowedAttrsString }}.', | ||
notAllowedNoAttributes: | ||
'A <style> tag without attributes is not allowed. Allowed: {{ allowedAttrsString }}.' | ||
} | ||
}, | ||
|
||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const sourceCode = context.getSourceCode() | ||
if (!sourceCode.parserServices.getDocumentFragment) { | ||
return {} | ||
} | ||
const documentFragment = sourceCode.parserServices.getDocumentFragment() | ||
if (!documentFragment) { | ||
return {} | ||
} | ||
|
||
const topLevelElements = documentFragment.children.filter(isVElement) | ||
const topLevelStyleTags = topLevelElements.filter( | ||
(element) => element.rawName === 'style' | ||
) | ||
|
||
if (topLevelStyleTags.length === 0) { | ||
return {} | ||
} | ||
|
||
const userDefinedAllowedAttrs = getUserDefinedAllowedAttrs(context) | ||
const allowedAttrs = | ||
userDefinedAllowedAttrs.length > 0 | ||
? userDefinedAllowedAttrs | ||
: defaultAllowedAttrs | ||
|
||
const allowsNoAttributes = allowedAttrs.includes('no-attributes') | ||
const allowsScoped = allowedAttrs.includes('scoped') | ||
const allowsModule = allowedAttrs.includes('module') | ||
const allowedAttrsString = [...allowedAttrs].sort().join(', ') | ||
|
||
return { | ||
Program() { | ||
for (const styleTag of topLevelStyleTags) { | ||
if (!allowsNoAttributes && hasNoAttributes(styleTag)) { | ||
context.report({ | ||
node: styleTag, | ||
messageId: 'notAllowedNoAttributes', | ||
data: { | ||
allowedAttrsString | ||
} | ||
}) | ||
return | ||
} | ||
|
||
if (!allowsScoped && isScoped(styleTag)) { | ||
context.report({ | ||
node: styleTag, | ||
messageId: 'notAllowedScoped', | ||
data: { | ||
allowedAttrsString | ||
} | ||
}) | ||
return | ||
} | ||
|
||
if (!allowsModule && isModule(styleTag)) { | ||
context.report({ | ||
node: styleTag, | ||
messageId: 'notAllowedModule', | ||
data: { | ||
allowedAttrsString | ||
} | ||
}) | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Hmm. I think the name
no-attributes
is misleading. lang="scss" has attributes. What do you think?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.
Yeah... that could be confusing 🤔 Maybe using
plain
like in the other plugin is the best choice here. If you agree I'll rename itThere 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.
I think
plain
is better 👍