Skip to content

Commit

Permalink
Merge 1e5c101 into b0eb172
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Apr 22, 2020
2 parents b0eb172 + 1e5c101 commit 11fb201
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Expand Up @@ -16,7 +16,8 @@ module.exports = {
rules: {
'require-jsdoc': 'error',
"no-warning-comments": "warn",
'no-lonely-if': 'off'
'no-lonely-if': 'off',
"@mysticatea/ts/ban-ts-ignore": 'off'
},
overrides: [
{
Expand Down
32 changes: 32 additions & 0 deletions docs/rules/require-scoped.md
Expand Up @@ -31,6 +31,38 @@ This rule reports the `<style>` tags missing the `scoped` attribute.

</eslint-code-block>

## :wrench: Options

Default is set to `always`.

```json
{
"vue-scoped-css/require-scoped": ["error", "always" | "never"]
}
```

- `"always"` (default) ... requires `scoped`.
- `"never"` ... disallowed `scoped`.

### `"never"`

<eslint-code-block :rules="{'vue-scoped-css/require-scoped': ['error', 'never']}">

```vue
<template>
</template>
<!-- ✓ GOOD -->
<style>
</style>
<!-- ✗ BAD -->
<style scoped>
</style>
```

</eslint-code-block>

## :books: Further reading

- [Vue Loader - Scoped CSS]
Expand Down
42 changes: 36 additions & 6 deletions lib/rules/require-scoped.ts
Expand Up @@ -20,12 +20,15 @@ module.exports = {
fixable: null,
messages: {
missing: "Missing `scoped` attribute.",
forbidden: "`scoped` attribute are forbidden.",
add: "Add `scoped` attribute.",
remove: "Remove `scoped` attribute.",
},
schema: [],
schema: [{ enum: ["always", "never"] }],
type: "suggestion",
},
create(context: RuleContext) {
const always = context.options[0] !== "never"
const styles = getStyleContexts(context).filter(StyleContext.isValid)
if (!styles.length) {
return {}
Expand All @@ -37,7 +40,7 @@ module.exports = {
* Reports the given node.
* @param {ASTNode} node node to report
*/
function report(node: AST.VElement) {
function reportAlways(node: AST.VElement) {
reporter.report({
node: node.startTag,
messageId: "missing",
Expand All @@ -50,8 +53,7 @@ module.exports = {
return (
close &&
fixer.insertTextBefore(
// eslint-disable-next-line @mysticatea/ts/ban-ts-ignore, spaced-comment
/// @ts-ignore
// @ts-ignore
close,
" scoped",
)
Expand All @@ -62,11 +64,39 @@ module.exports = {
})
}

/**
* Reports the given node.
* @param {ASTNode} node node to report
*/
function reportNever(node: AST.VElement) {
const scopedAttr = node.startTag.attributes.find(
attr => attr.key.name === "scoped",
)
reporter.report({
node: scopedAttr!,
messageId: "forbidden",
data: {},
suggest: [
{
messageId: "remove",
fix(fixer: Rule.RuleFixer) {
return fixer.remove(
// @ts-ignore
scopedAttr,
)
},
},
],
})
}

return {
"Program:exit"() {
for (const style of styles) {
if (!style.scoped) {
report(style.styleElement)
if (always && !style.scoped) {
reportAlways(style.styleElement)
} else if (!always && style.scoped) {
reportNever(style.styleElement)
}
}
},
Expand Down
45 changes: 41 additions & 4 deletions tests/lib/rules/require-scoped.ts
Expand Up @@ -24,6 +24,15 @@ tester.run("require-scoped", rule, {
<template>
</template>
`,
{
code: `
<template>
</template>
<style>
</style>
`,
options: ["never"],
},
],
invalid: [
{
Expand All @@ -40,8 +49,7 @@ tester.run("require-scoped", rule, {
column: 13,
endLine: 4,
endColumn: 20,
// eslint-disable-next-line @mysticatea/ts/ban-ts-ignore, spaced-comment
/// @ts-ignore
// @ts-ignore
suggestions: [
{
desc: "Add `scoped` attribute.",
Expand Down Expand Up @@ -69,8 +77,7 @@ tester.run("require-scoped", rule, {
column: 13,
endLine: 4,
endColumn: 22,
// eslint-disable-next-line @mysticatea/ts/ban-ts-ignore, spaced-comment
/// @ts-ignore
// @ts-ignore
suggestions: [
{
desc: "Add `scoped` attribute.",
Expand Down Expand Up @@ -104,5 +111,35 @@ tester.run("require-scoped", rule, {
},
]
: []),
{
code: `
<template>
</template>
<style scoped>
</style>
`,
options: ["never"],
errors: [
{
messageId: "forbidden",
line: 4,
column: 20,
endLine: 4,
endColumn: 26,
// @ts-ignore
suggestions: [
{
desc: "Remove `scoped` attribute.",
output: `
<template>
</template>
<style >
</style>
`,
},
],
},
],
},
],
})
3 changes: 0 additions & 3 deletions tests/lib/styles/parser/index.ts
Expand Up @@ -3,13 +3,10 @@ import assert from "assert"
import fs from "fs"
import path from "path"

// eslint-disable-next-line @mysticatea/ts/ban-ts-ignore
// @ts-ignore
import CSSStringifier from "postcss/lib/stringifier"
// eslint-disable-next-line @mysticatea/ts/ban-ts-ignore
// @ts-ignore
import SCSSStringifier from "postcss-scss/lib/scss-stringifier"
// eslint-disable-next-line @mysticatea/ts/ban-ts-ignore
// @ts-ignore
import StylusStringifier from "postcss-styl/lib/stringifier"

Expand Down

0 comments on commit 11fb201

Please sign in to comment.