-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: no-missing-label-refs should not crash on undefined labels #290
Conversation
{ | ||
code: "[foo]\n[foo][bar]", | ||
errors: [ | ||
{ | ||
messageId: "notFound", | ||
data: { label: "foo" }, | ||
line: 1, | ||
column: 2, | ||
endLine: 1, | ||
endColumn: 5, | ||
}, | ||
{ | ||
messageId: "notFound", | ||
data: { label: "bar" }, | ||
line: 2, | ||
column: 7, | ||
endLine: 2, | ||
endColumn: 10, | ||
}, | ||
], | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was another bug: the regex that looks for [foo][bar]
matched first and then the loop just skipped the previous code so only one error was reported.
findOffsets(node.value, columnStart); | ||
|
||
const startLine = nodeStartLine + startLineOffset; | ||
const startColumn = nodeStartColumn + startColumnOffset; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Columns were wrong in some cases, for two reasons:
TextNode#value
is not always the raw text of the node - some lines are trimmed left, and some aren't.findOffsets()
:columnOffset
is the column offset only when it's on the same line. For other lines,columnOffset
is 0-based column number, not offset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point on 2. Can you add comments to findOffsets()
to document that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a note on how columnOffset
should be used in 63d3b4d.
if (!Array.isArray(match)) { | ||
break; | ||
} | ||
const labelPattern = /\[(?<left>[^\]]*)\](?:\[(?<right>[^\]]*)\])?/dgu; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we pull this regex out into the module scope? There doesn't seem to be anything context-sensitive that would require it to be in here. I'd rather parse regexes once vs. every time the function is called.
Also, it would be helpful to add a comment explaining what the regex does and how it is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it would be helpful to add a comment explaining what the regex does and how it is used.
Added in 60d5735.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we pull this regex out into the module scope? There doesn't seem to be anything context-sensitive that would require it to be in here. I'd rather parse regexes once vs. every time the function is called.
The g
flag makes it mutable - each .exec()
modifies lastIndex
. I think it's a good practice to keep those regex instances scoped to the search instance that's using them, especially if they are used in a loop, because a small change in the code, like adding an early exit in the loop, can cause bugs that are difficult to reproduce and figure out.
|
||
while ((match = labelPattern.exec(nodeText))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment explaining how this loop works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in bcee538.
findOffsets(node.value, columnStart); | ||
|
||
const startLine = nodeStartLine + startLineOffset; | ||
const startColumn = nodeStartColumn + startColumnOffset; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point on 2. Can you add comments to findOffsets()
to document that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
Fixes #289