Skip to content

Commit

Permalink
number-literal-case: Fix false positive on `@typescript-eslint/pars…
Browse files Browse the repository at this point in the history
…er` (#815)
  • Loading branch information
fisker committed Sep 1, 2020
1 parent 49d05aa commit 33a1268
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
16 changes: 10 additions & 6 deletions rules/number-literal-case.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
'use strict';
const getDocumentationUrl = require('./utils/get-documentation-url');

const fix = (value, isBigInt) => {
value = value.toLowerCase();
if (value.startsWith('0x')) {
value = '0x' + value.slice(2).toUpperCase();
const fix = (raw, isBigInt) => {
let fixed = raw.toLowerCase();
if (fixed.startsWith('0x')) {
fixed = '0x' + fixed.slice(2).toUpperCase();

if (isBigInt) {
fixed = fixed.slice(0, -1) + 'n';
}
}

return `${value}${isBigInt ? 'n' : ''}`;
return fixed;
};

const create = context => {
Expand All @@ -20,7 +24,7 @@ const create = context => {
return;
}

const fixed = fix(isBigInt ? bigint : raw, isBigInt);
const fixed = fix(raw, isBigInt);

if (raw !== fixed) {
context.report({
Expand Down
41 changes: 37 additions & 4 deletions test/number-literal-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,31 @@ const ruleTester = avaRuleTester(test, {
ecmaVersion: 2020
}
});
const babelRuleTester = avaRuleTester(test, {
parser: require.resolve('babel-eslint')
});
const typescriptRuleTester = avaRuleTester(test, {
parser: require.resolve('@typescript-eslint/parser')
});

const error = {
message: 'Invalid number literal casing.'
};

// TODO: Add numeric separator tests when ESLint supports it.
// Legacy octal literals
ruleTester.run('number-literal-case', rule, {
valid: [
'const foo = 0777',
'const foo = 0888'
],
invalid: []
});

// TODO: Add numeric separator tests when ESLint supports it.
const tests = {
valid: [
// Number
'const foo = 1234',
'const foo = 0777',
'const foo = 0888',
'const foo = 0b10',
'const foo = 0o1234567',
'const foo = 0xABCDEF',
Expand Down Expand Up @@ -81,6 +94,22 @@ ruleTester.run('number-literal-case', rule, {
errors: [error],
output: 'const foo = 0xABCDEFn'
},
// `0n`
{
code: 'const foo = 0B0n',
errors: [error],
output: 'const foo = 0b0n'
},
{
code: 'const foo = 0O0n',
errors: [error],
output: 'const foo = 0o0n'
},
{
code: 'const foo = 0X0n',
errors: [error],
output: 'const foo = 0x0n'
},

// Exponential notation
{
Expand Down Expand Up @@ -116,4 +145,8 @@ ruleTester.run('number-literal-case', rule, {
`
}
]
});
};

ruleTester.run('number-literal-case', rule, tests);
babelRuleTester.run('number-literal-case', rule, tests);
typescriptRuleTester.run('number-literal-case', rule, tests);

0 comments on commit 33a1268

Please sign in to comment.