11import { test } from '../utils' ;
22
33import { RuleTester } from 'eslint' ;
4+ const isCore = require ( 'is-core-module' ) ;
45
56const ruleTester = new RuleTester ( ) ;
67const rule = require ( 'rules/no-nodejs-modules' ) ;
@@ -10,7 +11,7 @@ const error = message => ({
1011} ) ;
1112
1213ruleTester . 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