1
1
// @ts -ignore
2
2
3
- // Requires
3
+ // Imports
4
4
import fsNode from "node:fs" ;
5
5
import { createRequire } from "node:module" ;
6
6
const dynamicRequire = createRequire ( import . meta. url ) ;
@@ -15,7 +15,7 @@ import { lint, extendConfig, readConfig } from "markdownlint/promise";
15
15
import { expandTildePath } from "markdownlint/helpers" ;
16
16
import appendToArray from "./append-to-array.mjs" ;
17
17
import mergeOptions from "./merge-options.mjs" ;
18
- import resolveAndRequire from "./resolve-and-require .mjs" ;
18
+ import resolveModule from "./resolve-module .mjs" ;
19
19
import parsers from "./parsers/parsers.mjs" ;
20
20
import jsoncParse from "./parsers/jsonc-parse.mjs" ;
21
21
import yamlParse from "./parsers/yaml-parse.mjs" ;
@@ -65,73 +65,65 @@ const readConfigFile = (fs, dir, name, otherwise) => () => {
65
65
) ;
66
66
} ;
67
67
68
- // Import or resolve/require a module ID with a custom directory in the path
69
- const importOrRequireResolve = async ( dirOrDirs , id , noRequire ) => {
70
- if ( typeof id === "string" ) {
71
- if ( noRequire ) {
72
- return null ;
73
- }
74
- const dirs = Array . isArray ( dirOrDirs ) ? dirOrDirs : [ dirOrDirs ] ;
75
- const expandId = expandTildePath ( id , os ) ;
76
- const errors = [ ] ;
77
- // Try to load via require(...)
78
- try {
79
- const isModule = / \. m j s $ / iu. test ( expandId ) ;
80
- if ( ! isModule ) {
81
- // Try not to use require for modules due to breaking change in Node 22.12:
82
- // https://github.com/nodejs/node/releases/tag/v22.12.0
83
- return resolveAndRequire ( dynamicRequire , expandId , dirs ) ;
84
- }
85
- } catch ( error ) {
86
- errors . push ( error ) ;
87
- }
88
- // Try to load via import(...)
68
+ // Import a module ID with a custom directory in the path
69
+ const importModule = async ( dirOrDirs , id , noImport ) => {
70
+ if ( typeof id !== "string" ) {
71
+ return id ;
72
+ } else if ( noImport ) {
73
+ return null ;
74
+ }
75
+ const dirs = Array . isArray ( dirOrDirs ) ? dirOrDirs : [ dirOrDirs ] ;
76
+ const expandId = expandTildePath ( id , os ) ;
77
+ const errors = [ ] ;
78
+ let moduleName = null ;
79
+ try {
89
80
try {
90
- // eslint-disable-next-line n/no-unsupported-features/node-builtins
91
- const isURL = ! pathDefault . isAbsolute ( expandId ) && URL . canParse ( expandId ) ;
92
- const urlString = (
93
- isURL ? new URL ( expandId ) : pathToFileURL ( pathDefault . resolve ( dirs [ 0 ] , expandId ) )
94
- ) . toString ( ) ;
95
- // eslint-disable-next-line no-inline-comments
96
- const module = await import ( /* webpackIgnore: true */ urlString ) ;
97
- return module . default ;
81
+ moduleName = pathToFileURL ( resolveModule ( dynamicRequire , expandId , dirs ) ) ;
98
82
} catch ( error ) {
99
83
errors . push ( error ) ;
84
+ moduleName =
85
+ // eslint-disable-next-line n/no-unsupported-features/node-builtins
86
+ ( ! pathDefault . isAbsolute ( expandId ) && URL . canParse ( expandId ) )
87
+ ? new URL ( expandId )
88
+ : pathToFileURL ( pathDefault . resolve ( dirs [ 0 ] , expandId ) ) ;
100
89
}
101
- // Give up
90
+ // eslint-disable-next-line no-inline-comments
91
+ const module = await import ( /* webpackIgnore: true */ moduleName ) ;
92
+ return module . default ;
93
+ } catch ( error ) {
94
+ errors . push ( error ) ;
102
95
throw new AggregateError (
103
96
errors ,
104
- `Unable to require or import module '${ id } '.`
97
+ `Unable to import module '${ id } '.`
105
98
) ;
106
99
}
107
- return id ;
108
100
} ;
109
101
110
- // Import or require an array of modules by ID
111
- const importOrRequireIds = ( dirs , ids , noRequire ) => (
102
+ // Import an array of modules by ID
103
+ const importModuleIds = ( dirs , ids , noImport ) => (
112
104
Promise . all (
113
105
ids . map (
114
- ( id ) => importOrRequireResolve ( dirs , id , noRequire )
106
+ ( id ) => importModule ( dirs , id , noImport )
115
107
)
116
108
) . then ( ( results ) => results . filter ( Boolean ) )
117
109
) ;
118
110
119
- // Import or require an array of modules by ID (preserving parameters)
120
- const importOrRequireIdsAndParams = ( dirs , idsAndParams , noRequire ) => (
111
+ // Import an array of modules by ID (preserving parameters)
112
+ const importModuleIdsAndParams = ( dirs , idsAndParams , noImport ) => (
121
113
Promise . all (
122
114
idsAndParams . map (
123
- ( idAndParams ) => importOrRequireResolve ( dirs , idAndParams [ 0 ] , noRequire ) .
115
+ ( idAndParams ) => importModule ( dirs , idAndParams [ 0 ] , noImport ) .
124
116
then ( ( module ) => module && [ module , ...idAndParams . slice ( 1 ) ] )
125
117
)
126
118
) . then ( ( results ) => results . filter ( Boolean ) )
127
119
) ;
128
120
129
- // Import or require a JavaScript file and return the exported object
130
- const importOrRequireConfig = ( fs , dir , name , noRequire , otherwise ) => ( ) => {
121
+ // Import a JavaScript file and return the exported object
122
+ const importConfig = ( fs , dir , name , noImport , otherwise ) => ( ) => {
131
123
const file = pathPosix . join ( dir , name ) ;
132
124
return fs . promises . access ( file ) .
133
125
then (
134
- ( ) => importOrRequireResolve ( dir , name , noRequire ) ,
126
+ ( ) => importModule ( dir , name , noImport ) ,
135
127
otherwise
136
128
) ;
137
129
} ;
@@ -151,7 +143,7 @@ const getExtendedConfig = (config, configPath, fs) => {
151
143
} ;
152
144
153
145
// Read an options or config file in any format and return the object
154
- const readOptionsOrConfig = async ( configPath , fs , noRequire ) => {
146
+ const readOptionsOrConfig = async ( configPath , fs , noImport ) => {
155
147
const basename = pathPosix . basename ( configPath ) ;
156
148
const dirname = pathPosix . dirname ( configPath ) ;
157
149
let options = null ;
@@ -165,7 +157,7 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => {
165
157
basename . endsWith ( ".markdownlint-cli2.cjs" ) ||
166
158
basename . endsWith ( ".markdownlint-cli2.mjs" )
167
159
) {
168
- options = await importOrRequireResolve ( dirname , basename , noRequire ) ;
160
+ options = await importModule ( dirname , basename , noImport ) ;
169
161
} else if (
170
162
basename . endsWith ( ".markdownlint.jsonc" ) ||
171
163
basename . endsWith ( ".markdownlint.json" ) ||
@@ -177,7 +169,7 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => {
177
169
basename . endsWith ( ".markdownlint.cjs" ) ||
178
170
basename . endsWith ( ".markdownlint.mjs" )
179
171
) {
180
- config = await importOrRequireResolve ( dirname , basename , noRequire ) ;
172
+ config = await importModule ( dirname , basename , noImport ) ;
181
173
} else {
182
174
throw new Error (
183
175
"File name should be (or end with) one of the supported types " +
@@ -288,7 +280,7 @@ const getAndProcessDirInfo = (
288
280
dirToDirInfo ,
289
281
dir ,
290
282
relativeDir ,
291
- noRequire ,
283
+ noImport ,
292
284
allowPackageJson
293
285
) => {
294
286
// Create dirInfo
@@ -322,10 +314,10 @@ const getAndProcessDirInfo = (
322
314
( ) => fs . promises . readFile ( file , utf8 ) . then ( yamlParse ) ,
323
315
( ) => fs . promises . access ( captureFile ( markdownlintCli2Cjs ) ) .
324
316
then (
325
- ( ) => importOrRequireResolve ( dir , file , noRequire ) ,
317
+ ( ) => importModule ( dir , file , noImport ) ,
326
318
( ) => fs . promises . access ( captureFile ( markdownlintCli2Mjs ) ) .
327
319
then (
328
- ( ) => importOrRequireResolve ( dir , file , noRequire ) ,
320
+ ( ) => importModule ( dir , file , noImport ) ,
329
321
( ) => ( allowPackageJson
330
322
? fs . promises . access ( captureFile ( packageJson ) )
331
323
// eslint-disable-next-line prefer-promise-reject-errors
@@ -379,16 +371,16 @@ const getAndProcessDirInfo = (
379
371
fs ,
380
372
dir ,
381
373
".markdownlint.yml" ,
382
- importOrRequireConfig (
374
+ importConfig (
383
375
fs ,
384
376
dir ,
385
377
".markdownlint.cjs" ,
386
- noRequire ,
387
- importOrRequireConfig (
378
+ noImport ,
379
+ importConfig (
388
380
fs ,
389
381
dir ,
390
382
".markdownlint.mjs" ,
391
- noRequire ,
383
+ noImport ,
392
384
noop
393
385
)
394
386
)
@@ -417,7 +409,7 @@ const getBaseOptions = async (
417
409
options ,
418
410
fixDefault ,
419
411
noGlobs ,
420
- noRequire
412
+ noImport
421
413
) => {
422
414
const tasks = [ ] ;
423
415
const dirToDirInfo = { } ;
@@ -427,7 +419,7 @@ const getBaseOptions = async (
427
419
dirToDirInfo ,
428
420
baseDir ,
429
421
relativeDir ,
430
- noRequire ,
422
+ noImport ,
431
423
true
432
424
) ;
433
425
await Promise . all ( tasks ) ;
@@ -468,7 +460,7 @@ const enumerateFiles = async (
468
460
dirToDirInfo ,
469
461
gitignore ,
470
462
ignoreFiles ,
471
- noRequire
463
+ noImport
472
464
) => {
473
465
const tasks = [ ] ;
474
466
/** @type {import("globby").Options } */
@@ -536,7 +528,7 @@ const enumerateFiles = async (
536
528
dirToDirInfo ,
537
529
dir ,
538
530
null ,
539
- noRequire ,
531
+ noImport ,
540
532
false
541
533
) ;
542
534
dirInfo . files . push ( file ) ;
@@ -549,7 +541,7 @@ const enumerateParents = async (
549
541
fs ,
550
542
baseDir ,
551
543
dirToDirInfo ,
552
- noRequire
544
+ noImport
553
545
) => {
554
546
const tasks = [ ] ;
555
547
@@ -578,7 +570,7 @@ const enumerateParents = async (
578
570
dirToDirInfo ,
579
571
dir ,
580
572
null ,
581
- noRequire ,
573
+ noImport ,
582
574
false
583
575
) ;
584
576
lastDirInfo . parent = dirInfo ;
@@ -603,7 +595,7 @@ const createDirInfos = async (
603
595
optionsOverride ,
604
596
gitignore ,
605
597
ignoreFiles ,
606
- noRequire
598
+ noImport
607
599
) => {
608
600
await enumerateFiles (
609
601
fs ,
@@ -613,13 +605,13 @@ const createDirInfos = async (
613
605
dirToDirInfo ,
614
606
gitignore ,
615
607
ignoreFiles ,
616
- noRequire
608
+ noImport
617
609
) ;
618
610
await enumerateParents (
619
611
fs ,
620
612
baseDir ,
621
613
dirToDirInfo ,
622
- noRequire
614
+ noImport
623
615
) ;
624
616
625
617
// Merge file lists with identical configuration
@@ -650,10 +642,10 @@ const createDirInfos = async (
650
642
) ;
651
643
if ( markdownlintOptions && markdownlintOptions . customRules ) {
652
644
tasks . push (
653
- importOrRequireIds (
645
+ importModuleIds (
654
646
[ effectiveDir , ...effectiveModulePaths ] ,
655
647
markdownlintOptions . customRules ,
656
- noRequire
648
+ noImport
657
649
) . then ( ( customRules ) => {
658
650
// Expand nested arrays (for packages that export multiple rules)
659
651
markdownlintOptions . customRules = customRules . flat ( ) ;
@@ -662,10 +654,10 @@ const createDirInfos = async (
662
654
}
663
655
if ( markdownlintOptions && markdownlintOptions . markdownItPlugins ) {
664
656
tasks . push (
665
- importOrRequireIdsAndParams (
657
+ importModuleIdsAndParams (
666
658
[ effectiveDir , ...effectiveModulePaths ] ,
667
659
markdownlintOptions . markdownItPlugins ,
668
- noRequire
660
+ noImport
669
661
) . then ( ( markdownItPlugins ) => {
670
662
markdownlintOptions . markdownItPlugins = markdownItPlugins ;
671
663
} )
@@ -858,7 +850,7 @@ const outputSummary = async (
858
850
modulePaths ,
859
851
logMessage ,
860
852
logError ,
861
- noRequire
853
+ noImport
862
854
) => {
863
855
const errorsPresent = ( summary . length > 0 ) ;
864
856
if ( errorsPresent || outputFormatters ) {
@@ -871,7 +863,7 @@ const outputSummary = async (
871
863
const dir = relativeDir || baseDir ;
872
864
const dirs = [ dir , ...modulePaths ] ;
873
865
const formattersAndParams = outputFormatters
874
- ? await importOrRequireIdsAndParams ( dirs , outputFormatters , noRequire )
866
+ ? await importModuleIdsAndParams ( dirs , outputFormatters , noImport )
875
867
// eslint-disable-next-line no-inline-comments, unicorn/no-await-expression-member
876
868
: [ [ ( await import ( /* webpackMode: "eager" */ "markdownlint-cli2-formatter-default" ) ) . default ] ] ;
877
869
await Promise . all ( formattersAndParams . map ( ( formatterAndParams ) => {
@@ -891,7 +883,7 @@ export const main = async (params) => {
891
883
optionsDefault,
892
884
optionsOverride,
893
885
fileContents,
894
- noRequire ,
886
+ noImport ,
895
887
allowStdin
896
888
} = params ;
897
889
let {
@@ -948,7 +940,7 @@ export const main = async (params) => {
948
940
const resolvedConfigPath =
949
941
posixPath ( pathDefault . resolve ( baseDirSystem , configPath ) ) ;
950
942
optionsArgv =
951
- await readOptionsOrConfig ( resolvedConfigPath , fs , noRequire ) ;
943
+ await readOptionsOrConfig ( resolvedConfigPath , fs , noImport ) ;
952
944
relativeDir = pathPosix . dirname ( resolvedConfigPath ) ;
953
945
}
954
946
// Process arguments and get base options
@@ -961,7 +953,7 @@ export const main = async (params) => {
961
953
optionsArgv || optionsDefault ,
962
954
fixDefault ,
963
955
noGlobs ,
964
- noRequire
956
+ noImport
965
957
) ;
966
958
} finally {
967
959
if ( ! baseOptions ?. baseMarkdownlintOptions . noBanner ) {
@@ -1020,7 +1012,7 @@ export const main = async (params) => {
1020
1012
optionsOverride ,
1021
1013
gitignore ,
1022
1014
ignoreFiles ,
1023
- noRequire
1015
+ noImport
1024
1016
) ;
1025
1017
// Output linting status
1026
1018
if ( showProgress ) {
@@ -1058,7 +1050,7 @@ export const main = async (params) => {
1058
1050
modulePaths ,
1059
1051
logMessage ,
1060
1052
logError ,
1061
- noRequire
1053
+ noImport
1062
1054
) ;
1063
1055
// Return result
1064
1056
return errorsPresent ? 1 : 0 ;
0 commit comments