Skip to content

feat: add vue/no-import-compiler-macros rule #2684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ For example:
| [vue/no-deprecated-model-definition] | disallow deprecated `model` definition (in Vue.js 3.0.0+) | :bulb: | :warning: |
| [vue/no-duplicate-attr-inheritance] | enforce `inheritAttrs` to be set to `false` when using `v-bind="$attrs"` | | :hammer: |
| [vue/no-empty-component-block] | disallow the `<template>` `<script>` `<style>` block to be empty | :wrench: | :hammer: |
| [vue/no-import-compiler-macros] | disallow importing Vue compiler macros | :wrench: | :warning: |
| [vue/no-multiple-objects-in-class] | disallow passing multiple objects in an array to class | | :hammer: |
| [vue/no-potential-component-option-typo] | disallow a potential typo in your component property | :bulb: | :hammer: |
| [vue/no-ref-object-reactivity-loss] | disallow usages of ref objects that can lead to loss of reactivity | | :warning: |
Expand Down Expand Up @@ -467,6 +468,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
[vue/no-expose-after-await]: ./no-expose-after-await.md
[vue/no-extra-parens]: ./no-extra-parens.md
[vue/no-implicit-coercion]: ./no-implicit-coercion.md
[vue/no-import-compiler-macros]: ./no-import-compiler-macros.md
[vue/no-invalid-model-keys]: ./no-invalid-model-keys.md
[vue/no-irregular-whitespace]: ./no-irregular-whitespace.md
[vue/no-lifecycle-after-await]: ./no-lifecycle-after-await.md
Expand Down
54 changes: 54 additions & 0 deletions docs/rules/no-import-compiler-macros.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-import-compiler-macros
description: disallow importing Vue compiler macros
---

# vue/no-import-compiler-macros

> disallow importing Vue compiler macros

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> _**This rule has not been released yet.**_ </badge>
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fix-problems) can automatically fix some of the problems reported by this rule.

## :book: Rule Details

This rule disallow importing vue compiler macros.

<eslint-code-block fix :rules="{'vue/no-import-compiler-macros': ['error']}">

```vue
<script setup>
/* ✗ BAD */
import { defineProps, withDefaults } from 'vue'
</script>
```

</eslint-code-block>

<eslint-code-block fix :rules="{'vue/no-import-compiler-macros': ['error']}">

```vue
<script setup>
/* ✓ GOOD */
import { ref } from 'vue'
</script>
```

</eslint-code-block>

## :wrench: Options

Nothing.

## :books: Further Reading

- [defineProps() & defineEmits()]

[defineProps() & defineEmits()]: https://vuejs.org/api/sfc-script-setup.html#defineprops-defineemits

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-import-compiler-macros.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-import-compiler-macros.js)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
'no-expose-after-await': require('./rules/no-expose-after-await'),
'no-extra-parens': require('./rules/no-extra-parens'),
'no-implicit-coercion': require('./rules/no-implicit-coercion'),
'no-import-compiler-macros': require('./rules/no-import-compiler-macros'),
'no-invalid-model-keys': require('./rules/no-invalid-model-keys'),
'no-irregular-whitespace': require('./rules/no-irregular-whitespace'),
'no-lifecycle-after-await': require('./rules/no-lifecycle-after-await'),
Expand Down Expand Up @@ -285,7 +286,7 @@
vue: require('./processor')
},
environments: {
// TODO Remove in the next major version

Check warning on line 289 in lib/index.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO Remove in the next major version'
/** @deprecated */
'setup-compiler-macros': {
globals: {
Expand Down
101 changes: 101 additions & 0 deletions lib/rules/no-import-compiler-macros.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @author Wayne Zhang
* See LICENSE file in root directory for full license.
*/
'use strict'

const COMPILER_MACROS = new Set([
'defineProps',
'defineEmits',
'defineExpose',
'withDefaults',
'defineModel',
'defineOptions',
'defineSlots'
])

const VUE_MODULES = new Set(['@vue/runtime-core', '@vue/runtime-dom', 'vue'])

/**
* @param {Token} node
*/
function isComma(node) {
return node.type === 'Punctuator' && node.value === ','
}

module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow importing Vue compiler macros',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-import-compiler-macros.html'
},
fixable: 'code',
schema: [],
messages: {
noImportCompilerMacros:
"'{{name}}' is a compiler macro and doesn't need to be imported."
}
},
/**
* @param {RuleContext} context
* @returns {RuleListener}
*/
create(context) {
const sourceCode = context.getSourceCode()

return {
ImportDeclaration(node) {
if (node.specifiers.length === 0 || !VUE_MODULES.has(node.source.value))
return

for (const specifier of node.specifiers) {
if (
specifier.type !== 'ImportSpecifier' ||
!COMPILER_MACROS.has(specifier.imported.name)
) {
continue
}

context.report({
node: specifier,
messageId: 'noImportCompilerMacros',
data: {
name: specifier.imported.name
},
fix: (fixer) => {
const isOnlySpecifier = node.specifiers.length === 1
const isLastSpecifier =
specifier === node.specifiers[node.specifiers.length - 1]

if (isOnlySpecifier) {
return fixer.remove(node)
} else if (isLastSpecifier) {
const precedingComma = sourceCode.getTokenBefore(
specifier,
isComma
)
return fixer.removeRange([
precedingComma ? precedingComma.range[0] : specifier.range[0],
specifier.range[1]
])
} else {
const subsequentComma = sourceCode.getTokenAfter(
specifier,
isComma
)
return fixer.removeRange([
specifier.range[0],
subsequentComma
? subsequentComma.range[1]
: specifier.range[1]
])
}
}
})
}
}
}
}
}
201 changes: 201 additions & 0 deletions tests/lib/rules/no-import-compiler-macros.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/**
* @author Wayne Zhang
* See LICENSE file in root directory for full license.
*/
'use strict'

