-
Notifications
You must be signed in to change notification settings - Fork 886
Conversation
src/rules/numberLiteralFormatRule.ts
Outdated
} | ||
|
||
// Allow '10', but not '1.0' | ||
if (text.endsWith("0") && text.includes(".")) { |
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.
you will get false positives for 1.1e10
and will not catch 1.0e1
consider node.getText(sourceFile).replace(/[eE]-?\d+$/, "")
Edit: should also work for 1.0e-1
src/rules/numberLiteralFormatRule.ts
Outdated
return; | ||
} | ||
|
||
if (text.startsWith(".")) { |
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.
consider adding a check for .endsWith('.')
for literals like 1.
(or 1.e1
- see my other comment)
src/rules/numberLiteralFormatRule.ts
Outdated
function check(node: ts.NumericLiteral): void { | ||
// Apparently the number literal '0.0' has a '.text' of '0', so use '.getText()' instead. | ||
const text = node.getText(sourceFile); | ||
if (text.includes("e")) { |
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.
e
can also be uppercase
also consider removing this if statement in favor of some thing like this:
const [num, exp] = text.split(/e/i);
if (exp !== undefined) {
// check exponent for leading zero
}
// check num, you don't need an extra function and a callback for adding the failure
src/rules/numberLiteralFormatRule.ts
Outdated
} | ||
|
||
function checkFormat(text: string, fail: (message: string) => void): void { | ||
if (text.length <= 1) { |
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.
add || !text.includes(".")
and remove it from the other condition below
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.
Code LGTM
Though I'm not sure if we rather want to make this rule configurable
c9d358e
to
3ec5417
Compare
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.
better to keep things simple & opinionated at first. configuration can come later by user request/demand.
PR checklist
Overview of change:
Added the
number-literal-format
rule, which warns on.5
and0.50
(both: prefer0.5
).This could also be extended to implement #1391 if anyone wants that.
CHANGELOG.md entry:
[new-rule]
number-literal-format