From 8541bb841028569118dbf5b27215e6a4fd8e165f Mon Sep 17 00:00:00 2001 From: Isaiah Smith Date: Wed, 31 Jan 2024 09:53:31 -0800 Subject: [PATCH] Fix: `node:test` not grouped with `` (#143) Following discussion in #142 , this adds an explicit check for `"node:test"` to `BUILTIN_MODULES_REGEX_STR` in order to properly group it with other builtin modules. Test covers: - `"node:test"` correctly gets grouped in with other builtin node modules - `"node:test"` is correctly sorted among other builtin node modules - `"*node:test"` is NOT grouped in with builtin modules - `"node:test*"` is NOT grouped in with builtin modules - `"node"` is NOT grouped in with builtin modules - `"test"` is NOT grouped in with builtin modules Fixes https://github.com/IanVS/prettier-plugin-sort-imports/issues/142 --- src/constants.ts | 6 +++-- .../__snapshots__/ppsi.spec.ts.snap | 25 +++++++++++++++++++ .../groups-node-test-with-builtin-modules.js | 10 ++++++++ tests/BuiltinModulesNodeTest/ppsi.spec.ts | 12 +++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 tests/BuiltinModulesNodeTest/__snapshots__/ppsi.spec.ts.snap create mode 100644 tests/BuiltinModulesNodeTest/groups-node-test-with-builtin-modules.js create mode 100644 tests/BuiltinModulesNodeTest/ppsi.spec.ts diff --git a/src/constants.ts b/src/constants.ts index e044e8f..3ce945c 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -23,9 +23,11 @@ export const mergeableImportFlavors = [ importFlavorType, ] as const; -export const BUILTIN_MODULES_REGEX_STR = `^(?:node:)?(?:${builtinModules.join( +// NOTE: 'test' is not included in `builtinModules` so explicitly check for "node:test" +// REF: https://github.com/nodejs/node/issues/42785 +export const BUILTIN_MODULES_REGEX_STR = `(^(?:node:)?(?:${builtinModules.join( '|', -)})$`; +)})$)|(^node:test$)`; export const BUILTIN_MODULES_SPECIAL_WORD = ''; /** diff --git a/tests/BuiltinModulesNodeTest/__snapshots__/ppsi.spec.ts.snap b/tests/BuiltinModulesNodeTest/__snapshots__/ppsi.spec.ts.snap new file mode 100644 index 0000000..eb11fc4 --- /dev/null +++ b/tests/BuiltinModulesNodeTest/__snapshots__/ppsi.spec.ts.snap @@ -0,0 +1,25 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`groups-node-test-with-builtin-modules.js - typescript-verify > groups-node-test-with-builtin-modules.js 1`] = ` +import nodeUtil from 'node:util'; + +import nodeNodeTestTest from 'node:node:test:test'; +import nodeNodeTest from 'node:node:test'; +import nodeTestTest from 'node:test:test'; +import nodeTest from 'node:test'; +import test from 'test' +import node from 'node'; + +import fs from 'fs'; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +import fs from "fs"; +import nodeTest from "node:test"; +import nodeUtil from "node:util"; + +import node from "node"; +import nodeNodeTest from "node:node:test"; +import nodeNodeTestTest from "node:node:test:test"; +import nodeTestTest from "node:test:test"; +import test from "test"; + +`; diff --git a/tests/BuiltinModulesNodeTest/groups-node-test-with-builtin-modules.js b/tests/BuiltinModulesNodeTest/groups-node-test-with-builtin-modules.js new file mode 100644 index 0000000..f9d301f --- /dev/null +++ b/tests/BuiltinModulesNodeTest/groups-node-test-with-builtin-modules.js @@ -0,0 +1,10 @@ +import nodeUtil from 'node:util'; + +import nodeNodeTestTest from 'node:node:test:test'; +import nodeNodeTest from 'node:node:test'; +import nodeTestTest from 'node:test:test'; +import nodeTest from 'node:test'; +import test from 'test' +import node from 'node'; + +import fs from 'fs'; diff --git a/tests/BuiltinModulesNodeTest/ppsi.spec.ts b/tests/BuiltinModulesNodeTest/ppsi.spec.ts new file mode 100644 index 0000000..6330354 --- /dev/null +++ b/tests/BuiltinModulesNodeTest/ppsi.spec.ts @@ -0,0 +1,12 @@ +import {run_spec} from '../../test-setup/run_spec'; + +run_spec(__dirname, ["typescript"], { + importOrder: [ + "", + "", + "", + "", + "", + "^[.]", + ], +});