Skip to content

Commit 1cb0dc7

Browse files
sosukesuzukiljharb
authored andcommitted
[Tests] no-nodejs-modules: add tests for node protocol URL
1 parent 9e4c9a9 commit 1cb0dc7

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
66

77
## [Unreleased]
88

9+
### Changed
10+
- [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki])
11+
912
## [2.25.4] - 2022-01-02
1013

1114
### Fixed
@@ -955,6 +958,7 @@ for info on changes for earlier releases.
955958

956959
[`memo-parser`]: ./memo-parser/README.md
957960

961+
[#2367]: https://github.com/import-js/eslint-plugin-import/pull/2367
958962
[#2341]: https://github.com/import-js/eslint-plugin-import/pull/2341
959963
[#2334]: https://github.com/import-js/eslint-plugin-import/pull/2334
960964
[#2305]: https://github.com/import-js/eslint-plugin-import/pull/2305
@@ -1618,6 +1622,7 @@ for info on changes for earlier releases.
16181622
[@skyrpex]: https://github.com/skyrpex
16191623
[@sompylasar]: https://github.com/sompylasar
16201624
[@soryy708]: https://github.com/soryy708
1625+
[@sosukesuzuki]: https://github.com/sosukesuzuki
16211626
[@spalger]: https://github.com/spalger
16221627
[@st-sloth]: https://github.com/st-sloth
16231628
[@stekycz]: https://github.com/stekycz
@@ -1646,4 +1651,4 @@ for info on changes for earlier releases.
16461651
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
16471652
[@xpl]: https://github.com/xpl
16481653
[@yordis]: https://github.com/yordis
1649-
[@zloirock]: https://github.com/zloirock
1654+
[@zloirock]: https://github.com/zloirock

tests/src/rules/no-nodejs-modules.js

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { test } from '../utils';
22

33
import { RuleTester } from 'eslint';
4+
const isCore = require('is-core-module');
45

56
const ruleTester = new RuleTester();
67
const rule = require('rules/no-nodejs-modules');
@@ -10,7 +11,7 @@ const error = message => ({
1011
});
1112

1213
ruleTester.run('no-nodejs-modules', rule, {
13-
valid: [
14+
valid: [].concat(
1415
test({ code: 'import _ from "lodash"' }),
1516
test({ code: 'import find from "lodash.find"' }),
1617
test({ code: 'import foo from "./foo"' }),
@@ -55,8 +56,42 @@ ruleTester.run('no-nodejs-modules', rule, {
5556
allow: ['path', 'events'],
5657
}],
5758
}),
58-
],
59-
invalid: [
59+
isCore('node:events') ? [
60+
test({
61+
code: 'import events from "node:events"',
62+
options: [{
63+
allow: ['node:events'],
64+
}],
65+
}),
66+
test({
67+
code: 'var events = require("node:events")',
68+
options: [{
69+
allow: ['node:events'],
70+
}],
71+
}),
72+
]: [],
73+
isCore('node:path') ? [
74+
test({
75+
code: 'import path from "node:path"',
76+
options: [{
77+
allow: ['node:path'],
78+
}],
79+
}),
80+
test({
81+
code: 'var path = require("node:path")',
82+
options: [{
83+
allow: ['node:path'],
84+
}],
85+
}),
86+
] : [],
87+
isCore('node:path') && isCore('node:events') ? test({
88+
code: 'import path from "node:path";import events from "node:events"',
89+
options: [{
90+
allow: ['node:path', 'node:events'],
91+
}],
92+
}) : [],
93+
),
94+
invalid: [].concat(
6095
test({
6196
code: 'import path from "path"',
6297
errors: [error('Do not import Node.js builtin module "path"')],
@@ -80,5 +115,32 @@ ruleTester.run('no-nodejs-modules', rule, {
80115
}],
81116
errors: [error('Do not import Node.js builtin module "fs"')],
82117
}),
83-
],
118+
isCore('node:path') ? [
119+
test({
120+
code: 'import path from "node:path"',
121+
errors: [error('Do not import Node.js builtin module "node:path"')],
122+
}),
123+
test({
124+
code: 'var path = require("node:path")',
125+
errors: [error('Do not import Node.js builtin module "node:path"')],
126+
}),
127+
] : [],
128+
isCore('node:fs') ? [
129+
test({
130+
code: 'import fs from "node:fs"',
131+
errors: [error('Do not import Node.js builtin module "node:fs"')],
132+
}),
133+
test({
134+
code: 'var fs = require("node:fs")',
135+
errors: [error('Do not import Node.js builtin module "node:fs"')],
136+
}),
137+
test({
138+
code: 'import fs from "node:fs"',
139+
options: [{
140+
allow: ['node:path'],
141+
}],
142+
errors: [error('Do not import Node.js builtin module "node:fs"')],
143+
}),
144+
] : [],
145+
),
84146
});

0 commit comments

Comments
 (0)