Skip to content

Add no-restricted-imports rule #1792

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

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ module.exports = {
'no-proto': 2,
'no-redeclare': 2,
'no-regex-spaces': 2,
'no-restricted-imports': [
'error',
{
paths: [
'@vue/reactivity',
'@vue/runtime-core',
'@vue/runtime-dom',
'@vue/shared'
],
message: "Please always import these APIs from the main 'vue' package"
}
],
'no-return-assign': [2, 'except-parens'],
'no-self-assign': 2,
'no-self-compare': 2,
Expand Down
42 changes: 42 additions & 0 deletions docs/rules/no-restricted-imports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-restricted-imports
description: disallow imports from some `'@vue/*'` packages
---
# vue/no-restricted-imports

> disallow imports from some `'@vue/*'` packages

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

## :book: Further Reading

- [no-restricted-imports]

[no-restricted-imports]: https://eslint.org/docs/rules/no-restricted-imports

<eslint-code-block :rules="{'vue/no-restricted-imports': ['error']}">

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

// ✗ BAD
import { ref } from '@vue/reactivity';
</script>
```

</eslint-code-block>

## :rocket: Version

This rule was introduced in eslint-plugin-vue v*.0.0

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-restricted-imports.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-restricted-imports.js)

<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-restricted-imports)</sup>
12 changes: 12 additions & 0 deletions lib/rules/no-restricted-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @author Jackson Hammond
* See LICENSE file in root directory for full license.
*/
'use strict'

const { wrapCoreRule } = require('../utils')

// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
module.exports = wrapCoreRule('no-restricted-imports', {
applyDocument: true
})
57 changes: 57 additions & 0 deletions tests/lib/rules/no-restricted-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @author Jackson Hammond
* See LICENSE file in root directory for full license.
*/
'use strict'

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/no-restricted-imports')

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

tester.run('no-transitive-dependency-imports', rule, {
valid: [
{
filename: 'test.vue',
code: `
import { ref } from 'vue'
`
},
{
filename: 'test.vue',
code: `
import { ref, computed } from 'vue'
`
},
{
filename: 'test.vue',
code: `
import {
ref
} from 'vue'
`
},
{
filename: 'test.vue',
code: `
import {
ref,
computed
} from 'vue'
`
}
],
invalid: [
{
filename: 'test.vue',
code: `import { computed } from '@vue/reactivity'`,
errors: ["Please always import these APIs from the main 'vue' package"]
}
]
})