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

Commit

Permalink
Hexadecimal literals should be uppercase
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-hanson committed Apr 11, 2017
1 parent 6520098 commit 3ec5417
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
30 changes: 28 additions & 2 deletions src/rules/numberLiteralFormatRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { isNumericLiteral } from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";
import { isUpperCase } from "./variableNameRule";

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
Expand All @@ -33,10 +34,11 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING_LEADING_0 = "Exponent should not have a leading '0'.";
public static FAILURE_STRING_LEADING_0 = "Number literal should not have a leading '0'.";
public static FAILURE_STRING_TRAILING_0 = "Number literal should not have a trailing '0'.";
public static FAILURE_STRING_TRAILING_DECIMAL = "Number literal should not end in '.'.";
public static FAILURE_STRING_LEADING_DECIMAL = "Number literal should begin with '0.' and not just '.'.";
public static FAILURE_STRING_NOT_UPPERCASE = "Hexadecimal number literal should be uppercase.";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk);
Expand All @@ -55,12 +57,36 @@ function walk(ctx: Lint.WalkContext<void>): void {
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.length <= 1) {
return;
}

if (text.startsWith("0")) {
// Hex/octal/binary number can't have decimal point or exponent, so no other errors possible.
switch (text[1]) {
case "x":
if (!isUpperCase(text.slice(2))) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING_NOT_UPPERCASE);
}
return;
case "o":
case "b":
return;
case ".":
break;
default:
ctx.addFailureAtNode(node, Rule.FAILURE_STRING_LEADING_0);
return;
}
}

const [num, exp] = text.split(/e/i);
if (exp !== undefined && (exp.startsWith("-0") || exp.startsWith("0"))) {
ctx.addFailureAt(node.getEnd() - exp.length, exp.length, Rule.FAILURE_STRING_LEADING_0);
}

if (num.length <= 1 || !text.includes(".")) {
if (!num.includes(".")) {
return;
}

Expand Down
9 changes: 8 additions & 1 deletion test/rules/number-literal-format/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
10;
1.1e10;

01;
~~ [leading-0]

1.
~~ [trailing-decimal]

Expand All @@ -27,7 +30,11 @@
1.0e10;
~~~ [trailing-0]

[leading-0]: Exponent should not have a leading '0'.
0xDEAdBEEF;
~~~~~~~~~~ [uppercase]

[leading-0]: Number literal should not have a leading '0'.
[trailing-0]: Number literal should not have a trailing '0'.
[trailing-decimal]: Number literal should not end in '.'.
[leading-decimal]: Number literal should begin with '0.' and not just '.'.
[uppercase]: Hexadecimal number literal should be uppercase.

0 comments on commit 3ec5417

Please sign in to comment.