Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork689
feat: addvue/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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
87b05ba
feat: add no-import-compiler-macros rule
waynzhd692fdd
feat: add docs
waynzh8c4c5c6
Update lib/rules/no-import-compiler-macros.js
waynzhce38578
Update docs
waynzheb0d6d2
refactor
waynzh781692c
refactor: autofix
waynzhf539191
feat: add test
waynzh8b746bb
refactor
waynzh3c7654d
rename
waynzhFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletionsdocs/rules/index.md
This file contains hidden or 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
54 changes: 54 additions & 0 deletionsdocs/rules/no-import-compiler-macros.md
This file contains hidden or 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,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 | ||
FloEdelmann marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
- [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 deletionslib/index.js
This file contains hidden or 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
101 changes: 101 additions & 0 deletionslib/rules/no-import-compiler-macros.js
This file contains hidden or 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,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 deletionstests/lib/rules/no-import-compiler-macros.js
This file contains hidden or 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,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 | ||
} | ||
] | ||
} | ||
] | ||
}) |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.