Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Add number-literal-format rule #2526

Merged
merged 4 commits into from
Apr 12, 2017

Conversation

andy-hanson
Copy link
Contributor

PR checklist

  • Addresses an existing issue: #0000
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

Overview of change:

Added the number-literal-format rule, which warns on .5 and 0.50 (both: prefer 0.5).
This could also be extended to implement #1391 if anyone wants that.

CHANGELOG.md entry:

[new-rule] number-literal-format

}

// Allow '10', but not '1.0'
if (text.endsWith("0") && text.includes(".")) {
Copy link
Contributor

@ajafff ajafff Apr 8, 2017

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

return;
}

if (text.startsWith(".")) {
Copy link
Contributor

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)

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")) {
Copy link
Contributor

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

}

function checkFormat(text: string, fail: (message: string) => void): void {
if (text.length <= 1) {
Copy link
Contributor

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

Copy link
Contributor

@ajafff ajafff left a 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

@andy-hanson andy-hanson force-pushed the number-literal-format branch from c9d358e to 3ec5417 Compare April 11, 2017 00:31
Copy link
Contributor

@adidahiya adidahiya left a 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.

@adidahiya adidahiya merged commit 92c4e1e into palantir:master Apr 12, 2017
@adidahiya adidahiya added this to the TSLint v5.2 milestone Apr 12, 2017
@andy-hanson andy-hanson deleted the number-literal-format branch April 13, 2017 00:35
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants