-
-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
defineOptions
and defineSlots
to `vue/no-unsuppor…
…ted-features` rule (#2163)
- Loading branch information
Showing
6 changed files
with
258 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../../utils/index') | ||
|
||
module.exports = { | ||
supported: '>=3.3.0', | ||
/** @param {RuleContext} context @returns {RuleListener} */ | ||
createScriptVisitor(context) { | ||
const sourceCode = context.getSourceCode() | ||
return utils.defineScriptSetupVisitor(context, { | ||
onDefineOptionsEnter(node) { | ||
context.report({ | ||
node, | ||
messageId: 'forbiddenDefineOptions', | ||
fix(fixer) { | ||
return fix(fixer, node) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
/** | ||
* @param {RuleFixer} fixer | ||
* @param {CallExpression} node defineOptions() node | ||
*/ | ||
function fix(fixer, node) { | ||
if (node.arguments.length === 0) return null | ||
const scriptSetup = utils.getScriptSetupElement(context) | ||
if (!scriptSetup) return null | ||
if ( | ||
scriptSetup.parent.children | ||
.filter(utils.isVElement) | ||
.some( | ||
(node) => | ||
node.name === 'script' && !utils.hasAttribute(node, 'setup') | ||
) | ||
) { | ||
// has `<script>` | ||
return null | ||
} | ||
|
||
// Find defineOptions statement | ||
/** @type {ASTNode} */ | ||
let statement = node | ||
while (statement.parent && statement.parent.type !== 'Program') { | ||
statement = statement.parent | ||
} | ||
// Calc remove range | ||
/** @type {Range} */ | ||
const removeRange = [...statement.range] | ||
if ( | ||
sourceCode.lines[statement.loc.start.line - 1] | ||
.slice(0, statement.loc.start.column) | ||
.trim() === '' | ||
) { | ||
removeRange[0] -= statement.loc.start.column | ||
} | ||
|
||
return [ | ||
fixer.insertTextBefore( | ||
scriptSetup, | ||
`<script>\nexport default ${sourceCode.getText( | ||
node.arguments[0] | ||
)}\n</script>\n` | ||
), | ||
fixer.removeRange(removeRange) | ||
] | ||
} | ||
} | ||
} |
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,22 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../../utils/index') | ||
|
||
module.exports = { | ||
supported: '>=3.3.0', | ||
/** @param {RuleContext} context @returns {RuleListener} */ | ||
createScriptVisitor(context) { | ||
return utils.defineScriptSetupVisitor(context, { | ||
onDefineSlotsEnter(node) { | ||
context.report({ | ||
node, | ||
messageId: 'forbiddenDefineSlots' | ||
}) | ||
} | ||
}) | ||
} | ||
} |
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,88 @@ | ||
/** | ||
* @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('define-options', '^3.2.0') | ||
const tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { | ||
ecmaVersion: 2019, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
tester.run('no-unsupported-features/define-options', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
<script setup> | ||
defineOptions({}) | ||
</script>`, | ||
options: buildOptions({ version: '^3.3.0' }) | ||
}, | ||
{ | ||
code: ` | ||
<script setup> | ||
defineProps({}) | ||
</script>`, | ||
options: buildOptions() | ||
}, | ||
{ | ||
code: ` | ||
<script setup> | ||
defineOptions({}) | ||
</script>`, | ||
options: buildOptions({ version: '^3.0.0', ignores: ['define-options'] }) | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
<script setup> | ||
defineOptions({ name: 'Foo' }) | ||
</script>`, | ||
options: buildOptions(), | ||
output: ` | ||
<script> | ||
export default { name: 'Foo' } | ||
</script> | ||
<script setup> | ||
</script>`, | ||
errors: [ | ||
{ | ||
message: | ||
'`defineOptions()` macros are not supported until Vue.js "3.3.0".', | ||
line: 3 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
<script setup> | ||
defineOptions({}); | ||
</script>`, | ||
options: buildOptions(), | ||
output: ` | ||
<script> | ||
export default {} | ||
</script> | ||
<script setup> | ||
</script>`, | ||
errors: [ | ||
{ | ||
message: | ||
'`defineOptions()` macros are not supported until Vue.js "3.3.0".', | ||
line: 3 | ||
} | ||
] | ||
} | ||
] | ||
}) |
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,59 @@ | ||
/** | ||
* @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('define-slots', '^3.2.0') | ||
const tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { | ||
ecmaVersion: 2019 | ||
} | ||
}) | ||
|
||
tester.run('no-unsupported-features/define-slots', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
<script setup> | ||
const slots = defineSlots() | ||
</script>`, | ||
options: buildOptions({ version: '^3.3.0' }) | ||
}, | ||
{ | ||
code: ` | ||
<script setup> | ||
defineProps({}) | ||
</script>`, | ||
options: buildOptions() | ||
}, | ||
{ | ||
code: ` | ||
<script setup> | ||
const slots = defineSlots() | ||
</script>`, | ||
options: buildOptions({ version: '^3.0.0', ignores: ['define-slots'] }) | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
<script setup> | ||
const slots = defineSlots() | ||
</script>`, | ||
options: buildOptions(), | ||
errors: [ | ||
{ | ||
message: | ||
'`defineSlots()` macros are not supported until Vue.js "3.3.0".', | ||
line: 3 | ||
} | ||
] | ||
} | ||
] | ||
}) |