-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Add lint rule to check that Debug.assert calls do not eagerly interpolate strings
#17125
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
Conversation
rbuckton
left a comment
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.
In most cases the interpolation is probably cheaper than the closure. For the common cases, we can create specific assertions on Debug.
src/compiler/core.ts
Outdated
|
|
||
| Debug.assert(start >= 0, "start must be non-negative, is " + start); | ||
| Debug.assert(length >= 0, "length must be non-negative, is " + length); | ||
| Debug.assert(start >= 0, "start must be non-negative", () => `is ${start}`); |
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.
Adding callbacks is arguably more expensive, as you have to create an environment record allocation each time.
It might make more sense to add something like Debug.assertNonNegative(start, "start") or similar.
| */ | ||
| function createInlineBreak(label: Label, location?: TextRange): ReturnStatement { | ||
| Debug.assert(label > 0, `Invalid label: ${label}`); | ||
| Debug.assert(label > 0, "Invalid label", () => label.toString()); |
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.
The callback is probably more expensive.
src/compiler/core.ts
Outdated
|
|
||
| Debug.assert(start >= 0, "start must be non-negative, is " + start); | ||
| Debug.assert(length >= 0, "length must be non-negative, is " + length); | ||
| Debug.assertLessThanOrEqual(0, start); |
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.
assertGreaterThanOrEqual?
| } | ||
|
|
||
| export function assert(expression: boolean, message?: string, verboseDebugInfo?: () => string, stackCrawlMark?: Function): void { | ||
| export function assert(expression: boolean, message?: string, verboseDebugInfo?: string | (() => string), stackCrawlMark?: Function): void { |
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.
Why this change?
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 avoids needing a closure for the second argument (if it is not an interpolation).
src/server/project.ts
Outdated
| const scriptInfo = this.projectService.getScriptInfoForPath(sourceFile.path); | ||
| if (!scriptInfo) { | ||
| Debug.assert(false, `scriptInfo for a file '${sourceFile.fileName}' is missing.`); | ||
| Debug.assert(false, "scriptInfo for a file is missing:", sourceFile.fileName); |
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.
No need to change this, other than from an assert(false, to fail as the condition was already checked.
Debug.assert calls do not eagerly interpolate stringsDebug.assert calls do not eagerly interpolate strings
The second argument is usually never used (unless an assertion fails), so we shouldn't spend time eagerly computing its value.