77// Imports
88//-----------------------------------------------------------------------------
99
10- import { findOffsets , illegalShorthandTailPattern } from "../util.js" ;
10+ import { illegalShorthandTailPattern } from "../util.js" ;
1111
1212//-----------------------------------------------------------------------------
1313// Type Definitions
@@ -17,6 +17,7 @@ import { findOffsets, illegalShorthandTailPattern } from "../util.js";
1717 * @import { Position } from "unist";
1818 * @import { Text } from "mdast";
1919 * @import { MarkdownRuleDefinition } from "../types.js";
20+ * @import { MarkdownSourceCode } from "../language/markdown-source-code.js";
2021 * @typedef {"notFound" } NoMissingLabelRefsMessageIds
2122 * @typedef {[{ allowLabels?: string[] }] } NoMissingLabelRefsOptions
2223 * @typedef {MarkdownRuleDefinition<{ RuleOptions: NoMissingLabelRefsOptions, MessageIds: NoMissingLabelRefsMessageIds }> } NoMissingLabelRefsRuleDefinition
@@ -29,14 +30,13 @@ import { findOffsets, illegalShorthandTailPattern } from "../util.js";
2930/**
3031 * Finds missing references in a node.
3132 * @param {Text } node The node to check.
32- * @param {string } nodeText The text of the node .
33+ * @param {MarkdownSourceCode } sourceCode The Markdown source code object .
3334 * @returns {Array<{label:string,position:Position}> } The missing references.
3435 */
35- function findMissingReferences ( node , nodeText ) {
36+ function findMissingReferences ( node , sourceCode ) {
3637/** @type {Array<{label:string,position:Position}> } */
3738const missing = [ ] ;
38- const nodeStartLine = node . position . start . line ;
39- const nodeStartColumn = node . position . start . column ;
39+ const nodeText = sourceCode . getText ( node ) ;
4040
4141/**
4242 * Matches substrings like `"[foo]"`, `"[]"`, `"[foo][bar]"`, `"[foo][]"`, `"[][bar]"`, or `"[][]"`.
@@ -46,6 +46,7 @@ function findMissingReferences(node, nodeText) {
4646const labelPattern =
4747/ (?< = (?< ! \\ ) (?: \\ { 2 } ) * ) \[ (?< left > (?: \\ .| [ ^ [ \] \\ ] ) * ) \] (?: \[ (?< right > (?: \\ .| [ ^ \] \\ ] ) * ) \] ) ? / dgu;
4848
49+ /** @type {RegExpExecArray } */
4950let match ;
5051
5152/*
@@ -75,28 +76,14 @@ function findMissingReferences(node, nodeText) {
7576labelIndices = match . indices . groups . left ;
7677}
7778
78- const { lineOffset : startLineOffset , columnOffset : startColumnOffset } =
79- findOffsets ( nodeText , labelIndices [ 0 ] ) ;
80- const { lineOffset : endLineOffset , columnOffset : endColumnOffset } =
81- findOffsets ( nodeText , labelIndices [ 1 ] ) ;
79+ const startOffset = labelIndices [ 0 ] + node . position . start . offset ;
80+ const endOffset = labelIndices [ 1 ] + node . position . start . offset ;
8281
8382missing . push ( {
8483label : label . trim ( ) ,
8584position : {
86- start : {
87- line : nodeStartLine + startLineOffset ,
88- column :
89- startLineOffset > 0
90- ? startColumnOffset + 1
91- : nodeStartColumn + startColumnOffset ,
92- } ,
93- end : {
94- line : nodeStartLine + endLineOffset ,
95- column :
96- endLineOffset > 0
97- ? endColumnOffset + 1
98- : nodeStartColumn + endColumnOffset ,
99- } ,
85+ start : sourceCode . getLocFromIndex ( startOffset ) ,
86+ end : sourceCode . getLocFromIndex ( endOffset ) ,
10087} ,
10188} ) ;
10289}
@@ -169,7 +156,7 @@ export default {
169156text ( node ) {
170157const missingReferences = findMissingReferences (
171158node ,
172- sourceCode . getText ( node ) ,
159+ sourceCode ,
173160) ;
174161
175162for ( const missingReference of missingReferences ) {
0 commit comments