1
1
// @ts -check
2
2
3
- "use strict" ;
4
-
5
- const fs = require ( "node:fs" ) ;
6
- const path = require ( "node:path" ) ;
7
- const Ajv = require ( "ajv" ) ;
8
- const jsoncParser = require ( "jsonc-parser" ) ;
9
- const jsYaml = require ( "js-yaml" ) ;
10
- const md = require ( "markdown-it" ) ( ) ;
11
- const pluginInline = require ( "markdown-it-for-inline" ) ;
12
- const pluginSub = require ( "markdown-it-sub" ) ;
13
- const pluginSup = require ( "markdown-it-sup" ) ;
14
- const test = require ( "ava" ) . default ;
15
- const { "exports" : packageExports , homepage, name, version } = require ( "../package.json" ) ;
16
- const markdownlint = require ( "../lib/markdownlint" ) ;
17
- const constants = require ( "../lib/constants" ) ;
18
- const rules = require ( "../lib/rules" ) ;
19
- const customRules = require ( "./rules/rules.js" ) ;
20
- const configSchema = require ( "../schema/markdownlint-config-schema.json" ) ;
21
- const configSchemaStrict = require ( "../schema/markdownlint-config-schema-strict.json" ) ;
3
+ import fs from "node:fs" ;
4
+ import { createRequire } from "node:module" ;
5
+ const require = createRequire ( import . meta. url ) ;
6
+ import path from "node:path" ;
7
+ import Ajv from "ajv" ;
8
+ import jsoncParser from "jsonc-parser" ;
9
+ import jsYaml from "js-yaml" ;
10
+ import markdownIt from "markdown-it" ;
11
+ import pluginInline from "markdown-it-for-inline" ;
12
+ import pluginSub from "markdown-it-sub" ;
13
+ import pluginSup from "markdown-it-sup" ;
14
+ import test from "ava" ;
15
+ import markdownlint from "../lib/markdownlint.mjs" ;
16
+ import * as constants from "../lib/constants.mjs" ;
17
+ import rules from "../lib/rules.mjs" ;
18
+ import customRules from "./rules/rules.cjs" ;
19
+ import { __dirname , importWithTypeJson } from "./esm-helpers.mjs" ;
20
+ const packageJson = await importWithTypeJson ( "../package.json" ) ;
21
+ const configSchema = await importWithTypeJson ( "../schema/markdownlint-config-schema.json" ) ;
22
+ const configSchemaStrict = await importWithTypeJson ( "../schema/markdownlint-config-schema-strict.json" ) ;
22
23
23
24
const deprecatedRuleNames = new Set ( constants . deprecatedRuleNames ) ;
24
25
const ajvOptions = {
@@ -82,7 +83,7 @@ test("projectFiles", (t) => {
82
83
return import ( "globby" )
83
84
. then ( ( module ) => module . globby ( projectFiles ) )
84
85
. then ( ( files ) => {
85
- t . is ( files . length , 61 ) ;
86
+ t . is ( files . length , 60 ) ;
86
87
const options = {
87
88
files,
88
89
"config" : require ( "../.markdownlint.json" )
@@ -109,7 +110,7 @@ test("projectFilesExtendedAscii", (t) => {
109
110
"!doc/md036.md"
110
111
] ) )
111
112
. then ( ( files ) => {
112
- t . is ( files . length , 57 ) ;
113
+ t . is ( files . length , 56 ) ;
113
114
const options = {
114
115
files,
115
116
"config" : require ( "../.markdownlint.json" ) ,
@@ -450,7 +451,7 @@ test("styleFiles", async(t) => {
450
451
t . truthy ( require ( path . join ( "../style" , file ) ) , "Unable to load/parse." ) ;
451
452
const exportValue = `./style/${ file } ` ;
452
453
const exportKey = exportValue . replace ( / \. j s o n $ / , "" ) ;
453
- t . is ( packageExports [ exportKey ] , exportValue ) ;
454
+ t . is ( packageJson . exports [ exportKey ] , exportValue ) ;
454
455
}
455
456
} ) ;
456
457
@@ -884,7 +885,7 @@ test("readme", async(t) => {
884
885
let seenTags = false ;
885
886
let inTags = false ;
886
887
// @ts -ignore
887
- for ( const token of md . parse ( contents , { } ) ) {
888
+ for ( const token of markdownIt ( ) . parse ( contents , { } ) ) {
888
889
if (
889
890
( token . type === "bullet_list_open" ) &&
890
891
( token . level === 0 )
@@ -1035,14 +1036,14 @@ test("validateConfigExampleJson", (t) => {
1035
1036
const validateSchema = ajv . compile ( configSchema ) ;
1036
1037
t . is (
1037
1038
configSchema . $id . replace ( / ^ .* \/ v (?< ver > \d + \. \d + \. \d + ) \/ .* $ / u, "$<ver>" ) ,
1038
- version
1039
+ packageJson . version
1039
1040
) ;
1040
1041
t . is ( configSchema . $id , configSchema . properties . $schema . default ) ;
1041
1042
1042
1043
// Validate JSONC
1043
1044
const fileJson = ".markdownlint.jsonc" ;
1044
1045
const dataJson = fs . readFileSync (
1045
- path . join ( __dirname , "../schema" , fileJson ) ,
1046
+ path . join ( __dirname ( import . meta ) , "../schema" , fileJson ) ,
1046
1047
"utf8"
1047
1048
) ;
1048
1049
const jsonObject = jsoncParser . parse ( dataJson ) ;
@@ -1055,7 +1056,7 @@ test("validateConfigExampleJson", (t) => {
1055
1056
// Validate YAML
1056
1057
const fileYaml = ".markdownlint.yaml" ;
1057
1058
const dataYaml = fs . readFileSync (
1058
- path . join ( __dirname , "../schema" , fileYaml ) ,
1059
+ path . join ( __dirname ( import . meta ) , "../schema" , fileYaml ) ,
1059
1060
"utf8"
1060
1061
) ;
1061
1062
const yamlObject = jsYaml . load ( dataYaml ) ;
@@ -1074,7 +1075,7 @@ test("allBuiltInRulesHaveValidUrl", (t) => {
1074
1075
t . is (
1075
1076
// @ts -ignore
1076
1077
rule . information . href ,
1077
- `${ homepage } /blob/v${ version } /doc/${ ruleName } .md`
1078
+ `${ packageJson . homepage } /blob/v${ packageJson . version } /doc/${ ruleName } .md`
1078
1079
) ;
1079
1080
}
1080
1081
} ) ;
@@ -1087,12 +1088,12 @@ test("someCustomRulesHaveValidUrl", (t) => {
1087
1088
if ( rule === customRules . anyBlockquote ) {
1088
1089
t . is (
1089
1090
rule . information ?. href ,
1090
- `${ homepage } /blob/main/test/rules/any-blockquote.js`
1091
+ `${ packageJson . homepage } /blob/main/test/rules/any-blockquote.js`
1091
1092
) ;
1092
1093
} else if ( rule === customRules . lettersEX ) {
1093
1094
t . is (
1094
1095
rule . information ?. href ,
1095
- `${ homepage } /blob/main/test/rules/letters-E-X.js`
1096
+ `${ packageJson . homepage } /blob/main/test/rules/letters-E-X.js`
1096
1097
) ;
1097
1098
}
1098
1099
}
@@ -1351,21 +1352,21 @@ test("configParsersTOML", async(t) => {
1351
1352
test ( "getVersion" , ( t ) => {
1352
1353
t . plan ( 1 ) ;
1353
1354
const actual = markdownlint . getVersion ( ) ;
1354
- const expected = version ;
1355
+ const expected = packageJson . version ;
1355
1356
t . is ( actual , expected , "Version string not correct." ) ;
1356
1357
} ) ;
1357
1358
1358
1359
test ( "constants" , ( t ) => {
1359
1360
t . plan ( 2 ) ;
1360
1361
// @ts -ignore
1361
- t . is ( constants . homepage , homepage ) ;
1362
+ t . is ( constants . homepage , packageJson . homepage ) ;
1362
1363
// @ts -ignore
1363
- t . is ( constants . version , version ) ;
1364
+ t . is ( constants . version , packageJson . version ) ;
1364
1365
} ) ;
1365
1366
1366
1367
const exportMappings = new Map ( [
1367
- [ "." , "../lib/markdownlint.js " ] ,
1368
- [ "./helpers" , "../helpers/helpers.js " ] ,
1368
+ [ "." , "../lib/markdownlint.mjs " ] ,
1369
+ [ "./helpers" , "../helpers/helpers.cjs " ] ,
1369
1370
[ "./style/all" , "../style/all.json" ] ,
1370
1371
[ "./style/cirosantilli" , "../style/cirosantilli.json" ] ,
1371
1372
[ "./style/prettier" , "../style/prettier.json" ] ,
@@ -1374,16 +1375,22 @@ const exportMappings = new Map([
1374
1375
1375
1376
test ( "exportMappings" , ( t ) => {
1376
1377
t . deepEqual (
1377
- Object . keys ( packageExports ) ,
1378
+ Object . keys ( packageJson . exports ) ,
1378
1379
[ ...exportMappings . keys ( ) ]
1379
1380
) ;
1380
1381
} ) ;
1381
1382
1383
+ // const commonJsRe = /\.js$/u;
1384
+ const jsonRe = / \. j s o n $ / u;
1385
+ const importOptionsJson = { "with" : { "type" : "json" } } ;
1386
+
1382
1387
for ( const [ exportName , exportPath ] of exportMappings ) {
1383
- test ( exportName , ( t ) => {
1384
- t . is (
1385
- require ( exportName . replace ( / ^ \. / u, name ) ) ,
1386
- require ( exportPath )
1387
- ) ;
1388
+ test ( exportName , async ( t ) => {
1389
+ // const commonJs = !commonJsRe.test(exportPath);
1390
+ const json = jsonRe . test ( exportPath ) ;
1391
+ const importOptions = json ? importOptionsJson : undefined ;
1392
+ const importExportName = await import ( exportName . replace ( / ^ \. / u, packageJson . name ) , importOptions ) ;
1393
+ const importExportPath = await import ( exportPath , importOptions ) ;
1394
+ t . is ( importExportName , importExportPath ) ;
1388
1395
} ) ;
1389
1396
}
0 commit comments