@@ -35,12 +35,18 @@ const utf8 = "utf8";
3535// No-op function
3636const noop = ( ) => null ;
3737
38- // Gets a synchronous function to parse JSONC text
39- const getJsoncParse = async ( ) => {
40- const { "default" : stripJsonComments } =
41- // eslint-disable-next-line no-inline-comments
42- await import ( /* webpackMode: "eager" */ "strip-json-comments" ) ;
43- return ( text ) => JSON . parse ( stripJsonComments ( text ) ) ;
38+ // Synchronous function to parse JSONC text
39+ const jsoncParse = ( text ) => {
40+ const { parse, printParseErrorCode } = require ( "jsonc-parser" ) ;
41+ const errors = [ ] ;
42+ const result = parse ( text , errors , { "allowTrailingComma" : true } ) ;
43+ if ( errors . length > 0 ) {
44+ const aggregate = errors . map (
45+ ( err ) => `${ printParseErrorCode ( err . error ) } (offset ${ err . offset } , length ${ err . length } )`
46+ ) . join ( ", " ) ;
47+ throw new Error ( `Unable to parse JSON(C) content, ${ aggregate } ` ) ;
48+ }
49+ return result ;
4450} ;
4551
4652// Synchronous function to parse YAML text
@@ -67,12 +73,10 @@ const readConfig = (fs, dir, name, otherwise) => {
6773 const file = pathPosix . join ( dir , name ) ;
6874 return ( ) => fs . promises . access ( file ) .
6975 then (
70- ( ) => getJsoncParse ( ) . then (
71- ( jsoncParse ) => markdownlintReadConfig (
72- file ,
73- [ jsoncParse , yamlParse ] ,
74- fs
75- )
76+ ( ) => markdownlintReadConfig (
77+ file ,
78+ [ jsoncParse , yamlParse ] ,
79+ fs
7680 ) ,
7781 otherwise
7882 ) ;
@@ -139,9 +143,8 @@ const importOrRequireConfig = (fs, dir, name, noRequire, otherwise) => {
139143} ;
140144
141145// Extend a config object if it has 'extends' property
142- const getExtendedConfig = async ( config , configPath , fs ) => {
146+ const getExtendedConfig = ( config , configPath , fs ) => {
143147 if ( config . extends ) {
144- const jsoncParse = await getJsoncParse ( ) ;
145148 return markdownlintExtendConfig (
146149 config ,
147150 configPath ,
@@ -150,7 +153,7 @@ const getExtendedConfig = async (config, configPath, fs) => {
150153 ) ;
151154 }
152155
153- return config ;
156+ return Promise . resolve ( config ) ;
154157} ;
155158
156159// Read an options or config file in any format and return the object
@@ -160,7 +163,6 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => {
160163 let options = null ;
161164 let config = null ;
162165 if ( basename . endsWith ( ".markdownlint-cli2.jsonc" ) ) {
163- const jsoncParse = await getJsoncParse ( ) ;
164166 options = jsoncParse ( await fs . promises . readFile ( configPath , utf8 ) ) ;
165167 } else if ( basename . endsWith ( ".markdownlint-cli2.yaml" ) ) {
166168 options = yamlParse ( await fs . promises . readFile ( configPath , utf8 ) ) ;
@@ -177,7 +179,6 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => {
177179 basename . endsWith ( ".markdownlint.yaml" ) ||
178180 basename . endsWith ( ".markdownlint.yml" )
179181 ) {
180- const jsoncParse = await getJsoncParse ( ) ;
181182 config =
182183 await markdownlintReadConfig ( configPath , [ jsoncParse , yamlParse ] , fs ) ;
183184 } else if (
@@ -319,10 +320,7 @@ const getAndProcessDirInfo = (
319320 then (
320321 ( ) => fs . promises .
321322 readFile ( markdownlintCli2Jsonc , utf8 ) .
322- then (
323- ( content ) => getJsoncParse ( ) .
324- then ( ( jsoncParse ) => jsoncParse ( content ) )
325- ) ,
323+ then ( jsoncParse ) ,
326324 ( ) => fs . promises . access ( markdownlintCli2Yaml ) .
327325 then (
328326 ( ) => fs . promises .
@@ -346,11 +344,8 @@ const getAndProcessDirInfo = (
346344 then (
347345 ( ) => fs . promises .
348346 readFile ( packageJson , utf8 ) .
349- then (
350- ( content ) => getJsoncParse ( ) .
351- then ( ( jsoncParse ) => jsoncParse ( content ) ) .
352- then ( ( obj ) => obj [ packageName ] )
353- ) ,
347+ then ( jsoncParse ) .
348+ then ( ( obj ) => obj [ packageName ] ) ,
354349 noop
355350 )
356351 )
@@ -743,8 +738,7 @@ const createDirInfos = async (
743738} ;
744739
745740// Lint files in groups by shared configuration
746- const lintFiles = async ( fs , dirInfos , fileContents ) => {
747- const jsoncParse = await getJsoncParse ( ) ;
741+ const lintFiles = ( fs , dirInfos , fileContents ) => {
748742 const tasks = [ ] ;
749743 // For each dirInfo
750744 for ( const dirInfo of dirInfos ) {
0 commit comments