@@ -10,7 +10,14 @@ const MIN_LENGTH = 9
1010const START_CODES = [ 64 , 91 , 99 , 111 , 100 , 101 ]
1111
1212// regexp to match the import syntax
13- const SYNTAX_RE = / ^ @ \[ c o d e (?: { (?: ( \d + ) ? - ( \d + ) ? ) } ) ? (?: ( [ ^ \] ] + ) ) ? \] \( ( [ ^ ) ] * ) \) /
13+ const SYNTAX_RE =
14+ / ^ @ \[ c o d e (?: { (?: (?: (?< lineStart > \d + ) ? - (?< lineEnd > \d + ) ? ) | (?< lineSingle > \d + ) ) } ) ? (?: (?< info > [ ^ \] ] + ) ) ? \] \( (?< importPath > [ ^ ) ] * ) \) /
15+
16+ /**
17+ * Utility function to parse line number from line string that matched by SYNTAX_RE
18+ */
19+ const parseLineNumber = ( line : string | undefined ) : number | undefined =>
20+ line ? Number . parseInt ( line , 10 ) : undefined
1421
1522export const createImportCodeBlockRule =
1623 ( { handleImportPath = ( str ) => str } : ImportCodePluginOptions ) : RuleBlock =>
@@ -36,17 +43,21 @@ export const createImportCodeBlockRule =
3643
3744 // check if it's matched the syntax
3845 const match = state . src . slice ( pos , max ) . match ( SYNTAX_RE )
39- if ( ! match ) return false
46+ if ( ! match ?. groups ) return false
4047
4148 // return true as we have matched the syntax
4249 if ( silent ) return true
4350
44- const [ , lineStart , lineEnd , info , importPath ] = match
51+ const { info, importPath } = match . groups
52+
53+ const lineSingle = parseLineNumber ( match . groups . lineSingle )
54+ const lineStart = lineSingle ?? parseLineNumber ( match . groups . lineStart ) ?? 0
55+ const lineEnd = lineSingle ?? parseLineNumber ( match . groups . lineEnd )
4556
4657 const meta : ImportCodeTokenMeta = {
4758 importPath : handleImportPath ( importPath ) ,
48- lineStart : lineStart ? Number . parseInt ( lineStart , 10 ) : 0 ,
49- lineEnd : lineEnd ? Number . parseInt ( lineEnd , 10 ) : undefined ,
59+ lineStart,
60+ lineEnd,
5061 }
5162
5263 // create a import_code token
0 commit comments