-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: prevents the usage of Intl.NumberFormat for performa…
…nce purposes
- Loading branch information
1 parent
3f5064a
commit 8147b29
Showing
7 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
example-app/eslint-breaking-examples/break-intl-number-format-rule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// This code should trigger the ESLint rule `avoidIntlNumberFormatRule` | ||
|
||
const number = 1234567.89; | ||
|
||
// Incorrect usage: This will be flagged by the ESLint rule | ||
const formatter = new Intl.NumberFormat("en-US", { | ||
style: "currency", | ||
currency: "USD", | ||
}); | ||
|
||
const formattedNumber = formatter.format(number); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(formattedNumber); // Outputs: $1,234,567.89 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/eslint-plugin/docs/rules/avoid-intl-number-format.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Disallow the use of `Intl.NumberFormat` due to potential performance issues (`@bam.tech/avoid-intl-number-format`) | ||
|
||
💼 This rule is enabled in the `performance` config. | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Prevents from the using `Intl.NumberFormat` to improve performance. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/eslint-plugin/lib/rules/avoid-intl-number-format.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { Rule } from "eslint"; | ||
|
||
// Custom Rule: No Intl.NumberFormat Usage | ||
export const avoidIntlNumberFormatRule: Rule.RuleModule = { | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: | ||
"Disallow the use of `Intl.NumberFormat` due to potential performance issues.", | ||
category: "Possible Errors", | ||
recommended: true, | ||
url: "https://github.com/your-repo/eslint-plugin/docs/rules/no-intl-numberformat.md", // Update with your actual URL | ||
}, | ||
messages: { | ||
noIntlNumberFormat: | ||
"Avoid using `Intl.NumberFormat` as it can lead to performance issues. Consider using a lightweight formatting alternative or memoizing the formatter instance.", | ||
}, | ||
schema: [], | ||
fixable: "code", | ||
}, | ||
|
||
create(context) { | ||
return { | ||
NewExpression(node) { | ||
if ( | ||
node.callee.type === "MemberExpression" && | ||
node.callee.object.type === "Identifier" && | ||
node.callee.object.name === "Intl" && | ||
node.callee.property.type === "Identifier" && | ||
node.callee.property.name === "NumberFormat" | ||
) { | ||
context.report({ | ||
node, | ||
messageId: "noIntlNumberFormat", | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/eslint-plugin/tests/lib/rules/avoid-intl-number-format.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Save without formatting: [⌘ + K] > [S] | ||
|
||
// This should trigger an error breaking eslint-plugin-bam-custom-rules: | ||
// bam-custom-rules/avoid-intl-number-format | ||
|
||
import { avoidIntlNumberFormatRule } from "../../../lib/rules/avoid-intl-number-format"; | ||
import { RuleTester } from "eslint"; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve("@typescript-eslint/parser"), | ||
}); | ||
|
||
const valid = [ | ||
` const formatCurrency = (number: number) => { | ||
return numeral(number).format('$0,0.00'); | ||
}; | ||
const number = 1234567.89; | ||
console.log(formatCurrency(number));`, | ||
]; | ||
|
||
const invalid = [ | ||
` const number = 1234567.89; | ||
const formatter = new Intl.NumberFormat('en-US', { | ||
style: 'currency', | ||
currency: 'USD', | ||
}); | ||
const formattedNumber = formatter.format(number); | ||
console.log(formattedNumber);`, | ||
]; | ||
|
||
ruleTester.run("no-animated-without-native-driver", avoidIntlNumberFormatRule, { | ||
valid, | ||
invalid: invalid.map((code) => ({ | ||
code, | ||
errors: [ | ||
"Avoid using `Intl.NumberFormat` as it can lead to performance issues. Consider using a lightweight formatting alternative or memoizing the formatter instance.", | ||
], | ||
})), | ||
}); |