Skip to content

Commit

Permalink
[Tests] no-nodejs-modules: add tests for node protocol URL
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki authored and ljharb committed Jan 22, 2022
1 parent 9e4c9a9 commit 1cb0dc7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## [Unreleased]

### Changed
- [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki])

## [2.25.4] - 2022-01-02

### Fixed
Expand Down Expand Up @@ -955,6 +958,7 @@ for info on changes for earlier releases.

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

[#2367]: https://github.com/import-js/eslint-plugin-import/pull/2367
[#2341]: https://github.com/import-js/eslint-plugin-import/pull/2341
[#2334]: https://github.com/import-js/eslint-plugin-import/pull/2334
[#2305]: https://github.com/import-js/eslint-plugin-import/pull/2305
Expand Down Expand Up @@ -1618,6 +1622,7 @@ for info on changes for earlier releases.
[@skyrpex]: https://github.com/skyrpex
[@sompylasar]: https://github.com/sompylasar
[@soryy708]: https://github.com/soryy708
[@sosukesuzuki]: https://github.com/sosukesuzuki
[@spalger]: https://github.com/spalger
[@st-sloth]: https://github.com/st-sloth
[@stekycz]: https://github.com/stekycz
Expand Down Expand Up @@ -1646,4 +1651,4 @@ for info on changes for earlier releases.
[@wtgtybhertgeghgtwtg]: https://github.com/wtgtybhertgeghgtwtg
[@xpl]: https://github.com/xpl
[@yordis]: https://github.com/yordis
[@zloirock]: https://github.com/zloirock
[@zloirock]: https://github.com/zloirock
70 changes: 66 additions & 4 deletions tests/src/rules/no-nodejs-modules.js
@@ -1,6 +1,7 @@
import { test } from '../utils';

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

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

ruleTester.run('no-nodejs-modules', rule, {
valid: [
valid: [].concat(
test({ code: 'import _ from "lodash"' }),
test({ code: 'import find from "lodash.find"' }),
test({ code: 'import foo from "./foo"' }),
Expand Down Expand Up @@ -55,8 +56,42 @@ ruleTester.run('no-nodejs-modules', rule, {
allow: ['path', 'events'],
}],
}),
],
invalid: [
isCore('node:events') ? [
test({
code: 'import events from "node:events"',
options: [{
allow: ['node:events'],
}],
}),
test({
code: 'var events = require("node:events")',
options: [{
allow: ['node:events'],
}],
}),
]: [],
isCore('node:path') ? [
test({
code: 'import path from "node:path"',
options: [{
allow: ['node:path'],
}],
}),
test({
code: 'var path = require("node:path")',
options: [{
allow: ['node:path'],
}],
}),
] : [],
isCore('node:path') && isCore('node:events') ? test({
code: 'import path from "node:path";import events from "node:events"',
options: [{
allow: ['node:path', 'node:events'],
}],
}) : [],
),
invalid: [].concat(
test({
code: 'import path from "path"',
errors: [error('Do not import Node.js builtin module "path"')],
Expand All @@ -80,5 +115,32 @@ ruleTester.run('no-nodejs-modules', rule, {
}],
errors: [error('Do not import Node.js builtin module "fs"')],
}),
],
isCore('node:path') ? [
test({
code: 'import path from "node:path"',
errors: [error('Do not import Node.js builtin module "node:path"')],
}),
test({
code: 'var path = require("node:path")',
errors: [error('Do not import Node.js builtin module "node:path"')],
}),
] : [],
isCore('node:fs') ? [
test({
code: 'import fs from "node:fs"',
errors: [error('Do not import Node.js builtin module "node:fs"')],
}),
test({
code: 'var fs = require("node:fs")',
errors: [error('Do not import Node.js builtin module "node:fs"')],
}),
test({
code: 'import fs from "node:fs"',
options: [{
allow: ['node:path'],
}],
errors: [error('Do not import Node.js builtin module "node:fs"')],
}),
] : [],
),
});

0 comments on commit 1cb0dc7

Please sign in to comment.