Skip to content
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

feat: add no-drafts-import rule #85

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
23 changes: 23 additions & 0 deletions docs/rules/no-drafts-import.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Disallow use of the drafts endpoint in primer/react (no-drafts-import)

🔧 The `--fix` option on the [ESLint CLI](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

The `/drafts` endpoint in `@primer/react` has been deprecated in favor of
`@primer/react/experimental`.

## Rule details

This rule disallows the use of importing from `@primer/react/drafts` as
`@primer/react/experimental` is now the preferred entrypoint.

👎 Examples of **incorrect** code for this rule:

```tsx
import {UnderlineNav} from '@primer/react/drafts'
```

👍 Examples of **correct** code for this rule:

```tsx
import {UnderlineNav} from '@primer/react/experimental'
```
41 changes: 41 additions & 0 deletions src/rules/__tests__/no-drafts-import.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const rule = require('../no-drafts-import')
const {RuleTester} = require('eslint')

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
ecmaFeatures: {
jsx: true

Check failure on line 11 in src/rules/__tests__/no-drafts-import.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
}

Check failure on line 12 in src/rules/__tests__/no-drafts-import.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
}

Check failure on line 13 in src/rules/__tests__/no-drafts-import.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
})

ruleTester.run('no-drafts-import', rule, {
valid: [
`import {UnderlineNav} from '@primer/react/experimental'`,
`import {TreeView, UnderlineNav} from '@primer/react/experimental'`

Check failure on line 19 in src/rules/__tests__/no-drafts-import.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
],
invalid: [
{
code: `import {UnderlineNav} from '@primer/react/drafts'`,
output: `import {UnderlineNav} from '@primer/react/experimental'`,
errors: [
{
messageId: 'entrypoint-error'

Check failure on line 27 in src/rules/__tests__/no-drafts-import.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
}

Check failure on line 28 in src/rules/__tests__/no-drafts-import.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
]

Check failure on line 29 in src/rules/__tests__/no-drafts-import.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
},
{
code: `import {TreeView, UnderlineNav} from '@primer/react/drafts'`,
output: `import {TreeView, UnderlineNav} from '@primer/react/experimental'`,
errors: [
{
messageId: 'entrypoint-error'

Check failure on line 36 in src/rules/__tests__/no-drafts-import.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
}

Check failure on line 37 in src/rules/__tests__/no-drafts-import.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
]

Check failure on line 38 in src/rules/__tests__/no-drafts-import.test.js

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
}
]
})
38 changes: 38 additions & 0 deletions src/rules/no-drafts-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
docs: {
description: '',
url: require('../url')(module)
},
type: 'suggestion',
hasSuggestions: true,
fixable: true,
schema: [],
messages: {
'entrypoint-error': 'The drafts entrypoint is deprecated. Use the experimental entrypoint instead.'
}
},
create(context) {
return {
ImportDeclaration(node) {
const source = node.source.value
if (source !== '@primer/react/drafts') {
return
}

context.report({
node,
messageId: 'entrypoint-error',
fix(fixer) {
return fixer.replaceText(node.source, `'@primer/react/experimental'`)
}
})
}
}
}
}
Loading