Skip to content

Commit fdea085

Browse files
author
Fabien Bézagu
committed
GCI535: add support for the numerable library
1 parent f191d29 commit fdea085

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

eslint-plugin/lib/rules/no-imported-number-format-library.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ module.exports = {
6464
context.report(errorReport(node.parent.property));
6565
}
6666
},
67+
ImportDeclaration(node) {
68+
const importedLibraryName = node.source.value;
69+
if (importedLibraryName === 'numerable') {
70+
const formatSpecifier = node.specifiers.find(specifier => specifier.type === 'ImportSpecifier' && specifier.imported.name === 'format');
71+
if (formatSpecifier) {
72+
context.report(errorReport(formatSpecifier));
73+
}
74+
}
75+
}
6776
};
6877
},
6978
};

eslint-plugin/tests/lib/rules/no-imported-number-format-library.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ const ruleTester = new RuleTester({
3535
sourceType: "module",
3636
},
3737
});
38-
const expectedError = {
38+
const expectedIdentifierError = {
3939
messageId: "ShouldNotUseImportedNumberFormatLibrary",
4040
type: "Identifier",
4141
};
42-
42+
const expectedImportError = {
43+
messageId: "ShouldNotUseImportedNumberFormatLibrary",
44+
type: "ImportSpecifier",
45+
};
4346
ruleTester.run("no-imported-number-format-library", rule, {
4447
valid: [
4548
"new Intl.NumberFormat().format(1000);",
@@ -49,26 +52,37 @@ ruleTester.run("no-imported-number-format-library", rule, {
4952
const number2 = numbro(2000);
5053
number2.add(1000);
5154
`,
55+
"import { parse } from 'numerable';",
56+
"import { format } from 'date-fns';",
57+
"import mysql from 'mysql2';"
5258
],
5359
invalid: [
5460
{
5561
code: "numbro(1000).format({thousandSeparated: true});",
56-
errors: [expectedError],
62+
errors: [expectedIdentifierError],
5763
},
5864
{
5965
code: `
6066
const number = numbro(1000);
6167
number.format({thousandSeparated: true});
6268
`,
63-
errors: [expectedError],
69+
errors: [expectedIdentifierError],
6470
},
6571
{
6672
code: `
6773
const number = numbro(1000);
6874
const number2 = numbro(2000);
6975
number.format({thousandSeparated: true});
7076
`,
71-
errors: [expectedError],
77+
errors: [expectedIdentifierError],
78+
},
79+
{
80+
code: "import { format } from 'numerable';",
81+
errors: [expectedImportError],
82+
},
83+
{
84+
code: "import { format as myFormat} from 'numerable';",
85+
errors: [expectedImportError],
7286
},
7387
],
7488
});

test-project/src/no-imported-number-format-library.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numbro from "numbro";
2+
import { parse, format } from "numerable"; // Non-compliant: usage of external library to format number
23

34
numbro(1000).format({thousandSeparated: true}); // Non-compliant: usage of external library to format number
45

@@ -8,3 +9,6 @@ variable.format({thousandSeparated: true}); // Non-compliant: usage of external
89
numbro(2000).add(1000); // Compliant
910

1011
new Intl.NumberFormat().format(1000); // Compliant
12+
13+
format(28);
14+
parse("29"); // Compliant

0 commit comments

Comments
 (0)