-
-
Notifications
You must be signed in to change notification settings - Fork 667
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update some rules to support style CSS vars injection (#1574)
* Update `vue/no-unused-properties` and `vue/script-setup-uses-vars` rule to support style CSS vars * Add testcase * upgrade vue-eslint-parser * update test * Update no-unsupported-features
- Loading branch information
Showing
11 changed files
with
333 additions
and
2 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
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,28 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const { getStyleVariablesContext } = require('../../utils/style-variables') | ||
|
||
module.exports = { | ||
supported: '>=3.0.3', | ||
/** @param {RuleContext} context @returns {TemplateListener} */ | ||
createScriptVisitor(context) { | ||
const styleVars = getStyleVariablesContext(context) | ||
if (!styleVars) { | ||
return {} | ||
} | ||
return { | ||
Program() { | ||
for (const vBind of styleVars.vBinds) { | ||
context.report({ | ||
node: vBind, | ||
messageId: 'forbiddenStyleCssVarsInjection' | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
} |
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,61 @@ | ||
const { isVElement } = require('..') | ||
|
||
module.exports = { | ||
getStyleVariablesContext | ||
} | ||
|
||
class StyleVariablesContext { | ||
/** | ||
* @param {RuleContext} context | ||
* @param {VElement[]} styles | ||
*/ | ||
constructor(context, styles) { | ||
this.context = context | ||
this.styles = styles | ||
/** @type {VReference[]} */ | ||
this.references = [] | ||
/** @type {VExpressionContainer[]} */ | ||
this.vBinds = [] | ||
for (const style of styles) { | ||
for (const node of style.children) { | ||
if (node.type === 'VExpressionContainer') { | ||
this.vBinds.push(node) | ||
for (const ref of node.references.filter( | ||
(ref) => ref.variable == null | ||
)) { | ||
this.references.push(ref) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** @type {Map<VElement, StyleVariablesContext} */ | ||
const cache = new Map() | ||
/** | ||
* Get the style vars context | ||
* @param {RuleContext} context | ||
* @returns {StyleVariablesContext | null} | ||
*/ | ||
function getStyleVariablesContext(context) { | ||
const df = | ||
context.parserServices.getDocumentFragment && | ||
context.parserServices.getDocumentFragment() | ||
if (!df) { | ||
return null | ||
} | ||
const styles = df.children | ||
.filter(isVElement) | ||
.filter((e) => e.name === 'style') | ||
if (!styles.length) { | ||
return null | ||
} | ||
let ctx = cache.get(styles[0]) | ||
if (ctx) { | ||
return ctx | ||
} | ||
ctx = new StyleVariablesContext(context, styles) | ||
cache.set(styles[0], ctx) | ||
return ctx | ||
} |
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
118 changes: 118 additions & 0 deletions
118
tests/lib/rules/no-unsupported-features/style-css-vars-injection.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../../lib/rules/no-unsupported-features') | ||
const utils = require('./utils') | ||
|
||
const buildOptions = utils.optionsBuilder('style-css-vars-injection', '^3.0.3') | ||
const tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { | ||
ecmaVersion: 2019, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
tester.run('no-unsupported-features/style-css-vars-injection', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
<template> | ||
<div class="text">hello</div> | ||
</template> | ||
<script> | ||
export default { | ||
data() { | ||
return { | ||
color: 'red', | ||
font: { | ||
size: '2em', | ||
}, | ||
} | ||
}, | ||
} | ||
</script> | ||
<style> | ||
.text { | ||
color: v-bind(color); | ||
/* expressions (wrap in quotes) */ | ||
font-size: v-bind('font.size'); | ||
} | ||
</style>`, | ||
options: buildOptions() | ||
}, | ||
{ | ||
code: ` | ||
<template> | ||
<div class="text">hello</div> | ||
</template> | ||
<script> | ||
</script> | ||
<style> | ||
.text { | ||
color: red; | ||
font-size: 2em; | ||
} | ||
</style>`, | ||
options: buildOptions({ version: '^2.6.0' }) | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
<template> | ||
<div class="text">hello</div> | ||
</template> | ||
<script> | ||
export default { | ||
data() { | ||
return { | ||
color: 'red', | ||
font: { | ||
size: '2em', | ||
}, | ||
} | ||
}, | ||
} | ||
</script> | ||
<style> | ||
.text { | ||
color: v-bind(color); | ||
/* expressions (wrap in quotes) */ | ||
font-size: v-bind('font.size'); | ||
} | ||
</style>`, | ||
options: buildOptions({ version: '^3.0.0' }), | ||
errors: [ | ||
{ | ||
message: | ||
'SFC CSS variable injection is not supported until Vue.js "3.0.3".', | ||
line: 21, | ||
column: 18, | ||
endLine: 21, | ||
endColumn: 31 | ||
}, | ||
{ | ||
message: | ||
'SFC CSS variable injection is not supported until Vue.js "3.0.3".', | ||
line: 24, | ||
column: 22, | ||
endLine: 24, | ||
endColumn: 41 | ||
} | ||
] | ||
} | ||
] | ||
}) |
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
Oops, something went wrong.