|
| 1 | +/* |
| 2 | + * creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs |
| 3 | + * Copyright © 2023 Green Code Initiative (https://green-code-initiative.org) |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +"use strict"; |
| 20 | + |
| 21 | +//------------------------------------------------------------------------------ |
| 22 | +// Requirements |
| 23 | +//------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +const rule = require("../../../lib/rules/no-imported-number-format-library"); |
| 26 | +const { RuleTester } = require("eslint"); |
| 27 | +const { describe, it } = require("node:test"); |
| 28 | + |
| 29 | +//------------------------------------------------------------------------------ |
| 30 | +// Tests |
| 31 | +//------------------------------------------------------------------------------ |
| 32 | + |
| 33 | +const ruleTester = new RuleTester({ |
| 34 | + languageOptions: { |
| 35 | + ecmaVersion: 6, |
| 36 | + sourceType: "module", |
| 37 | + }, |
| 38 | +}); |
| 39 | + |
| 40 | +const expectedIdentifierError = { |
| 41 | + messageId: "ShouldNotUseImportedNumberFormatLibrary", |
| 42 | +}; |
| 43 | +const expectedImportError = { |
| 44 | + messageId: "ShouldNotUseImportedNumberFormatLibrary", |
| 45 | +}; |
| 46 | + |
| 47 | +const tests = { |
| 48 | + valid: [ |
| 49 | + "new Intl.NumberFormat().format(1000);", |
| 50 | + "numbro(1000).add(5);", |
| 51 | + ` |
| 52 | + const number = numbro(1000); |
| 53 | + const number2 = numbro(2000); |
| 54 | + number2.add(1000); |
| 55 | + `, |
| 56 | + "import { parse } from 'numerable';", |
| 57 | + "import { format } from 'date-fns';", |
| 58 | + "import mysql from 'mysql2';", |
| 59 | + ], |
| 60 | + invalid: [ |
| 61 | + { |
| 62 | + code: "numbro(1000).format({thousandSeparated: true});", |
| 63 | + errors: [expectedIdentifierError], |
| 64 | + }, |
| 65 | + { |
| 66 | + code: ` |
| 67 | + const number = numbro(1000); |
| 68 | + number.format({thousandSeparated: true}); |
| 69 | + `, |
| 70 | + errors: [expectedIdentifierError], |
| 71 | + }, |
| 72 | + { |
| 73 | + code: ` |
| 74 | + const number = numbro(1000); |
| 75 | + const number2 = numbro(2000); |
| 76 | + number.format({thousandSeparated: true}); |
| 77 | + `, |
| 78 | + errors: [expectedIdentifierError], |
| 79 | + }, |
| 80 | + { |
| 81 | + code: "import { format } from 'numerable';", |
| 82 | + errors: [expectedImportError], |
| 83 | + }, |
| 84 | + { |
| 85 | + code: "import { format as myFormat} from 'numerable';", |
| 86 | + errors: [expectedImportError], |
| 87 | + }, |
| 88 | + ], |
| 89 | +}; |
| 90 | + |
| 91 | +describe("no-imported-number-format-library", () => { |
| 92 | + it("no-imported-number-format-library", () => { |
| 93 | + ruleTester.run("no-imported-number-format-library", rule, tests); |
| 94 | + }); |
| 95 | +}); |
0 commit comments