Skip to content

Commit

Permalink
import-style: Support node: protocol (#2297)
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian authored Apr 3, 2024
1 parent 677af7f commit c28a086
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 6 deletions.
6 changes: 6 additions & 0 deletions rules/import-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,15 @@ const defaultStyles = {
path: {
default: true,
},
'node:path': {
default: true,
},
util: {
named: true,
},
'node:util': {
named: true,
},
};

/** @param {import('eslint').Rule.RuleContext} context */
Expand Down
45 changes: 45 additions & 0 deletions test/import-style.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,18 @@ test({
code: 'import {inspect} from \'util\'',
options: [],
},
{
code: 'import {inspect} from \'node:util\'',
options: [],
},
{
code: 'const {inspect} = require(\'util\')',
options: [],
},
{
code: 'const {inspect} = require(\'node:util\')',
options: [],
},
{
code: 'import chalk from \'chalk\'',
options: [],
Expand All @@ -137,10 +145,18 @@ test({
code: 'const {inspect} = require(\'util\')',
options: [],
},
{
code: 'const {inspect} = require(\'node:util\')',
options: [],
},
{
code: 'export {promisify, callbackify} from \'util\'',
options: [],
},
{
code: 'export {promisify, callbackify} from \'node:util\'',
options: [],
},

{
code: 'require(\'chalk\')',
Expand Down Expand Up @@ -538,26 +554,51 @@ test({
options: [],
errors: [{}],
},
{
code: 'import util from \'node:util\'',
options: [],
errors: [{}],
},
{
code: 'import * as util from \'util\'',
options: [],
errors: [{}],
},
{
code: 'import * as util from \'node:util\'',
options: [],
errors: [{}],
},
{
code: 'const util = require(\'util\')',
options: [],
errors: [{}],
},
{
code: 'const util = require(\'node:util\')',
options: [],
errors: [{}],
},
{
code: 'require(\'util\')',
options: [],
errors: [{}],
},
{
code: 'require(\'node:util\')',
options: [],
errors: [{}],
},
{
code: 'require(\'ut\' + \'il\')',
options: [],
errors: [{}],
},
{
code: 'require(\'node:\' + \'util\')',
options: [],
errors: [{}],
},
{
code: 'import {red} from \'chalk\'',
options: [],
Expand Down Expand Up @@ -626,9 +667,13 @@ test.snapshot({
],
invalid: [
'import util from \'util\'',
'import util from \'node:util\'',
'import * as util from \'util\'',
'import * as util from \'node:util\'',
'const util = require(\'util\')',
'const util = require(\'node:util\')',
'require(\'util\')',
'require(\'node:util\')',
'import {red} from \'chalk\'',
'import {red as green} from \'chalk\'',
outdent`
Expand Down
72 changes: 66 additions & 6 deletions test/snapshots/import-style.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,22 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^^^ Use named import for module \`util\`.␊
`

## invalid(2): import * as util from 'util'
## invalid(2): import util from 'node:util'

> Input
`␊
1 | import util from 'node:util'␊
`

> Error 1/1
`␊
> 1 | import util from 'node:util'␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use named import for module \`node:util\`.␊
`

## invalid(3): import * as util from 'util'

> Input
Expand All @@ -34,7 +49,22 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use named import for module \`util\`.␊
`

## invalid(3): const util = require('util')
## invalid(4): import * as util from 'node:util'

> Input
`␊
1 | import * as util from 'node:util'␊
`

> Error 1/1
`␊
> 1 | import * as util from 'node:util'␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use named import for module \`node:util\`.␊
`

## invalid(5): const util = require('util')

> Input
Expand All @@ -49,7 +79,22 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^^ Use named import for module \`util\`.␊
`

## invalid(4): require('util')
## invalid(6): const util = require('node:util')

> Input
`␊
1 | const util = require('node:util')␊
`

> Error 1/1
`␊
> 1 | const util = require('node:util')␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use named import for module \`node:util\`.␊
`

## invalid(7): require('util')

> Input
Expand All @@ -64,7 +109,22 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^ Use named import for module \`util\`.␊
`

## invalid(5): import {red} from 'chalk'
## invalid(8): require('node:util')

> Input
`␊
1 | require('node:util')␊
`

> Error 1/1
`␊
> 1 | require('node:util')␊
| ^^^^^^^^^^^^^^^^^^^^ Use named import for module \`node:util\`.␊
`

## invalid(9): import {red} from 'chalk'

> Input
Expand All @@ -79,7 +139,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^^^^^ Use default import for module \`chalk\`.␊
`

## invalid(6): import {red as green} from 'chalk'
## invalid(10): import {red as green} from 'chalk'

> Input
Expand All @@ -94,7 +154,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use default import for module \`chalk\`.␊
`

## invalid(7): async () => { const {red} = await import('chalk'); }
## invalid(11): async () => { const {red} = await import('chalk'); }

> Input
Expand Down
Binary file modified test/snapshots/import-style.mjs.snap
Binary file not shown.

0 comments on commit c28a086

Please sign in to comment.