-
-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
prefer-node-protocol
rule (#1203)
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
a7e393c
commit b1a5f53
Showing
16 changed files
with
451 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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,47 @@ | ||
# Prefer using the `node:` protocol when importing Node.js builtin modules | ||
|
||
When importing builtin modules, it's better to use the [`node:` protocol](https://nodejs.org/api/esm.html#esm_node_imports) as it makes it perfectly clear that the package is a Node.js builtin module. | ||
|
||
And don't forget to [upvote this issue](https://github.com/nodejs/node/issues/38343) if you agree. | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
import dgram from 'dgram'; | ||
``` | ||
|
||
```js | ||
export {strict as default} from 'assert'; | ||
``` | ||
|
||
```js | ||
import fs from 'fs/promises'; | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
import dgram from 'node:dgram'; | ||
``` | ||
|
||
```js | ||
export {strict as default} from 'node:assert'; | ||
``` | ||
|
||
```js | ||
import fs from 'node:fs/promises'; | ||
``` | ||
|
||
```js | ||
const fs = require('fs'); | ||
``` | ||
|
||
```js | ||
import _ from 'lodash'; | ||
``` | ||
|
||
```js | ||
import fs from './fs.js'; | ||
``` |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,53 @@ | ||
'use strict'; | ||
const isBuiltinModule = require('is-builtin-module'); | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
|
||
const MESSAGE_ID = 'prefer-node-protocol'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Prefer `node:{{moduleName}}` over `{{moduleName}}`.' | ||
}; | ||
|
||
const selector = [ | ||
':matches(ImportDeclaration, ExportNamedDeclaration, ImportExpression)', | ||
' > ', | ||
'Literal.source' | ||
].join(''); | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = context => { | ||
return { | ||
[selector](node) { | ||
const {value} = node; | ||
if ( | ||
typeof value !== 'string' || | ||
value.startsWith('node:') || | ||
!isBuiltinModule(value) | ||
) { | ||
return; | ||
} | ||
|
||
const firstCharacterIndex = node.range[0] + 1; | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID, | ||
data: {moduleName: value}, | ||
/** @param {import('eslint').Rule.RuleFixer} fixer */ | ||
fix: fixer => fixer.insertTextBeforeRange([firstCharacterIndex, firstCharacterIndex], 'node:') | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Prefer using the `node:` protocol when importing Node.js builtin modules.', | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
messages | ||
} | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,79 @@ | ||
import outdent from 'outdent'; | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
'import unicorn from "unicorn";', | ||
'import fs from "./fs";', | ||
'import fs from "unknown-builtin-module";', | ||
'const fs = require("fs");', | ||
'import fs from "node:fs";', | ||
outdent` | ||
async function foo() { | ||
const fs = await import(fs); | ||
} | ||
`, | ||
outdent` | ||
async function foo() { | ||
const fs = await import(0); | ||
} | ||
`, | ||
outdent` | ||
async function foo() { | ||
const fs = await import(\`fs\`); | ||
} | ||
` | ||
], | ||
invalid: [ | ||
'import fs from "fs";', | ||
'export {promises} from "fs";', | ||
outdent` | ||
async function foo() { | ||
const fs = await import('fs'); | ||
} | ||
`, | ||
'import fs from "fs/promises";', | ||
'export {default} from "fs/promises";', | ||
outdent` | ||
async function foo() { | ||
const fs = await import('fs/promises'); | ||
} | ||
`, | ||
'import {promises} from "fs";', | ||
'export {default as promises} from "fs";', | ||
'import {promises} from \'fs\';', | ||
outdent` | ||
async function foo() { | ||
const fs = await import("fs/promises"); | ||
} | ||
`, | ||
outdent` | ||
async function foo() { | ||
const fs = await import(/* escaped */"\\u{66}s/promises"); | ||
} | ||
`, | ||
'import "buffer";', | ||
'import "child_process";', | ||
'import "timers/promises";' | ||
] | ||
}); | ||
|
||
test.babel({ | ||
valid: [ | ||
'export fs from "node:fs";' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'export fs from "fs";', | ||
output: 'export fs from "node:fs";', | ||
errors: 1 | ||
}, | ||
{ | ||
code: 'await import(\'assert/strict\')', | ||
output: 'await import(\'node:assert/strict\')', | ||
errors: 1 | ||
} | ||
] | ||
}); |
Oops, something went wrong.