From c28a0861bfa981013abb11e36117a8d90c33a048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E6=98=8E=E5=81=A5?= Date: Wed, 3 Apr 2024 17:41:44 +0800 Subject: [PATCH] `import-style`: Support `node:` protocol (#2297) --- rules/import-style.js | 6 +++ test/import-style.mjs | 45 +++++++++++++++++ test/snapshots/import-style.mjs.md | 72 ++++++++++++++++++++++++--- test/snapshots/import-style.mjs.snap | Bin 521 -> 631 bytes 4 files changed, 117 insertions(+), 6 deletions(-) diff --git a/rules/import-style.js b/rules/import-style.js index 5e58261893..6db6674db4 100644 --- a/rules/import-style.js +++ b/rules/import-style.js @@ -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 */ diff --git a/test/import-style.mjs b/test/import-style.mjs index d057fc49e1..cd0a821c1b 100644 --- a/test/import-style.mjs +++ b/test/import-style.mjs @@ -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: [], @@ -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\')', @@ -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: [], @@ -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` diff --git a/test/snapshots/import-style.mjs.md b/test/snapshots/import-style.mjs.md index 8e3000c24a..997dd9c33c 100644 --- a/test/snapshots/import-style.mjs.md +++ b/test/snapshots/import-style.mjs.md @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/snapshots/import-style.mjs.snap b/test/snapshots/import-style.mjs.snap index 2111ea007e2f7b8dc5486f7333ff46407a0ee472..85a935ced4ccdd65acc808171b309b17a44835db 100644 GIT binary patch literal 631 zcmV--0*L)VRzV)<3adAb7in%@lbbQU%{DF}Q@O(u_;5mX z7Po6g8y|}Z00000000BEmN9P=F&KtZ5JJ6;i2)=&1Xb@Mm5>x@2}&dehK>vfCWxDJ zh!w{UzPmzc!oY;YPhg@WzY<;J%jIi)wxgyuWx2Buh0wA^-`xRsaCN6h4|Cc$ZKRWK84Y{ee_4mT|@@^m0J&iM|4R zqm)$8Y&47bVG+M&j4xzN+Z7I4D8l1P2PMNK;s>HkDn%?&tTsH9EKojP_De5yAc;pe ztX_-^lb2IO8DTGG%2mBMleQc_2mF1itJwOrTK+W-|ceNYR;zX^E_%pUy4L` zxdV!R$(W+yd}fhveQXT>`dBG*IrHZ_>+_;gNjWKl7ju1_ApiQfQEo#Us?WW`)j`yD zH)aRLxvR~1l;jh7@vZh??!_jn07bmR<#^WV*U?q)U(VmOXcvg3{tg7U&=`| z6oKn|Er`KT=>%yaBtoqQ*v_}xB97QF?x`fcdkzAYu#?@D+WT6*mfqFYmOeUwk8IX) zhf8+OB|naaX<`710GQJ$1pq*XU-pF`F-OD(JSfhGN@JjcRC1iBh8&8Nb;HTu^cN5&6b&>J5ig;fpLvX8kFfz}s z+hekCnSeU!cY{vAG{6@5f*^YI`CAZk-u|nGfQKxViA}^SrNqQuVPc<}$mj64<~*9G z$>!#Pux_T^%wo;|AQofmu(0*jYk|erQ5UX|S4YS}OJM)9=!$p!oc L)fZ4@U$R