eslint-plugin-nima 1.2.4 → 1.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -18
- package/dist/constants/boolean-prefixes.js.map +1 -1
- package/dist/constants/consoles.js.map +1 -1
- package/dist/constants/hooks.js.map +1 -1
- package/dist/index.d.ts +25 -24
- package/dist/index.js +13 -57
- package/dist/index.js.map +1 -1
- package/dist/{models/boolean-naming-convention.model.d.ts → rules/boolean-naming-convention/config.d.ts} +11 -0
- package/dist/rules/boolean-naming-convention/config.js +70 -0
- package/dist/rules/boolean-naming-convention/config.js.map +1 -0
- package/dist/rules/boolean-naming-convention/create.d.ts +11 -0
- package/dist/rules/boolean-naming-convention/create.js +415 -0
- package/dist/rules/boolean-naming-convention/create.js.map +1 -0
- package/dist/rules/boolean-naming-convention/index.d.ts +2 -0
- package/dist/rules/boolean-naming-convention/index.js +22 -0
- package/dist/rules/boolean-naming-convention/index.js.map +1 -0
- package/dist/rules/{restrict-imports.d.ts → boolean-naming-convention/rule.d.ts} +2 -2
- package/dist/rules/boolean-naming-convention/rule.js +14 -0
- package/dist/rules/boolean-naming-convention/rule.js.map +1 -0
- package/dist/rules/index.d.ts +12 -0
- package/dist/rules/index.js +49 -0
- package/dist/rules/index.js.map +1 -0
- package/dist/rules/no-handler-suffix/config.d.ts +14 -0
- package/dist/rules/no-handler-suffix/config.js +23 -0
- package/dist/rules/no-handler-suffix/config.js.map +1 -0
- package/dist/rules/no-handler-suffix/create.d.ts +8 -0
- package/dist/rules/no-handler-suffix/create.js +71 -0
- package/dist/rules/no-handler-suffix/create.js.map +1 -0
- package/dist/rules/no-handler-suffix/index.d.ts +2 -0
- package/dist/rules/no-handler-suffix/index.js +22 -0
- package/dist/rules/no-handler-suffix/index.js.map +1 -0
- package/dist/rules/{no-handler-suffix.d.ts → no-handler-suffix/rule.d.ts} +2 -2
- package/dist/rules/no-handler-suffix/rule.js +14 -0
- package/dist/rules/no-handler-suffix/rule.js.map +1 -0
- package/dist/rules/no-objects-in-deps/config.d.ts +14 -0
- package/dist/rules/no-objects-in-deps/config.js +22 -0
- package/dist/rules/no-objects-in-deps/config.js.map +1 -0
- package/dist/rules/no-objects-in-deps/create.d.ts +6 -0
- package/dist/rules/no-objects-in-deps/create.js +124 -0
- package/dist/rules/no-objects-in-deps/create.js.map +1 -0
- package/dist/rules/no-objects-in-deps/index.d.ts +2 -0
- package/dist/rules/no-objects-in-deps/index.js +22 -0
- package/dist/rules/no-objects-in-deps/index.js.map +1 -0
- package/dist/rules/{no-objects-in-deps.d.ts → no-objects-in-deps/rule.d.ts} +2 -2
- package/dist/rules/no-objects-in-deps/rule.js +14 -0
- package/dist/rules/no-objects-in-deps/rule.js.map +1 -0
- package/dist/rules/params-naming-convention/config.d.ts +21 -0
- package/dist/rules/params-naming-convention/config.js +55 -0
- package/dist/rules/params-naming-convention/config.js.map +1 -0
- package/dist/rules/params-naming-convention/create.d.ts +6 -0
- package/dist/rules/params-naming-convention/create.js +49 -0
- package/dist/rules/params-naming-convention/create.js.map +1 -0
- package/dist/rules/params-naming-convention/index.d.ts +2 -0
- package/dist/rules/params-naming-convention/index.js +22 -0
- package/dist/rules/params-naming-convention/index.js.map +1 -0
- package/dist/rules/{prefer-react-fc.d.ts → params-naming-convention/rule.d.ts} +2 -2
- package/dist/rules/params-naming-convention/rule.js +14 -0
- package/dist/rules/params-naming-convention/rule.js.map +1 -0
- package/dist/{models/prefer-arrow-functions.model.d.ts → rules/prefer-arrow-functions/config.d.ts} +10 -0
- package/dist/rules/prefer-arrow-functions/config.js +67 -0
- package/dist/rules/prefer-arrow-functions/config.js.map +1 -0
- package/dist/rules/prefer-arrow-functions/create.d.ts +9 -0
- package/dist/rules/prefer-arrow-functions/create.js +150 -0
- package/dist/rules/prefer-arrow-functions/create.js.map +1 -0
- package/dist/rules/prefer-arrow-functions/index.d.ts +2 -0
- package/dist/rules/prefer-arrow-functions/index.js +22 -0
- package/dist/rules/prefer-arrow-functions/index.js.map +1 -0
- package/dist/rules/{restrict-function-usage.d.ts → prefer-arrow-functions/rule.d.ts} +2 -2
- package/dist/rules/prefer-arrow-functions/rule.js +14 -0
- package/dist/rules/prefer-arrow-functions/rule.js.map +1 -0
- package/dist/rules/prefer-export-under-component/config.d.ts +14 -0
- package/dist/rules/prefer-export-under-component/config.js +23 -0
- package/dist/rules/prefer-export-under-component/config.js.map +1 -0
- package/dist/rules/prefer-export-under-component/create.d.ts +8 -0
- package/dist/rules/prefer-export-under-component/create.js +99 -0
- package/dist/rules/prefer-export-under-component/create.js.map +1 -0
- package/dist/rules/prefer-export-under-component/index.d.ts +2 -0
- package/dist/rules/prefer-export-under-component/index.js +22 -0
- package/dist/rules/prefer-export-under-component/index.js.map +1 -0
- package/dist/rules/{prefer-react-with-hooks.d.ts → prefer-export-under-component/rule.d.ts} +2 -2
- package/dist/rules/prefer-export-under-component/rule.js +14 -0
- package/dist/rules/prefer-export-under-component/rule.js.map +1 -0
- package/dist/rules/prefer-react-fc/config.d.ts +19 -0
- package/dist/rules/prefer-react-fc/config.js +43 -0
- package/dist/rules/prefer-react-fc/config.js.map +1 -0
- package/dist/rules/prefer-react-fc/create.d.ts +8 -0
- package/dist/rules/prefer-react-fc/create.js +166 -0
- package/dist/rules/prefer-react-fc/create.js.map +1 -0
- package/dist/rules/prefer-react-fc/index.d.ts +2 -0
- package/dist/rules/prefer-react-fc/index.js +22 -0
- package/dist/rules/prefer-react-fc/index.js.map +1 -0
- package/dist/rules/{prefer-arrow-functions.d.ts → prefer-react-fc/rule.d.ts} +2 -2
- package/dist/rules/prefer-react-fc/rule.js +14 -0
- package/dist/rules/prefer-react-fc/rule.js.map +1 -0
- package/dist/rules/prefer-react-with-hooks/config.d.ts +15 -0
- package/dist/rules/prefer-react-with-hooks/config.js +25 -0
- package/dist/rules/prefer-react-with-hooks/config.js.map +1 -0
- package/dist/rules/prefer-react-with-hooks/create.d.ts +7 -0
- package/dist/rules/prefer-react-with-hooks/create.js +188 -0
- package/dist/rules/prefer-react-with-hooks/create.js.map +1 -0
- package/dist/rules/prefer-react-with-hooks/index.d.ts +2 -0
- package/dist/rules/prefer-react-with-hooks/index.js +22 -0
- package/dist/rules/prefer-react-with-hooks/index.js.map +1 -0
- package/dist/rules/{prefer-export-under-component.d.ts → prefer-react-with-hooks/rule.d.ts} +2 -2
- package/dist/rules/prefer-react-with-hooks/rule.js +14 -0
- package/dist/rules/prefer-react-with-hooks/rule.js.map +1 -0
- package/dist/rules/prefer-void-for-optional-param/config.d.ts +15 -0
- package/dist/rules/prefer-void-for-optional-param/config.js +25 -0
- package/dist/rules/prefer-void-for-optional-param/config.js.map +1 -0
- package/dist/rules/prefer-void-for-optional-param/create.d.ts +8 -0
- package/dist/rules/prefer-void-for-optional-param/create.js +188 -0
- package/dist/rules/prefer-void-for-optional-param/create.js.map +1 -0
- package/dist/rules/prefer-void-for-optional-param/index.d.ts +2 -0
- package/dist/rules/prefer-void-for-optional-param/index.js +22 -0
- package/dist/rules/prefer-void-for-optional-param/index.js.map +1 -0
- package/dist/rules/prefer-void-for-optional-param/rule.d.ts +5 -0
- package/dist/rules/prefer-void-for-optional-param/rule.js +14 -0
- package/dist/rules/prefer-void-for-optional-param/rule.js.map +1 -0
- package/dist/rules/restrict-console-methods/config.d.ts +18 -0
- package/dist/rules/restrict-console-methods/config.js +38 -0
- package/dist/rules/restrict-console-methods/config.js.map +1 -0
- package/dist/rules/restrict-console-methods/create.d.ts +6 -0
- package/dist/rules/restrict-console-methods/create.js +34 -0
- package/dist/rules/restrict-console-methods/create.js.map +1 -0
- package/dist/rules/restrict-console-methods/index.d.ts +2 -0
- package/dist/rules/restrict-console-methods/index.js +22 -0
- package/dist/rules/restrict-console-methods/index.js.map +1 -0
- package/dist/rules/restrict-console-methods/rule.d.ts +5 -0
- package/dist/rules/restrict-console-methods/rule.js +14 -0
- package/dist/rules/restrict-console-methods/rule.js.map +1 -0
- package/dist/rules/restrict-function-usage/config.d.ts +21 -0
- package/dist/rules/restrict-function-usage/config.js +62 -0
- package/dist/rules/restrict-function-usage/config.js.map +1 -0
- package/dist/rules/restrict-function-usage/create.d.ts +6 -0
- package/dist/rules/restrict-function-usage/create.js +76 -0
- package/dist/rules/restrict-function-usage/create.js.map +1 -0
- package/dist/rules/restrict-function-usage/index.d.ts +2 -0
- package/dist/rules/restrict-function-usage/index.js +22 -0
- package/dist/rules/restrict-function-usage/index.js.map +1 -0
- package/dist/rules/restrict-function-usage/rule.d.ts +5 -0
- package/dist/rules/restrict-function-usage/rule.js +14 -0
- package/dist/rules/restrict-function-usage/rule.js.map +1 -0
- package/dist/rules/restrict-imports/config.d.ts +22 -0
- package/dist/rules/restrict-imports/config.js +69 -0
- package/dist/rules/restrict-imports/config.js.map +1 -0
- package/dist/rules/restrict-imports/create.d.ts +6 -0
- package/dist/rules/restrict-imports/create.js +92 -0
- package/dist/rules/restrict-imports/create.js.map +1 -0
- package/dist/rules/restrict-imports/index.d.ts +2 -0
- package/dist/rules/restrict-imports/index.js +22 -0
- package/dist/rules/restrict-imports/index.js.map +1 -0
- package/dist/rules/restrict-imports/rule.d.ts +5 -0
- package/dist/rules/restrict-imports/rule.js +14 -0
- package/dist/rules/restrict-imports/rule.js.map +1 -0
- package/dist/utility/core.js.map +1 -1
- package/dist/utility/file-helpers.d.ts +6 -0
- package/dist/utility/file-helpers.js +32 -0
- package/dist/utility/file-helpers.js.map +1 -0
- package/dist/utility/function-helpers.d.ts +4 -4
- package/dist/utility/function-helpers.js +17 -15
- package/dist/utility/function-helpers.js.map +1 -1
- package/dist/utility/type-helpers.d.ts +4 -1
- package/dist/utility/type-helpers.js +6 -4
- package/dist/utility/type-helpers.js.map +1 -1
- package/package.json +75 -73
- package/dist/models/boolean-naming-convention.model.js +0 -11
- package/dist/models/boolean-naming-convention.model.js.map +0 -1
- package/dist/models/no-handler-suffix.model.d.ts +0 -4
- package/dist/models/no-handler-suffix.model.js +0 -8
- package/dist/models/no-handler-suffix.model.js.map +0 -1
- package/dist/models/no-objects-in-deps.model.d.ts +0 -4
- package/dist/models/no-objects-in-deps.model.js +0 -8
- package/dist/models/no-objects-in-deps.model.js.map +0 -1
- package/dist/models/params-naming-convention.model.d.ts +0 -11
- package/dist/models/params-naming-convention.model.js +0 -8
- package/dist/models/params-naming-convention.model.js.map +0 -1
- package/dist/models/prefer-arrow-functions.model.js +0 -10
- package/dist/models/prefer-arrow-functions.model.js.map +0 -1
- package/dist/models/prefer-export-under-component.model.d.ts +0 -4
- package/dist/models/prefer-export-under-component.model.js +0 -8
- package/dist/models/prefer-export-under-component.model.js.map +0 -1
- package/dist/models/prefer-react-fc.model.d.ts +0 -9
- package/dist/models/prefer-react-fc.model.js +0 -8
- package/dist/models/prefer-react-fc.model.js.map +0 -1
- package/dist/models/prefer-react-with-hooks.model.d.ts +0 -5
- package/dist/models/prefer-react-with-hooks.model.js +0 -9
- package/dist/models/prefer-react-with-hooks.model.js.map +0 -1
- package/dist/models/prefer-void-for-optional-param.model.d.ts +0 -5
- package/dist/models/prefer-void-for-optional-param.model.js +0 -9
- package/dist/models/prefer-void-for-optional-param.model.js.map +0 -1
- package/dist/models/restrict-console-methods.model.d.ts +0 -8
- package/dist/models/restrict-console-methods.model.js +0 -8
- package/dist/models/restrict-console-methods.model.js.map +0 -1
- package/dist/models/restrict-function-usage.model.d.ts +0 -11
- package/dist/models/restrict-function-usage.model.js +0 -8
- package/dist/models/restrict-function-usage.model.js.map +0 -1
- package/dist/models/restrict-imports.model.d.ts +0 -12
- package/dist/models/restrict-imports.model.js +0 -8
- package/dist/models/restrict-imports.model.js.map +0 -1
- package/dist/rules/boolean-naming-convention.d.ts +0 -5
- package/dist/rules/boolean-naming-convention.js +0 -379
- package/dist/rules/boolean-naming-convention.js.map +0 -1
- package/dist/rules/no-handler-suffix.js +0 -89
- package/dist/rules/no-handler-suffix.js.map +0 -1
- package/dist/rules/no-objects-in-deps.js +0 -90
- package/dist/rules/no-objects-in-deps.js.map +0 -1
- package/dist/rules/params-naming-convention.d.ts +0 -5
- package/dist/rules/params-naming-convention.js +0 -95
- package/dist/rules/params-naming-convention.js.map +0 -1
- package/dist/rules/prefer-arrow-functions.js +0 -208
- package/dist/rules/prefer-arrow-functions.js.map +0 -1
- package/dist/rules/prefer-export-under-component.js +0 -113
- package/dist/rules/prefer-export-under-component.js.map +0 -1
- package/dist/rules/prefer-react-fc.js +0 -204
- package/dist/rules/prefer-react-fc.js.map +0 -1
- package/dist/rules/prefer-react-with-hooks.js +0 -185
- package/dist/rules/prefer-react-with-hooks.js.map +0 -1
- package/dist/rules/prefer-void-for-optional-param.d.ts +0 -6
- package/dist/rules/prefer-void-for-optional-param.js +0 -188
- package/dist/rules/prefer-void-for-optional-param.js.map +0 -1
- package/dist/rules/restrict-console-methods.d.ts +0 -5
- package/dist/rules/restrict-console-methods.js +0 -66
- package/dist/rules/restrict-console-methods.js.map +0 -1
- package/dist/rules/restrict-function-usage.js +0 -121
- package/dist/rules/restrict-function-usage.js.map +0 -1
- package/dist/rules/restrict-imports.js +0 -135
- package/dist/rules/restrict-imports.js.map +0 -1
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rule = exports.name = void 0;
|
|
4
|
-
const params_naming_convention_model_1 = require("../models/params-naming-convention.model");
|
|
5
|
-
const utils_1 = require("@typescript-eslint/utils");
|
|
6
|
-
const core_1 = require("../utility/core");
|
|
7
|
-
const function_helpers_1 = require("../utility/function-helpers");
|
|
8
|
-
exports.name = "params-naming-convention";
|
|
9
|
-
exports.rule = (0, core_1.createRule)({
|
|
10
|
-
create: (context, [options]) => {
|
|
11
|
-
const { allowedParameters = 1, ignore = ["e"], ignoreFunctions = ["reduce"], ignorePrefixes = ["$"], } = options;
|
|
12
|
-
function checkParams(node) {
|
|
13
|
-
const parameters = node.params;
|
|
14
|
-
const fnName = (0, function_helpers_1.getFunctionName)(node);
|
|
15
|
-
if (parameters.length <= allowedParameters) {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
if (fnName && ignoreFunctions.includes(fnName)) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
if (parameters.length === 1 &&
|
|
22
|
-
parameters[0].type === utils_1.AST_NODE_TYPES.ObjectPattern)
|
|
23
|
-
return;
|
|
24
|
-
if (parameters.length > 1 &&
|
|
25
|
-
parameters[1].type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
26
|
-
parameters[1].name === "index") {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
const identifiers = parameters.filter((p) => p.type === utils_1.AST_NODE_TYPES.Identifier);
|
|
30
|
-
const parameterNames = identifiers.filter((p) => !ignore.includes(p.name) &&
|
|
31
|
-
!ignorePrefixes?.some((prefix) => p.name.startsWith(prefix)));
|
|
32
|
-
const suggestedParameters = ignorePrefixes.flatMap((ignoredPrefix) => parameterNames
|
|
33
|
-
.slice(-(parameterNames.length - allowedParameters))
|
|
34
|
-
.map((parameter) => ignoredPrefix + parameter.name));
|
|
35
|
-
if (parameterNames.length > 0) {
|
|
36
|
-
context.report({
|
|
37
|
-
data: {
|
|
38
|
-
count: suggestedParameters.length.toString(),
|
|
39
|
-
params: suggestedParameters.join(", "),
|
|
40
|
-
},
|
|
41
|
-
messageId: params_naming_convention_model_1.Messages.USE_OBJECT_PARAMETERS,
|
|
42
|
-
node,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return {
|
|
47
|
-
"FunctionDeclaration, FunctionExpression, ArrowFunctionExpression": checkParams,
|
|
48
|
-
};
|
|
49
|
-
},
|
|
50
|
-
defaultOptions: [
|
|
51
|
-
{
|
|
52
|
-
allowedParameters: 1,
|
|
53
|
-
ignore: ["e"],
|
|
54
|
-
ignoreFunctions: ["reduce"],
|
|
55
|
-
ignorePrefixes: ["$"],
|
|
56
|
-
},
|
|
57
|
-
],
|
|
58
|
-
meta: {
|
|
59
|
-
docs: {
|
|
60
|
-
description: "Enforce using a single object parameter for all functions",
|
|
61
|
-
recommended: false,
|
|
62
|
-
url: "https://github.com/NIMA-Enterprises/eslint-plugin-nima/blob/main/documentation/rules/params-naming-convention.md",
|
|
63
|
-
},
|
|
64
|
-
messages: {
|
|
65
|
-
[params_naming_convention_model_1.Messages.USE_OBJECT_PARAMETERS]: "NIMA: Function has {{count}} extra parameter(s). Either prefix them: {{params}}, or put all parameters in one object.",
|
|
66
|
-
},
|
|
67
|
-
schema: [
|
|
68
|
-
{
|
|
69
|
-
additionalProperties: false,
|
|
70
|
-
properties: {
|
|
71
|
-
allowedParameters: {
|
|
72
|
-
default: 1,
|
|
73
|
-
type: "number",
|
|
74
|
-
},
|
|
75
|
-
ignore: {
|
|
76
|
-
items: { type: "string" },
|
|
77
|
-
type: "array",
|
|
78
|
-
},
|
|
79
|
-
ignoreFunctions: {
|
|
80
|
-
items: { type: "string" },
|
|
81
|
-
type: "array",
|
|
82
|
-
},
|
|
83
|
-
ignorePrefixes: {
|
|
84
|
-
items: { type: "string" },
|
|
85
|
-
type: "array",
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
type: "object",
|
|
89
|
-
},
|
|
90
|
-
],
|
|
91
|
-
type: "problem",
|
|
92
|
-
},
|
|
93
|
-
name: exports.name,
|
|
94
|
-
});
|
|
95
|
-
//# sourceMappingURL=params-naming-convention.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"params-naming-convention.js","sourceRoot":"","sources":["../../src/rules/params-naming-convention.ts"],"names":[],"mappings":";;;AAAA,2FAAgF;AAChF,oDAAyE;AACzE,wCAA2C;AAC3C,gEAA4D;AAE/C,QAAA,IAAI,GAAG,0BAA0B,CAAC;AAElC,QAAA,IAAI,GAAG,IAAA,iBAAU,EAAoB;IAChD,MAAM,EAAE,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,EAAE,EAAE;QAC7B,MAAM,EACJ,iBAAiB,GAAG,CAAC,EACrB,MAAM,GAAG,CA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E,QAAQ,EAAE;wBACzB,IAAI,EAAE,OAAO;qBACd;oBACD,cAAc,EAAE;wBACd,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;wBACzB,IAAI,EAAE,OAAO;qBACd;iBACF;gBACD,IAAI,EAAE,QAAQ;aACf;SACF;QACD,IAAI,EAAE,SAAS;KAChB;IAED,IAAI,EAAJ,YAAI;CACL,CAAC,CAAC"}
|
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rule = exports.name = void 0;
|
|
4
|
-
const prefer_arrow_functions_model_1 = require("../models/prefer-arrow-functions.model");
|
|
5
|
-
const utils_1 = require("@typescript-eslint/utils");
|
|
6
|
-
const core_1 = require("../utility/core");
|
|
7
|
-
exports.name = "prefer-arrow-functions";
|
|
8
|
-
exports.rule = (0, core_1.createRule)({
|
|
9
|
-
create: (context, [options]) => {
|
|
10
|
-
const { allowAsync = true, allowConstructors = true, allowFunctionDeclarations = false, allowFunctionExpressions = false, allowGenerators = true, allowMethodDefinitions = false, } = options;
|
|
11
|
-
const sourceCode = context.sourceCode;
|
|
12
|
-
const shouldSkipFunction = (node) => {
|
|
13
|
-
if (allowConstructors &&
|
|
14
|
-
node.parent?.type === utils_1.AST_NODE_TYPES.MethodDefinition &&
|
|
15
|
-
node.parent.kind === "constructor") {
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
if (allowGenerators && node?.generator) {
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
if (allowAsync && node?.async) {
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
if (node.type === utils_1.AST_NODE_TYPES.TSEmptyBodyFunctionExpression) {
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
return false;
|
|
28
|
-
};
|
|
29
|
-
const generateArrowFunction = (node) => {
|
|
30
|
-
const asyncKeyword = node.async ? "async " : "";
|
|
31
|
-
const params = node.params.length === 0
|
|
32
|
-
? "()"
|
|
33
|
-
: node.params.length === 1 &&
|
|
34
|
-
node.params[0].type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
35
|
-
!node.params[0].typeAnnotation
|
|
36
|
-
? sourceCode.getText(node.params[0])
|
|
37
|
-
: `(${node.params.map((p) => sourceCode.getText(p)).join(", ")})`;
|
|
38
|
-
const returnType = node.returnType
|
|
39
|
-
? `: ${sourceCode.getText(node.returnType.typeAnnotation)}`
|
|
40
|
-
: "";
|
|
41
|
-
const body = node.body ? sourceCode.getText(node.body) : "";
|
|
42
|
-
return `${asyncKeyword}${params}${returnType} => ${body}`;
|
|
43
|
-
};
|
|
44
|
-
return {
|
|
45
|
-
FunctionDeclaration: (node) => {
|
|
46
|
-
if (allowFunctionDeclarations || shouldSkipFunction(node)) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
context.report({
|
|
50
|
-
fix: (fixer) => {
|
|
51
|
-
if (!node.id)
|
|
52
|
-
return null;
|
|
53
|
-
const functionName = node.id.name;
|
|
54
|
-
const arrowFunction = generateArrowFunction(node);
|
|
55
|
-
let replacement;
|
|
56
|
-
if (node.parent?.type === utils_1.AST_NODE_TYPES.ExportDefaultDeclaration) {
|
|
57
|
-
const constDeclaration = `const ${functionName} = ${arrowFunction}`;
|
|
58
|
-
const exportDeclaration = `export default ${functionName}`;
|
|
59
|
-
replacement = `${constDeclaration};\n${exportDeclaration}`;
|
|
60
|
-
return fixer.replaceText(node.parent, replacement);
|
|
61
|
-
}
|
|
62
|
-
else if (node.parent?.type === utils_1.AST_NODE_TYPES.ExportNamedDeclaration) {
|
|
63
|
-
replacement = `export const ${functionName} = ${arrowFunction}`;
|
|
64
|
-
return fixer.replaceText(node.parent, replacement);
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
replacement = `const ${functionName} = ${arrowFunction}`;
|
|
68
|
-
return fixer.replaceText(node, replacement);
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
messageId: prefer_arrow_functions_model_1.Messages.PREFER_ARROW_FUNCTIONS,
|
|
72
|
-
node: node.id || node,
|
|
73
|
-
});
|
|
74
|
-
},
|
|
75
|
-
FunctionExpression: (node) => {
|
|
76
|
-
if ((node.parent?.type === utils_1.AST_NODE_TYPES.MethodDefinition ||
|
|
77
|
-
(node.parent?.type === utils_1.AST_NODE_TYPES.Property &&
|
|
78
|
-
(node.parent.method || !node.parent.method))) &&
|
|
79
|
-
allowMethodDefinitions) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
if (node.parent?.type === utils_1.AST_NODE_TYPES.Property &&
|
|
83
|
-
node.parent.method) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
if (allowFunctionExpressions || shouldSkipFunction(node)) {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
context.report({
|
|
90
|
-
fix: (fixer) => {
|
|
91
|
-
const arrowFunction = generateArrowFunction(node);
|
|
92
|
-
return fixer.replaceText(node, arrowFunction);
|
|
93
|
-
},
|
|
94
|
-
messageId: prefer_arrow_functions_model_1.Messages.PREFER_ARROW_FUNCTION_EXPRESSION,
|
|
95
|
-
node,
|
|
96
|
-
});
|
|
97
|
-
},
|
|
98
|
-
MethodDefinition: (node) => {
|
|
99
|
-
if (allowMethodDefinitions ||
|
|
100
|
-
node.kind === "constructor" ||
|
|
101
|
-
node.kind === "get" ||
|
|
102
|
-
node.kind === "set") {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
if (node.value.type === utils_1.AST_NODE_TYPES.FunctionExpression &&
|
|
106
|
-
!shouldSkipFunction(node.value)) {
|
|
107
|
-
context.report({
|
|
108
|
-
fix: (fixer) => {
|
|
109
|
-
const key = sourceCode.getText(node.key);
|
|
110
|
-
const arrowFunction = generateArrowFunction(node.value);
|
|
111
|
-
const static_ = node.static ? "static " : "";
|
|
112
|
-
const replacement = `${static_}${key} = ${arrowFunction}`;
|
|
113
|
-
return fixer.replaceText(node, replacement);
|
|
114
|
-
},
|
|
115
|
-
messageId: prefer_arrow_functions_model_1.Messages.PREFER_ARROW_METHOD,
|
|
116
|
-
node: node.key,
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
Property: (node) => {
|
|
121
|
-
if (allowMethodDefinitions ||
|
|
122
|
-
!node.method ||
|
|
123
|
-
node.kind === "get" ||
|
|
124
|
-
node.kind === "set" ||
|
|
125
|
-
node.value.type !== utils_1.AST_NODE_TYPES.FunctionExpression) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
const functionNode = node.value;
|
|
129
|
-
if (!shouldSkipFunction(functionNode)) {
|
|
130
|
-
context.report({
|
|
131
|
-
fix: (fixer) => {
|
|
132
|
-
const key = sourceCode.getText(node.key);
|
|
133
|
-
const arrowFunction = generateArrowFunction(functionNode);
|
|
134
|
-
const replacement = `${key}: ${arrowFunction}`;
|
|
135
|
-
return fixer.replaceText(node, replacement);
|
|
136
|
-
},
|
|
137
|
-
messageId: prefer_arrow_functions_model_1.Messages.PREFER_ARROW_METHOD,
|
|
138
|
-
node: node.key,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
};
|
|
143
|
-
},
|
|
144
|
-
defaultOptions: [
|
|
145
|
-
{
|
|
146
|
-
allowAsync: true,
|
|
147
|
-
allowConstructors: true,
|
|
148
|
-
allowFunctionDeclarations: false,
|
|
149
|
-
allowFunctionExpressions: false,
|
|
150
|
-
allowGenerators: true,
|
|
151
|
-
allowMethodDefinitions: false,
|
|
152
|
-
},
|
|
153
|
-
],
|
|
154
|
-
meta: {
|
|
155
|
-
docs: {
|
|
156
|
-
description: "Prefer arrow functions over function declarations and expressions",
|
|
157
|
-
recommended: true,
|
|
158
|
-
url: "https://github.com/NIMA-Enterprises/eslint-plugin-nima/blob/main/documentation/rules/prefer-arrow-functions.md",
|
|
159
|
-
},
|
|
160
|
-
fixable: "code",
|
|
161
|
-
messages: {
|
|
162
|
-
[prefer_arrow_functions_model_1.Messages.PREFER_ARROW_FUNCTION_EXPRESSION]: "NIMA: Prefer arrow functions over function expressions.",
|
|
163
|
-
[prefer_arrow_functions_model_1.Messages.PREFER_ARROW_FUNCTIONS]: "NIMA: Prefer arrow functions over function declarations.",
|
|
164
|
-
[prefer_arrow_functions_model_1.Messages.PREFER_ARROW_METHOD]: "NIMA: Prefer arrow functions over method definitions.",
|
|
165
|
-
},
|
|
166
|
-
schema: [
|
|
167
|
-
{
|
|
168
|
-
additionalProperties: false,
|
|
169
|
-
properties: {
|
|
170
|
-
allowAsync: {
|
|
171
|
-
default: true,
|
|
172
|
-
description: "Allow async functions (they can still be arrow functions)",
|
|
173
|
-
type: "boolean",
|
|
174
|
-
},
|
|
175
|
-
allowConstructors: {
|
|
176
|
-
default: true,
|
|
177
|
-
description: "Allow constructor functions",
|
|
178
|
-
type: "boolean",
|
|
179
|
-
},
|
|
180
|
-
allowFunctionDeclarations: {
|
|
181
|
-
default: false,
|
|
182
|
-
description: "Allow function declarations (function name() {})",
|
|
183
|
-
type: "boolean",
|
|
184
|
-
},
|
|
185
|
-
allowFunctionExpressions: {
|
|
186
|
-
default: false,
|
|
187
|
-
description: "Allow function expressions (const name = function() {})",
|
|
188
|
-
type: "boolean",
|
|
189
|
-
},
|
|
190
|
-
allowGenerators: {
|
|
191
|
-
default: true,
|
|
192
|
-
description: "Allow generator functions",
|
|
193
|
-
type: "boolean",
|
|
194
|
-
},
|
|
195
|
-
allowMethodDefinitions: {
|
|
196
|
-
default: false,
|
|
197
|
-
description: "Allow method definitions in classes and objects",
|
|
198
|
-
type: "boolean",
|
|
199
|
-
},
|
|
200
|
-
},
|
|
201
|
-
type: "object",
|
|
202
|
-
},
|
|
203
|
-
],
|
|
204
|
-
type: "suggestion",
|
|
205
|
-
},
|
|
206
|
-
name: exports.name,
|
|
207
|
-
});
|
|
208
|
-
//# sourceMappingURL=prefer-arrow-functions.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{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IAAI,EAAE,QAAQ;aACf;SACF;QACD,IAAI,EAAE,YAAY;KACnB;IACD,IAAI,EAAJ,YAAI;CACL,CAAC,CAAC"}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rule = exports.name = void 0;
|
|
4
|
-
const prefer_export_under_component_model_1 = require("../models/prefer-export-under-component.model");
|
|
5
|
-
const utils_1 = require("@typescript-eslint/utils");
|
|
6
|
-
const core_1 = require("../utility/core");
|
|
7
|
-
const function_helpers_1 = require("../utility/function-helpers");
|
|
8
|
-
exports.name = "prefer-export-under-component";
|
|
9
|
-
exports.rule = (0, core_1.createRule)({
|
|
10
|
-
create: (context) => {
|
|
11
|
-
return {
|
|
12
|
-
ExportDefaultDeclaration: (node) => {
|
|
13
|
-
if (node.declaration.type !== utils_1.AST_NODE_TYPES.FunctionDeclaration) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
const fnName = node.declaration.id?.name;
|
|
17
|
-
if (fnName &&
|
|
18
|
-
(0, function_helpers_1.isComponentFunction)(fnName) &&
|
|
19
|
-
(0, function_helpers_1.isReactReturn)(node.declaration)) {
|
|
20
|
-
context.report({
|
|
21
|
-
data: {
|
|
22
|
-
fnName,
|
|
23
|
-
},
|
|
24
|
-
fix: (fixer) => {
|
|
25
|
-
const sourceCode = context.sourceCode;
|
|
26
|
-
const text = sourceCode.getText(node);
|
|
27
|
-
const newText = text.replace(/^export\s+default\s+/, "");
|
|
28
|
-
return [
|
|
29
|
-
fixer.replaceText(node, newText),
|
|
30
|
-
fixer.insertTextAfter(node, `\n\nexport { ${fnName} };`),
|
|
31
|
-
];
|
|
32
|
-
},
|
|
33
|
-
messageId: prefer_export_under_component_model_1.Messages.EXPORT_BELOW_COMPONENT,
|
|
34
|
-
node,
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
ExportNamedDeclaration: (node) => {
|
|
39
|
-
if (!node.declaration)
|
|
40
|
-
return;
|
|
41
|
-
if (node.declaration.type === utils_1.AST_NODE_TYPES.VariableDeclaration) {
|
|
42
|
-
const declarator = node.declaration.declarations[0];
|
|
43
|
-
if (!declarator.init)
|
|
44
|
-
return;
|
|
45
|
-
const fnNode = declarator.init;
|
|
46
|
-
if (fnNode.type !== utils_1.AST_NODE_TYPES.ArrowFunctionExpression &&
|
|
47
|
-
fnNode.type !== utils_1.AST_NODE_TYPES.FunctionExpression) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const fnName = (0, function_helpers_1.getFunctionName)(fnNode);
|
|
51
|
-
if (fnName && (0, function_helpers_1.isComponentFunction)(fnName) && (0, function_helpers_1.isReactReturn)(fnNode)) {
|
|
52
|
-
context.report({
|
|
53
|
-
data: {
|
|
54
|
-
fnName,
|
|
55
|
-
},
|
|
56
|
-
fix: (fixer) => {
|
|
57
|
-
const sourceCode = context.sourceCode;
|
|
58
|
-
const text = sourceCode.getText(node);
|
|
59
|
-
const newText = text.replace(/^export\s+/, "");
|
|
60
|
-
return [
|
|
61
|
-
fixer.replaceText(node, newText),
|
|
62
|
-
fixer.insertTextAfter(node, `\n\nexport { ${fnName} };`),
|
|
63
|
-
];
|
|
64
|
-
},
|
|
65
|
-
messageId: prefer_export_under_component_model_1.Messages.EXPORT_BELOW_COMPONENT,
|
|
66
|
-
node,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
FunctionDeclaration: (node) => {
|
|
72
|
-
if (node.parent?.type !== utils_1.AST_NODE_TYPES.ExportNamedDeclaration) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
const fnName = (0, function_helpers_1.getFunctionName)(node);
|
|
76
|
-
if (fnName && (0, function_helpers_1.isComponentFunction)(fnName) && (0, function_helpers_1.isReactReturn)(node)) {
|
|
77
|
-
context.report({
|
|
78
|
-
data: {
|
|
79
|
-
fnName,
|
|
80
|
-
},
|
|
81
|
-
fix: (fixer) => {
|
|
82
|
-
const sourceCode = context.sourceCode;
|
|
83
|
-
const text = sourceCode.getText(node.parent);
|
|
84
|
-
const newText = text.replace(/^export\s+/, "");
|
|
85
|
-
return [
|
|
86
|
-
fixer.replaceText(node.parent, newText),
|
|
87
|
-
fixer.insertTextAfter(node.parent, `\n\nexport { ${fnName} };`),
|
|
88
|
-
];
|
|
89
|
-
},
|
|
90
|
-
messageId: prefer_export_under_component_model_1.Messages.EXPORT_BELOW_COMPONENT,
|
|
91
|
-
node: node.parent,
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
},
|
|
97
|
-
defaultOptions: [],
|
|
98
|
-
meta: {
|
|
99
|
-
docs: {
|
|
100
|
-
description: "Enforce separate declaration and export for React components",
|
|
101
|
-
recommended: true,
|
|
102
|
-
url: "https://github.com/NIMA-Enterprises/eslint-plugin-nima/blob/main/documentation/rules/prefer-export-under-component.md",
|
|
103
|
-
},
|
|
104
|
-
fixable: "code",
|
|
105
|
-
messages: {
|
|
106
|
-
[prefer_export_under_component_model_1.Messages.EXPORT_BELOW_COMPONENT]: "NIMA: Declare React component '{{ fnName }}' separately from its export statement",
|
|
107
|
-
},
|
|
108
|
-
schema: [],
|
|
109
|
-
type: "problem",
|
|
110
|
-
},
|
|
111
|
-
name: exports.name,
|
|
112
|
-
});
|
|
113
|
-
//# sourceMappingURL=prefer-export-under-component.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-export-under-component.js","sourceRoot":"","sources":["../../src/rules/prefer-export-under-component.ts"],"names":[],"mappings":";;;AAAA,qGAAgF;AAChF,oDAAyE;AACzE,wCAA2C;AAC3C,gEAImC;AAEtB,QAAA,IAAI,GAAG,+BAA+B,CAAC;AAEvC,QAAA,IAAI,GAAG,IAAA,iBAAU,EAAoB;IAChD,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE;QAClB,OAAO;YACL,wBAA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CAAC,EAAE,CAAC;oBACjE,OAAO,CAAC,MAAM,CAAC;wBACb,IAAI,EAAE;4BACJ,MAAM;yBACP;wBACD,GAAG,EAAE,CAAC,KAAK,EAAE,EAAE;4BACb,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;4BACtC,MAAM,IAAI,GAAG,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;4BAC7C,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;4BAE/C,OAAO;gCACL,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC;gCACvC,KAAK,CAAC,eAAe,CAAC,IAAI,CAAC,MAAM,EAAE,gBAAgB,MAAM,KAAK,CAAC;6BAChE,CAAC;wBACJ,CAAC;wBACD,SAAS,EAAE,8CAAQ,CAAC,sBAAsB;wBAC1C,IAAI,EAAE,IAAI,CAAC,MAAM;qBAClB,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;IACD,cAAc,EAAE,EAAE;IAElB,IAAI,EAAE;QACJ,IAAI,EAAE;YACJ,WAAW,EACT,8DAA8D;YAChE,WAAW,EAAE,IAAI;YACjB,GAAG,EAAE,uHAAuH;SAC7H;QACD,OAAO,EAAE,MAAM;QACf,QAAQ,EAAE;YACR,CAAC,8CAAQ,CAAC,sBAAsB,CAAC,EAC/B,mFAAmF;SACtF;QACD,MAAM,EAAE,EAAE;QACV,IAAI,EAAE,SAAS;KAChB;IAED,IAAI,EAAJ,YAAI;CACL,CAAC,CAAC"}
|
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rule = exports.name = void 0;
|
|
4
|
-
const prefer_react_fc_model_1 = require("../models/prefer-react-fc.model");
|
|
5
|
-
const utils_1 = require("@typescript-eslint/utils");
|
|
6
|
-
const core_1 = require("../utility/core");
|
|
7
|
-
const function_helpers_1 = require("../utility/function-helpers");
|
|
8
|
-
exports.name = "prefer-react-fc";
|
|
9
|
-
exports.rule = (0, core_1.createRule)({
|
|
10
|
-
create(context) {
|
|
11
|
-
function isReactComponent(node) {
|
|
12
|
-
const fnName = (0, function_helpers_1.getFunctionName)(node);
|
|
13
|
-
if (!(0, function_helpers_1.isComponentFunction)(fnName)) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
return hasJSXReturn(node);
|
|
17
|
-
}
|
|
18
|
-
function hasJSXReturn(node) {
|
|
19
|
-
if (!node.body) {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
if (node.body.type === utils_1.AST_NODE_TYPES.BlockStatement) {
|
|
23
|
-
return checkBlockForJSX(node.body);
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
return isJSXExpression(node.body);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
function checkBlockForJSX(block) {
|
|
30
|
-
for (const statement of block.body) {
|
|
31
|
-
if (statement.type === utils_1.AST_NODE_TYPES.ReturnStatement &&
|
|
32
|
-
statement.argument) {
|
|
33
|
-
if (isJSXExpression(statement.argument)) {
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
if (statement.type === utils_1.AST_NODE_TYPES.IfStatement) {
|
|
38
|
-
if (statement.consequent.type === utils_1.AST_NODE_TYPES.BlockStatement &&
|
|
39
|
-
checkBlockForJSX(statement.consequent)) {
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
if (statement.alternate?.type === utils_1.AST_NODE_TYPES.BlockStatement &&
|
|
43
|
-
checkBlockForJSX(statement.alternate)) {
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
function isJSXExpression(node) {
|
|
51
|
-
return (node.type === utils_1.AST_NODE_TYPES.JSXElement ||
|
|
52
|
-
node.type === utils_1.AST_NODE_TYPES.JSXFragment ||
|
|
53
|
-
(node.type === utils_1.AST_NODE_TYPES.ConditionalExpression &&
|
|
54
|
-
(isJSXExpression(node.consequent) ||
|
|
55
|
-
isJSXExpression(node.alternate))) ||
|
|
56
|
-
(node.type === utils_1.AST_NODE_TYPES.LogicalExpression &&
|
|
57
|
-
isJSXExpression(node.right)));
|
|
58
|
-
}
|
|
59
|
-
function hasReactFCAnnotation(node) {
|
|
60
|
-
if (node.parent?.type === utils_1.AST_NODE_TYPES.VariableDeclarator &&
|
|
61
|
-
node.parent.id.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
62
|
-
node.parent.id.typeAnnotation) {
|
|
63
|
-
const typeAnnotation = node.parent.id.typeAnnotation.typeAnnotation;
|
|
64
|
-
return containsReactFCType(typeAnnotation);
|
|
65
|
-
}
|
|
66
|
-
if (node.type === utils_1.AST_NODE_TYPES.FunctionDeclaration && node.returnType) {
|
|
67
|
-
return containsReactFCType(node.returnType.typeAnnotation);
|
|
68
|
-
}
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
function hasCustomComponentTypeAnnotation(node) {
|
|
72
|
-
if (node.parent?.type === utils_1.AST_NODE_TYPES.VariableDeclarator &&
|
|
73
|
-
node.parent.id.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
74
|
-
node.parent.id.typeAnnotation) {
|
|
75
|
-
const typeAnnotation = node.parent.id.typeAnnotation.typeAnnotation;
|
|
76
|
-
return hasCustomComponentType(typeAnnotation);
|
|
77
|
-
}
|
|
78
|
-
if (node.type === utils_1.AST_NODE_TYPES.FunctionDeclaration && node.returnType) {
|
|
79
|
-
return hasCustomComponentType(node.returnType.typeAnnotation);
|
|
80
|
-
}
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
function hasCustomComponentType(typeNode) {
|
|
84
|
-
if (typeNode.type === utils_1.AST_NODE_TYPES.TSIntersectionType) {
|
|
85
|
-
return typeNode.types.some((type) => isCustomComponentType(type));
|
|
86
|
-
}
|
|
87
|
-
if (typeNode.type === utils_1.AST_NODE_TYPES.TSUnionType) {
|
|
88
|
-
return typeNode.types.some((type) => isCustomComponentType(type));
|
|
89
|
-
}
|
|
90
|
-
return isCustomComponentType(typeNode);
|
|
91
|
-
}
|
|
92
|
-
function isCustomComponentType(typeNode) {
|
|
93
|
-
if (typeNode.type === utils_1.AST_NODE_TYPES.TSTypeReference) {
|
|
94
|
-
const typeName = typeNode.typeName;
|
|
95
|
-
if (typeName.type === utils_1.AST_NODE_TYPES.TSQualifiedName) {
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
if (typeName.type === utils_1.AST_NODE_TYPES.Identifier) {
|
|
99
|
-
const name = typeName.name;
|
|
100
|
-
if (name === "FC" || name === "FunctionComponent") {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
return name.endsWith("Component");
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
function containsReactFCType(typeNode) {
|
|
109
|
-
if (typeNode.type === utils_1.AST_NODE_TYPES.TSIntersectionType) {
|
|
110
|
-
return typeNode.types.some((type) => isReactFCType(type));
|
|
111
|
-
}
|
|
112
|
-
if (typeNode.type === utils_1.AST_NODE_TYPES.TSUnionType) {
|
|
113
|
-
return typeNode.types.some((type) => isReactFCType(type));
|
|
114
|
-
}
|
|
115
|
-
return isReactFCType(typeNode);
|
|
116
|
-
}
|
|
117
|
-
function isReactFCType(typeNode) {
|
|
118
|
-
if (typeNode.type === utils_1.AST_NODE_TYPES.TSTypeReference &&
|
|
119
|
-
typeNode.typeName.type === utils_1.AST_NODE_TYPES.TSQualifiedName &&
|
|
120
|
-
typeNode.typeName.left.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
121
|
-
typeNode.typeName.left.name === "React" &&
|
|
122
|
-
typeNode.typeName.right.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
123
|
-
(typeNode.typeName.right.name === "FC" ||
|
|
124
|
-
typeNode.typeName.right.name === "FunctionComponent")) {
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
if (typeNode.type === utils_1.AST_NODE_TYPES.TSTypeReference &&
|
|
128
|
-
typeNode.typeName.type === utils_1.AST_NODE_TYPES.Identifier &&
|
|
129
|
-
(typeNode.typeName.name === "FC" ||
|
|
130
|
-
typeNode.typeName.name === "FunctionComponent")) {
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
return false;
|
|
134
|
-
}
|
|
135
|
-
return {
|
|
136
|
-
ArrowFunctionExpression(node) {
|
|
137
|
-
if (isReactComponent(node) &&
|
|
138
|
-
!hasReactFCAnnotation(node) &&
|
|
139
|
-
!hasCustomComponentTypeAnnotation(node)) {
|
|
140
|
-
context.report({
|
|
141
|
-
messageId: prefer_react_fc_model_1.Messages.REQUIRE_REACT_FC,
|
|
142
|
-
node,
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
FunctionDeclaration(node) {
|
|
147
|
-
if (isReactComponent(node) &&
|
|
148
|
-
!hasReactFCAnnotation(node) &&
|
|
149
|
-
!hasCustomComponentTypeAnnotation(node)) {
|
|
150
|
-
context.report({
|
|
151
|
-
messageId: prefer_react_fc_model_1.Messages.REQUIRE_REACT_FC,
|
|
152
|
-
node: node.id || node,
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
},
|
|
156
|
-
FunctionExpression(node) {
|
|
157
|
-
if (isReactComponent(node) &&
|
|
158
|
-
!hasReactFCAnnotation(node) &&
|
|
159
|
-
!hasCustomComponentTypeAnnotation(node)) {
|
|
160
|
-
context.report({
|
|
161
|
-
messageId: prefer_react_fc_model_1.Messages.REQUIRE_REACT_FC,
|
|
162
|
-
node,
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
},
|
|
166
|
-
};
|
|
167
|
-
},
|
|
168
|
-
defaultOptions: [
|
|
169
|
-
{
|
|
170
|
-
allowArrowFunctions: true,
|
|
171
|
-
allowFunctionDeclarations: true,
|
|
172
|
-
},
|
|
173
|
-
],
|
|
174
|
-
meta: {
|
|
175
|
-
docs: {
|
|
176
|
-
description: "Enforce React.FC type annotation for React component functions",
|
|
177
|
-
recommended: true,
|
|
178
|
-
url: "https://github.com/NIMA-Enterprises/eslint-plugin-nima/blob/main/documentation/rules/prefer-react-fc.md",
|
|
179
|
-
},
|
|
180
|
-
fixable: "code",
|
|
181
|
-
messages: {
|
|
182
|
-
[prefer_react_fc_model_1.Messages.REQUIRE_REACT_FC]: "NIMA: Component functions must use React.FC type annotation.",
|
|
183
|
-
},
|
|
184
|
-
schema: [
|
|
185
|
-
{
|
|
186
|
-
additionalProperties: false,
|
|
187
|
-
properties: {
|
|
188
|
-
allowArrowFunctions: {
|
|
189
|
-
default: true,
|
|
190
|
-
type: "boolean",
|
|
191
|
-
},
|
|
192
|
-
allowFunctionDeclarations: {
|
|
193
|
-
default: true,
|
|
194
|
-
type: "boolean",
|
|
195
|
-
},
|
|
196
|
-
},
|
|
197
|
-
type: "object",
|
|
198
|
-
},
|
|
199
|
-
],
|
|
200
|
-
type: "problem",
|
|
201
|
-
},
|
|
202
|
-
name: exports.name,
|
|
203
|
-
});
|
|
204
|
-
//# sourceMappingURL=prefer-react-fc.js.map
|