-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-export-in-script-setup.js
72 lines (68 loc) · 2.14 KB
/
no-export-in-script-setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
/**
* @typedef {import('@typescript-eslint/types').TSESTree.ExportAllDeclaration} TSESTreeExportAllDeclaration
* @typedef {import('@typescript-eslint/types').TSESTree.ExportDefaultDeclaration} TSESTreeExportDefaultDeclaration
* @typedef {import('@typescript-eslint/types').TSESTree.ExportNamedDeclaration} TSESTreeExportNamedDeclaration
*/
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow `export` in `<script setup>`',
categories: ['vue3-essential', 'vue2-essential'],
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
* @param {SourceLocation} loc
*/
function verify(node, loc) {
const tsNode =
/** @type {TSESTreeExportAllDeclaration | TSESTreeExportDefaultDeclaration | TSESTreeExportNamedDeclaration} */ (
node
)
if (tsNode.exportKind === 'type') {
return
}
if (
tsNode.type === 'ExportNamedDeclaration' &&
tsNode.specifiers.length > 0 &&
tsNode.specifiers.every((spec) => spec.exportKind === 'type')
) {
return
}
context.report({
node,
loc,
messageId: 'forbidden'
})
}
return utils.defineScriptSetupVisitor(context, {
ExportAllDeclaration: (node) => verify(node, node.loc),
ExportDefaultDeclaration: (node) => verify(node, node.loc),
ExportNamedDeclaration: (node) => {
// export let foo = 'foo', export class Foo {}, export function foo() {}
if (node.declaration) {
verify(node, context.getSourceCode().getFirstToken(node).loc)
}
// export { foo }, export { foo } from 'bar'
else {
verify(node, node.loc)
}
}
})
}
}