const RuleTester = require('../../eslint-compat').RuleTester
const rule = require('../../../lib/rules/no-import-compiler-macros')

const tester = new RuleTester({
languageOptions: {
parser: require('vue-eslint-parser'),
ecmaVersion: 2020,
sourceType: 'module'
}
})

tester.run('no-import-compiler-macros', rule, {
valid: [
{
filename: 'test.vue',
code: `
<script setup>
import { ref, computed } from 'vue'
import { someFunction } from '@vue/runtime-core'
</script>
`
},
{
filename: 'test.vue',
code: `
<script>
import { defineProps } from 'some-other-package'
</script>
`
}
],
invalid: [
{
filename: 'test.vue',
code: `
<script setup>
import { defineProps } from 'vue'
</script>
`,
output: `
<script setup>

</script>
`,
errors: [
{
messageId: 'noImportCompilerMacros',
data: {
name: 'defineProps'
},
line: 3,
column: 16
}
]
},
{
filename: 'test.vue',
code: `
<script setup>
import {
ref,
defineProps
} from 'vue'
</script>
`,
output: `
<script setup>
import {
ref
} from 'vue'
</script>
`,
errors: [
{
messageId: 'noImportCompilerMacros',
data: {
name: 'defineProps'
},
line: 5,
column: 9
}
]
},
{
filename: 'test.vue',
code: `
<script setup>
import { ref, defineProps } from 'vue'
import { defineEmits, computed } from '@vue/runtime-core'
import { defineExpose, watch, withDefaults } from '@vue/runtime-dom'
</script>
`,
output: `
<script setup>
import { ref } from 'vue'
import { computed } from '@vue/runtime-core'
import { watch } from '@vue/runtime-dom'
</script>
`,
errors: [
{
messageId: 'noImportCompilerMacros',
data: {
name: 'defineProps'
},
line: 3,
column: 21
},
{
messageId: 'noImportCompilerMacros',
data: {
name: 'defineEmits'
},
line: 4,
column: 16
},
{
messageId: 'noImportCompilerMacros',
data: {
name: 'defineExpose'
},
line: 5,
column: 16
},
{
messageId: 'noImportCompilerMacros',
data: {
name: 'withDefaults'
},
line: 5,
column: 37
}
]
},
{
filename: 'test.vue',
code: `
<script setup>
import { defineModel, defineOptions } from 'vue'
</script>
`,
output: `
<script setup>
import { defineOptions } from 'vue'
</script>
`,
errors: [
{
messageId: 'noImportCompilerMacros',
data: {
name: 'defineModel'
},
line: 3,
column: 16
},
{
messageId: 'noImportCompilerMacros',
data: {
name: 'defineOptions'
},
line: 3,
column: 29
}
]
},
{
filename: 'test.vue',
code: `
<script setup lang="ts">
import { ref as refFoo, defineSlots as defineSlotsFoo, type computed } from '@vue/runtime-core'
</script>
`,
output: `
<script setup lang="ts">
import { ref as refFoo, type computed } from '@vue/runtime-core'
</script>
`,
languageOptions: {
parserOptions: {
parser: require.resolve('@typescript-eslint/parser')
}
},
errors: [
{
messageId: 'noImportCompilerMacros',
data: {
name: 'defineSlots'
},
line: 3,
column: 31
}
]
}
]
})
Loading