-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
vue/no-export-in-script-setup
rule (#1559)
* Add `vue/no-export-in-script-setup` rule * update
- Loading branch information
Showing
7 changed files
with
208 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-export-in-script-setup | ||
description: disallow `export` in `<script setup>` | ||
--- | ||
# vue/no-export-in-script-setup | ||
|
||
> disallow `export` in `<script setup>` | ||
- :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 warns ES module exports in `<script setup>`. | ||
|
||
The previous version of `<script setup>` RFC used `export` to define variables used in templates, but the new `<script setup>` RFC has been updated to define without using `export`. | ||
See [Vue RFCs - 0040-script-setup] for more details. | ||
|
||
<eslint-code-block :rules="{'vue/no-export-in-script-setup': ['error']}"> | ||
|
||
```vue | ||
<script setup> | ||
/* ✓ GOOD */ | ||
let msg = 'Hello!' | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-export-in-script-setup': ['error']}"> | ||
|
||
```vue | ||
<script setup> | ||
/* ✗ BAD */ | ||
export let msg = 'Hello!' | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further Reading | ||
|
||
- [Vue RFCs - 0040-script-setup] | ||
|
||
[Vue RFCs - 0040-script-setup]: https://github.com/vuejs/rfcs/blob/master/active-rfcs/0040-script-setup.md | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-export-in-script-setup.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-export-in-script-setup.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,41 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow `export` in `<script setup>`', | ||
// TODO Switch in the major version. | ||
// categories: ['vue3-essential'], | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-export-in-script-setup.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
forbidden: '`<script setup>` cannot contain ES module exports.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
/** @param {ExportAllDeclaration | ExportDefaultDeclaration | ExportNamedDeclaration} node */ | ||
function report(node) { | ||
context.report({ | ||
node, | ||
messageId: 'forbidden' | ||
}) | ||
} | ||
|
||
return utils.defineScriptSetupVisitor(context, { | ||
ExportAllDeclaration: report, | ||
ExportDefaultDeclaration: report, | ||
ExportNamedDeclaration: report | ||
}) | ||
} | ||
} |
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,107 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const eslint = require('eslint') | ||
const rule = require('../../../lib/rules/no-export-in-script-setup') | ||
|
||
const RuleTester = eslint.RuleTester | ||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
ruleTester.run('no-export-in-script-setup', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
export * from 'foo' | ||
export default {} | ||
export class A {} | ||
</script> | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
export * from 'foo' | ||
export default {} | ||
export class A {} | ||
</script> | ||
<script setup> | ||
let foo; | ||
</script> | ||
` | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script setup> | ||
export * from 'foo' | ||
export default {} | ||
export class A {} | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
message: '`<script setup>` cannot contain ES module exports.', | ||
line: 3 | ||
}, | ||
{ | ||
message: '`<script setup>` cannot contain ES module exports.', | ||
line: 4 | ||
}, | ||
{ | ||
message: '`<script setup>` cannot contain ES module exports.', | ||
line: 5 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script> | ||
let foo; | ||
</script> | ||
<script setup> | ||
export * from 'foo' | ||
export default {} | ||
export class A {} | ||
</script> | ||
`, | ||
errors: [ | ||
{ | ||
message: '`<script setup>` cannot contain ES module exports.', | ||
line: 6 | ||
}, | ||
{ | ||
message: '`<script setup>` cannot contain ES module exports.', | ||
line: 7 | ||
}, | ||
{ | ||
message: '`<script setup>` cannot contain ES module exports.', | ||
line: 8 | ||
} | ||
] | ||
} | ||
] | ||
}) |