@@ -13,7 +13,6 @@ import {
1313ConfigCommentParser ,
1414Directive ,
1515} from "@eslint/plugin-kit" ;
16- import { findOffsets } from "../util.js" ;
1716
1817//-----------------------------------------------------------------------------
1918// Types
@@ -66,53 +65,38 @@ class InlineConfigComment {
6665/**
6766 * Extracts inline configuration comments from an HTML node.
6867 * @param {Html } node The HTML node to extract comments from.
68+ * @param {MarkdownSourceCode } sourceCode The Markdown source code object.
6969 * @returns {Array<InlineConfigComment> } The inline configuration comments found in the node.
7070 */
71- function extractInlineConfigCommentsFromHTML ( node ) {
71+ function extractInlineConfigCommentsFromHTML ( node , sourceCode ) {
7272if ( ! configCommentStart . test ( node . value ) ) {
7373return [ ] ;
7474}
75+
76+ /** @type {Array<InlineConfigComment> } */
7577const comments = [ ] ;
7678
79+ /** @type {RegExpExecArray } */
7780let match ;
7881
7982while ( ( match = htmlComment . exec ( node . value ) ) ) {
8083if ( configCommentStart . test ( match [ 0 ] ) ) {
81- const comment = match [ 0 ] ;
82-
83- // calculate location of the comment inside the node
84- const start = {
85- ...node . position . start ,
86- } ;
87-
88- const end = {
89- ...node . position . start ,
90- } ;
91-
92- const {
93- lineOffset : startLineOffset ,
94- columnOffset : startColumnOffset ,
95- } = findOffsets ( node . value , match . index ) ;
96-
97- start . line += startLineOffset ;
98- start . column += startColumnOffset ;
99- start . offset += match . index ;
100-
101- const commentLineCount = comment . split ( "\n" ) . length - 1 ;
102-
103- end . line = start . line + commentLineCount ;
104- end . column =
105- commentLineCount === 0
106- ? start . column + comment . length
107- : comment . length - comment . lastIndexOf ( "\n" ) ;
108- end . offset = start . offset + comment . length ;
84+ // calculate offset of the comment inside the node
85+ const startOffset = match . index + node . position . start . offset ;
86+ const endOffset = startOffset + match [ 0 ] . length ;
10987
11088comments . push (
11189new InlineConfigComment ( {
11290value : match [ 1 ] . trim ( ) ,
11391position : {
114- start,
115- end,
92+ start : {
93+ ...sourceCode . getLocFromIndex ( startOffset ) ,
94+ offset : startOffset ,
95+ } ,
96+ end : {
97+ ...sourceCode . getLocFromIndex ( endOffset ) ,
98+ offset : endOffset ,
99+ } ,
116100} ,
117101} ) ,
118102) ;
@@ -191,8 +175,8 @@ export class MarkdownSourceCode extends TextSourceCodeBase {
191175 */
192176getInlineConfigNodes ( ) {
193177if ( ! this . #inlineConfigComments) {
194- this . #inlineConfigComments = this . #htmlNodes. flatMap (
195- extractInlineConfigCommentsFromHTML ,
178+ this . #inlineConfigComments = this . #htmlNodes. flatMap ( htmlNode =>
179+ extractInlineConfigCommentsFromHTML ( htmlNode , this ) ,
196180) ;
197181}
198182
0 commit comments