Skip to content

Commit cd3469f

Browse files
committed
update
1 parent f1e1ddb commit cd3469f

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

lib/rules/no-import-compiler-macros.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,19 @@ module.exports = {
3737
schema: [],
3838
messages: {
3939
noImportCompilerMacros:
40-
"'{{name}}' is a compiler macro and doesn't need to be imported."
40+
"'{{name}}' is a compiler macro and doesn't need to be imported.",
41+
onlyValidInScriptSetup:
42+
"'{{name}}' is a compiler macro and can only be used inside <script setup>."
4143
}
4244
},
4345
/**
4446
* @param {RuleContext} context
4547
* @returns {RuleListener}
4648
*/
4749
create(context) {
48-
const scriptSetup = utils.getScriptSetupElement(context)
49-
if (!scriptSetup) {
50-
return {}
51-
}
5250
const sourceCode = context.getSourceCode()
5351

54-
return utils.defineScriptSetupVisitor(context, {
52+
return {
5553
ImportDeclaration(node) {
5654
if (node.specifiers.length === 0 || !VUE_MODULES.has(node.source.value))
5755
return
@@ -66,7 +64,9 @@ module.exports = {
6664

6765
context.report({
6866
node: specifier,
69-
messageId: 'noImportCompilerMacros',
67+
messageId: utils.isScriptSetup(context)
68+
? 'noImportCompilerMacros'
69+
: 'onlyValidInScriptSetup',
7070
data: {
7171
name: specifier.imported.name
7272
},
@@ -102,6 +102,6 @@ module.exports = {
102102
})
103103
}
104104
}
105-
})
105+
}
106106
}
107107
}

tests/lib/rules/no-import-compiler-macros.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ tester.run('no-import-compiler-macros', rule, {
3333
import { defineProps } from 'some-other-package'
3434
</script>
3535
`
36-
},
37-
{
38-
filename: 'test.vue',
39-
code: `
40-
<script>
41-
// not in <script setup>
42-
import { defineProps } from 'vue'
43-
</script>
44-
`
4536
}
4637
],
4738
invalid: [
@@ -223,6 +214,27 @@ tester.run('no-import-compiler-macros', rule, {
223214
endColumn: 60
224215
}
225216
]
217+
},
218+
{
219+
filename: 'test.ts',
220+
code: `
221+
import { defineProps } from 'vue'
222+
`,
223+
output: `
224+
225+
`,
226+
errors: [
227+
{
228+
messageId: 'onlyValidInScriptSetup',
229+
data: {
230+
name: 'defineProps'
231+
},
232+
line: 2,
233+
column: 16,
234+
endLine: 2,
235+
endColumn: 27
236+
}
237+
]
226238
}
227239
]
228240
})

0 commit comments

Comments
 (0)