634 |
635 | This program is free software: you can redistribute it and/or modify
636 | it under the terms of the GNU Affero General Public License as published
637 | by the Free Software Foundation, either version 3 of the License, or
638 | (at your option) any later version.
639 |
640 | This program is distributed in the hope that it will be useful,
641 | but WITHOUT ANY WARRANTY; without even the implied warranty of
642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643 | GNU Affero General Public License for more details.
644 |
645 | You should have received a copy of the GNU Affero General Public License
646 | along with this program. If not, see .
647 |
648 | Also add information on how to contact you by electronic and paper mail.
649 |
650 | If your software can interact with users remotely through a computer
651 | network, you should also make sure that it provides a way for users to
652 | get its source. For example, if your program is a web application, its
653 | interface could display a "Source" link that leads users to an archive
654 | of the code. There are many ways you could offer source, and different
655 | solutions will be better for different programs; see section 13 for the
656 | specific requirements.
657 |
658 | You should also get your employer (if you work as a programmer) or school,
659 | if any, to sign a "copyright disclaimer" for the program, if necessary.
660 | For more information on this, and how to apply and follow the GNU AGPL, see
661 | .
662 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | 🙅♀️ Codely's ESLint NO default parameters plugin
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | A plugin that forbids the use of default parameters in functions, methods and constructors.
18 | Stars are welcome 😊
19 |
20 |
21 | ## 👀 How to use
22 |
23 | 1. Install the dependency
24 | ```bash
25 | npm install --save-dev eslint-plugin-no-default-parameters
26 | ```
27 | 2. Add the plugin to your `.eslintrc.js` file:
28 | ```js
29 | {
30 | plugins: ["no-default-parameters"],
31 | }
32 | ```
33 | 3. Enable the rule
34 | ```js
35 | {
36 | rules: {
37 | "no-default-parameters/enforce": ["error"],
38 | }
39 | }
40 | ```
41 |
42 | ## 👌 Codely Code Quality Standards
43 |
44 | Publishing this package we are committing ourselves to the following code quality standards:
45 |
46 | - 🤝 Respect **Semantic Versioning**: No breaking changes in patch or minor versions
47 | - 🤏 No surprises in transitive dependencies: Use the **bare minimum dependencies** needed to meet the purpose
48 | - 🎯 **One specific purpose** to meet without having to carry a bunch of unnecessary other utilities
49 | - ✅ **Tests** as documentation and usage examples
50 | - 📖 **Well documented ReadMe** showing how to install and use
51 | - ⚖️ **License favoring Open Source** and collaboration
52 |
53 | ## 🔀 Related resources
54 |
55 | - [🔦 Linting en JavaScript y TypeScript](https://pro.codely.com/library/linting-en-javascript-y-typescript-188432/446893/about/): Used as a template to bootstrap this plugin
56 | - [🤏 Codely's ESLint + Prettier configuration](https://github.com/CodelyTV/eslint-config-codely): Opinionated linting configuration considering modern TypeScript best practices and providing consistency to your import statements. Valid for your JavaScript or TypeScript projects
57 |
58 | Opinionated skeletons ready for different purposes:
59 |
60 | - [✨🌱 JavaScript Basic Skeleton](https://github.com/CodelyTV/javascript-basic-skeleton)
61 | - [🔷🌱 TypeScript Basic Skeleton](https://github.com/CodelyTV/typescript-basic-skeleton)
62 | - [🔷🕸️ TypeScript Web Skeleton](https://github.com/CodelyTV/typescript-web-skeleton)
63 | - [🔷🌍 TypeScript API Skeleton](https://github.com/CodelyTV/typescript-api-skeleton)
64 | - [🔷✨ TypeScript DDD Skeleton](https://github.com/CodelyTV/typescript-ddd-skeleton)
65 |
66 | ## 🙌 Thanks
67 |
68 | This package is inspired by an [eslint-plugin-es rule](https://github.com/mysticatea/eslint-plugin-es/blob/v1.4.1/lib/rules/no-default-parameters.js).
69 |
--------------------------------------------------------------------------------
/jest.config.ts:
--------------------------------------------------------------------------------
1 | /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2 | module.exports = {
3 | preset: "ts-jest",
4 | testEnvironment: "node",
5 | };
6 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "eslint-plugin-no-default-parameters",
3 | "version": "1.0.3",
4 | "description": "A plugin that forbids the use of default parameters in functions, methods and constructors.",
5 | "main": "dist/src/index.js",
6 | "scripts": {
7 | "lint": "eslint .",
8 | "lint:fix": "eslint . --fix",
9 | "test": "jest -c jest.config.ts",
10 | "build": "tsc"
11 | },
12 | "peerDependencies": {
13 | "eslint": "*"
14 | },
15 | "author": "Codely (www.codely.com)",
16 | "license": "SEE LICENCE IN LICENCE",
17 | "keywords": [
18 | "eslint",
19 | "eslintplugin",
20 | "cleancode",
21 | "codely",
22 | "javascript",
23 | "typescript",
24 | "ecmascript"
25 | ],
26 | "devDependencies": {
27 | "@types/eslint": "^8.44.8",
28 | "@types/jest": "^29.5.11",
29 | "@types/node": "^20.10.4",
30 | "@typescript-eslint/eslint-plugin": "^6.14.0",
31 | "@typescript-eslint/parser": "^6.14.0",
32 | "@typescript-eslint/rule-tester": "^6.14.0",
33 | "eslint-config-codely": "^3.1.3",
34 | "jest": "^29.7.0",
35 | "jest-junit": "^16.0.0",
36 | "ts-jest": "^29.1.1",
37 | "ts-node": "^10.9.2",
38 | "typescript": "^5.3.3"
39 | },
40 | "dependencies": {
41 | "@typescript-eslint/utils": "^6.14.0",
42 | "eslint-import-resolver-typescript": "^3.6.1",
43 | "eslint-module-utils": "^2.8.0"
44 | },
45 | "homepage": "https://github.com/CodelyTV/eslint-plugin-no-default-parameters#readme",
46 | "repository": {
47 | "type": "git",
48 | "url": "git+https://github.com/CodelyTV/eslint-plugin-no-default-parameters.git"
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------
1 | import enforce from "./rules/enforce";
2 |
3 | const config = {
4 | rules: {
5 | enforce,
6 | },
7 | };
8 |
9 | export = config;
10 |
--------------------------------------------------------------------------------
/src/rules/enforce.ts:
--------------------------------------------------------------------------------
1 | import { TSESLint, TSESTree } from "@typescript-eslint/utils";
2 |
3 | import { createRule } from "../utils/createRule";
4 |
5 | type MessageIds = "default-parameter";
6 | type Options = unknown;
7 |
8 | export type RuleContext = Readonly>;
9 |
10 | const rule = createRule({
11 | name: "enforce",
12 | meta: {
13 | docs: {
14 | description: "Forbids the use of default parameters in functions, methods and constructors",
15 | recommended: "strict",
16 | requiresTypeChecking: false,
17 | },
18 | messages: {
19 | "default-parameter": "Default parameter are forbidden",
20 | },
21 | type: "problem",
22 | schema: [],
23 | },
24 | defaultOptions: [],
25 | create(context: RuleContext) {
26 | return {
27 | ":function > AssignmentPattern"(node: TSESTree.Node) {
28 | context.report({ node, messageId: "default-parameter" });
29 | },
30 | "MethodDefinition[kind='constructor'] > FunctionExpression > TSParameterProperty[parameter.type='AssignmentPattern']"(
31 | node: TSESTree.Node,
32 | ) {
33 | context.report({ node, messageId: "default-parameter" });
34 | },
35 | };
36 | },
37 | });
38 |
39 | export default rule;
40 |
--------------------------------------------------------------------------------
/src/utils/createRule.ts:
--------------------------------------------------------------------------------
1 | import { ESLintUtils } from "@typescript-eslint/utils";
2 |
3 | export const createRule = ESLintUtils.RuleCreator(
4 | () => "https://github.com/CodelyTV/eslint-plugin-no-default-parameters/blob/main/README.md",
5 | );
6 |
--------------------------------------------------------------------------------
/tests/rules/enforce.spec.ts:
--------------------------------------------------------------------------------
1 | "use strict";
2 |
3 | import { RuleTester } from "@typescript-eslint/rule-tester";
4 |
5 | import rule from "../../src/rules/enforce";
6 |
7 | const ruleTester = new RuleTester({
8 | parser: "@typescript-eslint/parser",
9 | });
10 |
11 | ruleTester.run("enforce", rule, {
12 | valid: [
13 | "function f(a, ...rest) {}",
14 | "const f = function(a, ...rest) {}",
15 | "const f = (a, ...rest) => {}",
16 | "({ method(a, ...rest) {} })",
17 | "class A { method(a, ...rest) {} }",
18 | "class A { constructor(a, ...rest) {} }",
19 | "(class { method(a, ...rest) {} })",
20 | "var a = 1",
21 | "var {a = 0} = obj",
22 | "var [a = 0] = ary",
23 | "({a = 0} = obj)",
24 | "([a = 0] = ary)",
25 | "function f({a = 0}) {}",
26 | "function f([a = 0]) {}",
27 | ],
28 | invalid: [
29 | {
30 | code: "async function f(a = 0) {}",
31 | errors: [{ messageId: "default-parameter", data: {} }],
32 | },
33 | {
34 | code: "const f = async function(a = 0) {}",
35 | errors: [{ messageId: "default-parameter" }],
36 | },
37 | {
38 | code: "const f = async (a = 0) => {}",
39 | errors: [{ messageId: "default-parameter" }],
40 | },
41 | {
42 | code: "({ async method(a = 0) {} })",
43 | errors: [{ messageId: "default-parameter" }],
44 | },
45 | {
46 | code: "class A { async method(a = 0) {} }",
47 | errors: [{ messageId: "default-parameter" }],
48 | },
49 | {
50 | code: "class A { constructor(a = 0) {} }",
51 | errors: [{ messageId: "default-parameter" }],
52 | },
53 | {
54 | code: "class A { constructor(readonly a = 0) {} }",
55 | errors: [{ messageId: "default-parameter" }],
56 | },
57 | {
58 | code: "class A { constructor(public readonly a = 0) {} }",
59 | errors: [{ messageId: "default-parameter" }],
60 | },
61 | {
62 | code: "class A { constructor(readonly a = 0) {} }",
63 | errors: [{ messageId: "default-parameter" }],
64 | },
65 | {
66 | code: "(class { async method(a = 0) {} })",
67 | errors: [{ messageId: "default-parameter" }],
68 | },
69 | {
70 | code: "async function f({a} = 0) {}",
71 | errors: [{ messageId: "default-parameter" }],
72 | },
73 | {
74 | code: "async function f([a] = 0) {}",
75 | errors: [{ messageId: "default-parameter" }],
76 | },
77 | ],
78 | });
79 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "ES2019",
4 | "module": "node16",
5 | "moduleResolution": "node16",
6 | "strict": true,
7 | "skipLibCheck": true,
8 | "esModuleInterop": true,
9 | "allowSyntheticDefaultImports": true,
10 | "experimentalDecorators": true,
11 | "outDir": "./dist"
12 | },
13 | "include": ["**/*.ts"],
14 | "exclude": ["node_modules", "dist", "tests/rules/paths"],
15 | }
16 |
--------------------------------------------------------------------------------