eslint-plugin-nima 0.0.0
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 +1 -0
- package/dist/configs/recommended.d.ts +5 -0
- package/dist/configs/recommended.js +6 -0
- package/dist/configs/recommended.js.map +1 -0
- package/dist/constants/boolean-prefixes.d.ts +1 -0
- package/dist/constants/boolean-prefixes.js +12 -0
- package/dist/constants/boolean-prefixes.js.map +1 -0
- package/dist/constants/hooks.d.ts +2 -0
- package/dist/constants/hooks.js +26 -0
- package/dist/constants/hooks.js.map +1 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/rules/boolean-naming-convention.d.ts +9 -0
- package/dist/rules/boolean-naming-convention.js +307 -0
- package/dist/rules/boolean-naming-convention.js.map +1 -0
- package/dist/rules/no-handler-suffix.d.ts +3 -0
- package/dist/rules/no-handler-suffix.js +82 -0
- package/dist/rules/no-handler-suffix.js.map +1 -0
- package/dist/rules/no-objects-in-deps.d.ts +3 -0
- package/dist/rules/no-objects-in-deps.js +77 -0
- package/dist/rules/no-objects-in-deps.js.map +1 -0
- package/dist/rules/params-naming-convention.d.ts +8 -0
- package/dist/rules/params-naming-convention.js +92 -0
- package/dist/rules/params-naming-convention.js.map +1 -0
- package/dist/rules/prefer-arrow-functions.d.ts +10 -0
- package/dist/rules/prefer-arrow-functions.js +205 -0
- package/dist/rules/prefer-arrow-functions.js.map +1 -0
- package/dist/rules/prefer-react-fc.d.ts +6 -0
- package/dist/rules/prefer-react-fc.js +206 -0
- package/dist/rules/prefer-react-fc.js.map +1 -0
- package/dist/rules/prefer-react-with-hooks.d.ts +5 -0
- package/dist/rules/prefer-react-with-hooks.js +145 -0
- package/dist/rules/prefer-react-with-hooks.js.map +1 -0
- package/dist/rules/restrict-console-methods.d.ts +9 -0
- package/dist/rules/restrict-console-methods.js +68 -0
- package/dist/rules/restrict-console-methods.js.map +1 -0
- package/dist/utility/getFunctionName.d.ts +2 -0
- package/dist/utility/getFunctionName.js +33 -0
- package/dist/utility/getFunctionName.js.map +1 -0
- package/dist/utility/getType.d.ts +3 -0
- package/dist/utility/getType.js +21 -0
- package/dist/utility/getType.js.map +1 -0
- package/package.json +48 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, ESLintUtils, } from "@typescript-eslint/utils";
|
|
2
|
+
import { REACT_HOOKS } from "../constants/hooks.js";
|
|
3
|
+
export const name = "prefer-react-with-hooks";
|
|
4
|
+
export const rule = ESLintUtils.RuleCreator.withoutDocs({
|
|
5
|
+
create: (context) => {
|
|
6
|
+
let hasReactImport = false;
|
|
7
|
+
let reactImportNode = null;
|
|
8
|
+
return {
|
|
9
|
+
CallExpression: (node) => {
|
|
10
|
+
if (node.callee.type === AST_NODE_TYPES.Identifier &&
|
|
11
|
+
REACT_HOOKS.has(node.callee.name)) {
|
|
12
|
+
const hookName = node.callee.name;
|
|
13
|
+
context.report({
|
|
14
|
+
data: { hook: hookName },
|
|
15
|
+
fix: (fixer) => {
|
|
16
|
+
const fixes = [fixer.insertTextBefore(node.callee, "React.")];
|
|
17
|
+
if (!hasReactImport && !reactImportNode) {
|
|
18
|
+
const sourceCode = context.getSourceCode();
|
|
19
|
+
const program = sourceCode.ast;
|
|
20
|
+
if (program.body.length > 0) {
|
|
21
|
+
fixes.push(fixer.insertTextBefore(program.body[0], 'import React from "react";\n'));
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
fixes.push(fixer.insertTextAfter(program, 'import React from "react";\n'));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else if (reactImportNode && !hasReactImport) {
|
|
28
|
+
const sourceCode = context.getSourceCode();
|
|
29
|
+
const importText = sourceCode.getText(reactImportNode);
|
|
30
|
+
if (importText.includes("{") &&
|
|
31
|
+
!importText.match(/^import\s+\w+\s*,/)) {
|
|
32
|
+
fixes.push(fixer.replaceText(reactImportNode, importText.replace(/^import\s*{/, "import React, {")));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return fixes;
|
|
36
|
+
},
|
|
37
|
+
messageId: "preferReactPrefix",
|
|
38
|
+
node: node.callee,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
ImportDeclaration: (node) => {
|
|
43
|
+
if (node.source.value === "react") {
|
|
44
|
+
reactImportNode = node;
|
|
45
|
+
hasReactImport = node.specifiers.some((spec) => spec.type === AST_NODE_TYPES.ImportDefaultSpecifier ||
|
|
46
|
+
(spec.type === AST_NODE_TYPES.ImportNamespaceSpecifier &&
|
|
47
|
+
spec.local.name === "React"));
|
|
48
|
+
const useImports = node.specifiers.filter((s) => s.type === AST_NODE_TYPES.ImportSpecifier &&
|
|
49
|
+
s.imported.type === AST_NODE_TYPES.Identifier &&
|
|
50
|
+
REACT_HOOKS.has(s.imported.name));
|
|
51
|
+
if (useImports.length > 0) {
|
|
52
|
+
const firstHookImport = useImports[0];
|
|
53
|
+
const hookNames = useImports
|
|
54
|
+
.map((spec) => spec.imported.type === AST_NODE_TYPES.Identifier
|
|
55
|
+
? spec.imported.name
|
|
56
|
+
: "")
|
|
57
|
+
.filter(Boolean);
|
|
58
|
+
context.report({
|
|
59
|
+
data: {
|
|
60
|
+
hook: hookNames.length === 1
|
|
61
|
+
? hookNames[0]
|
|
62
|
+
: `${hookNames
|
|
63
|
+
.slice(0, -1)
|
|
64
|
+
.join(", ")} and ${hookNames.slice(-1)}`,
|
|
65
|
+
},
|
|
66
|
+
fix: (fixer) => {
|
|
67
|
+
const sourceCode = context.getSourceCode();
|
|
68
|
+
const fixes = [];
|
|
69
|
+
const remainingSpecifiers = node.specifiers.filter((spec) => !(spec.type === AST_NODE_TYPES.ImportSpecifier &&
|
|
70
|
+
spec.imported.type === AST_NODE_TYPES.Identifier &&
|
|
71
|
+
REACT_HOOKS.has(spec.imported.name)));
|
|
72
|
+
if (remainingSpecifiers.length === 0 && !hasReactImport) {
|
|
73
|
+
fixes.push(fixer.replaceText(node, 'import React from "react";'));
|
|
74
|
+
}
|
|
75
|
+
else if (remainingSpecifiers.length > 0 && !hasReactImport) {
|
|
76
|
+
const nonDefaultSpecifiers = remainingSpecifiers.filter((spec) => spec.type !== AST_NODE_TYPES.ImportDefaultSpecifier);
|
|
77
|
+
if (nonDefaultSpecifiers.length > 0) {
|
|
78
|
+
const specifierTexts = nonDefaultSpecifiers
|
|
79
|
+
.map((spec) => sourceCode.getText(spec))
|
|
80
|
+
.join(", ");
|
|
81
|
+
fixes.push(fixer.replaceText(node, `import React, { ${specifierTexts} } from "react";`));
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
fixes.push(fixer.replaceText(node, 'import React from "react";'));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else if (hasReactImport) {
|
|
88
|
+
const hasDefaultReact = remainingSpecifiers.some((spec) => spec.type === AST_NODE_TYPES.ImportDefaultSpecifier);
|
|
89
|
+
const namedSpecifiers = remainingSpecifiers.filter((spec) => spec.type === AST_NODE_TYPES.ImportSpecifier);
|
|
90
|
+
if (hasDefaultReact && namedSpecifiers.length > 0) {
|
|
91
|
+
const specifierTexts = namedSpecifiers
|
|
92
|
+
.map((spec) => sourceCode.getText(spec))
|
|
93
|
+
.join(", ");
|
|
94
|
+
fixes.push(fixer.replaceText(node, `import React, { ${specifierTexts} } from "react";`));
|
|
95
|
+
}
|
|
96
|
+
else if (hasDefaultReact && namedSpecifiers.length === 0) {
|
|
97
|
+
fixes.push(fixer.replaceText(node, 'import React from "react";'));
|
|
98
|
+
}
|
|
99
|
+
else if (!hasDefaultReact && namedSpecifiers.length > 0) {
|
|
100
|
+
const specifierTexts = namedSpecifiers
|
|
101
|
+
.map((spec) => sourceCode.getText(spec))
|
|
102
|
+
.join(", ");
|
|
103
|
+
fixes.push(fixer.replaceText(node, `import { ${specifierTexts} } from "react";`));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return fixes;
|
|
107
|
+
},
|
|
108
|
+
messageId: "preferReact",
|
|
109
|
+
node: firstHookImport,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
defaultOptions: [
|
|
117
|
+
{
|
|
118
|
+
autoFix: false,
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
meta: {
|
|
122
|
+
docs: {
|
|
123
|
+
description: "Enforce React.use* over named use* imports",
|
|
124
|
+
},
|
|
125
|
+
fixable: "code",
|
|
126
|
+
messages: {
|
|
127
|
+
preferReact: "NIMA: Use React.{{hook}} instead of importing {{hook}} directly.",
|
|
128
|
+
preferReactPrefix: "NIMA: Prefix {{hook}} with React.",
|
|
129
|
+
},
|
|
130
|
+
schema: [
|
|
131
|
+
{
|
|
132
|
+
additionalProperties: false,
|
|
133
|
+
properties: {
|
|
134
|
+
autoFix: {
|
|
135
|
+
default: true,
|
|
136
|
+
type: "boolean",
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
type: "object",
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
type: "problem",
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
//# sourceMappingURL=prefer-react-with-hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefer-react-with-hooks.js","sourceRoot":"","sources":["../../src/rules/prefer-react-with-hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,WAAW,GAEZ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpD,MAAM,CAAC,MAAM,IAAI,GAAG,yBAAyB,CAAC;AAE9C,MAAM,CAAC,MAAM,IAAI,GAAG,WAAW,CAAC,WAAW,CAAC,WAAW,CAAC;IACtD,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE;QAClB,IAAI,cAAc,GAAG,KAAK,CAAC;QAC3B,IAAI,eAAe,G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?: boolean;
|
|
4
|
+
noConsoleLog?: boolean;
|
|
5
|
+
noConsoleWarn?: boolean;
|
|
6
|
+
};
|
|
7
|
+
export declare const name = "restrict-console-methods";
|
|
8
|
+
export declare const rule: ESLintUtils.RuleModule<"noConsole", Options[], unknown, ESLintUtils.RuleListener>;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
2
|
+
export const name = "restrict-console-methods";
|
|
3
|
+
export const rule = ESLintUtils.RuleCreator.withoutDocs({
|
|
4
|
+
create(context, options) {
|
|
5
|
+
const noConsoleError = options[0].noConsoleError;
|
|
6
|
+
const noConsoleLog = options[0].noConsoleLog;
|
|
7
|
+
const noConsoleWarn = options[0].noConsoleWarn;
|
|
8
|
+
return {
|
|
9
|
+
CallExpression(node) {
|
|
10
|
+
if (node.callee.type === "MemberExpression" &&
|
|
11
|
+
node.callee.object.type === "Identifier" &&
|
|
12
|
+
node.callee.object.name === "console" &&
|
|
13
|
+
node.callee.property.type === "Identifier") {
|
|
14
|
+
const methodName = node.callee.property.name;
|
|
15
|
+
if ((methodName === "error" && !noConsoleError) ||
|
|
16
|
+
(methodName === "log" && !noConsoleLog) ||
|
|
17
|
+
(methodName === "warn" && !noConsoleWarn)) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
context.report({
|
|
21
|
+
data: {
|
|
22
|
+
console: node.callee.property.name,
|
|
23
|
+
},
|
|
24
|
+
messageId: "noConsole",
|
|
25
|
+
node,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
defaultOptions: [
|
|
32
|
+
{
|
|
33
|
+
noConsoleError: true,
|
|
34
|
+
noConsoleLog: true,
|
|
35
|
+
noConsoleWarn: true,
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
meta: {
|
|
39
|
+
docs: {
|
|
40
|
+
description: "Restrict the usage of console in the codebase",
|
|
41
|
+
},
|
|
42
|
+
messages: {
|
|
43
|
+
noConsole: "NIMA: The usage of console.{{ console }} is restricted.",
|
|
44
|
+
},
|
|
45
|
+
schema: [
|
|
46
|
+
{
|
|
47
|
+
additionalProperties: false,
|
|
48
|
+
properties: {
|
|
49
|
+
noConsoleError: {
|
|
50
|
+
default: true,
|
|
51
|
+
type: "boolean",
|
|
52
|
+
},
|
|
53
|
+
noConsoleLog: {
|
|
54
|
+
default: true,
|
|
55
|
+
type: "boolean",
|
|
56
|
+
},
|
|
57
|
+
noConsoleWarn: {
|
|
58
|
+
default: true,
|
|
59
|
+
type: "boolean",
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
type: "object",
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
type: "suggestion",
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
//# sourceMappingURL=restrict-console-methods.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"restrict-console-methods.js","sourceRoot":"","sources":["../../src/rules/restrict-console-methods.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAUvD,MAAM,CAAC,MAAM,IAAI,GAAG,0BAA0B,CAAC;AAE/C,MAAM,CAAC,MAAM,IAAI,GAAG,WAAW,CAAC,WAAW,CAAC,WAAW,CAAsB;IAC3E,MAAM,CAAC,OAAO,EAAE,OAAO;QACrB,MAAM,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC;QACjD,MAAM,YAAY,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC;QAC7C,MAAM,aAAa,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC;QAE/C,OAAO;YACL,cAAc,CAAC,IAAI;gBACjB,IACE,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,kBAAkB;oBACvC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI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;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, } from "@typescript-eslint/typescript-estree";
|
|
2
|
+
export function getFunctionName(node) {
|
|
3
|
+
var _a, _b, _c, _d, _e;
|
|
4
|
+
if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === AST_NODE_TYPES.CallExpression) {
|
|
5
|
+
const callExpr = node.parent;
|
|
6
|
+
if (callExpr.callee.type === AST_NODE_TYPES.MemberExpression &&
|
|
7
|
+
callExpr.callee.property.type === AST_NODE_TYPES.Identifier) {
|
|
8
|
+
return callExpr.callee.property.name;
|
|
9
|
+
}
|
|
10
|
+
if (callExpr.callee.type === AST_NODE_TYPES.Identifier) {
|
|
11
|
+
return callExpr.callee.name;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
if ("id" in node && ((_b = node.id) === null || _b === void 0 ? void 0 : _b.type) === AST_NODE_TYPES.Identifier) {
|
|
15
|
+
return node.id.name;
|
|
16
|
+
}
|
|
17
|
+
if (((_c = node.parent) === null || _c === void 0 ? void 0 : _c.type) === AST_NODE_TYPES.Property &&
|
|
18
|
+
node.parent.key.type === AST_NODE_TYPES.Identifier) {
|
|
19
|
+
return node.parent.key.name;
|
|
20
|
+
}
|
|
21
|
+
if (node.type === AST_NODE_TYPES.ArrowFunctionExpression &&
|
|
22
|
+
((_d = node.parent) === null || _d === void 0 ? void 0 : _d.type) === AST_NODE_TYPES.VariableDeclarator &&
|
|
23
|
+
node.parent.id.type === AST_NODE_TYPES.Identifier) {
|
|
24
|
+
return node.parent.id.name;
|
|
25
|
+
}
|
|
26
|
+
if (node.type === AST_NODE_TYPES.FunctionExpression &&
|
|
27
|
+
((_e = node.parent) === null || _e === void 0 ? void 0 : _e.type) === AST_NODE_TYPES.VariableDeclarator &&
|
|
28
|
+
node.parent.id.type === AST_NODE_TYPES.Identifier) {
|
|
29
|
+
return node.parent.id.name;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=getFunctionName.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getFunctionName.js","sourceRoot":"","sources":["../../src/utility/getFunctionName.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAEf,MAAM,sCAAsC,CAAC;AAE9C,MAAM,UAAU,eAAe,CAC7B,IAG+B;;IAE/B,IAAI,CAAA,MAAA,IAAI,CAAC,MAAM,0CAAE,IAAI,MAAK,cAAc,CAAC,cAAc,EAAE,CAAC;QACxD,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC;QAE7B,IACE,QAAQ,CAAC,MAAM,CAAC,IAAI,KAAK,cAAc,CAAC,gBAAgB;YACxD,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,KAAK,cAAc,CAAC,UAAU,EAC3D,CAAC;YACD,OAAO,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;QACvC,CAAC;QAED,IAAI,QAAQ,CAAC,MAAM,CAAC,IAAI,KAAK,cAAc,CAAC,UAAU,EAAE,CAAC;YACvD,OAAO,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC;QAC9B,CAAC;IACH,CAAC;IAED,IAAI,IAAI,IAAI,IAAI,IAAI,CAAA,MAAA,IAAI,CAAC,EAAE,0CAAE,IAAI,MAAK,cAAc,CAAC,UAAU,EAAE,CAAC;QAChE,OAAO,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC;IACtB,CAAC;IAED,IACE,CAAA,MAAA,IAAI,CAAC,MAAM,0CAAE,IAAI,MAAK,cAAc,CAAC,QAAQ;QAC7C,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,KAAK,cAAc,CAAC,UAAU,EAClD,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC;IAC9B,CAAC;IAED,IACE,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC,uBAAuB;QACpD,CAAA,MAAA,IAAI,CAAC,MAAM,0CAAE,IAAI,MAAK,cAAc,CAAC,kBAAkB;QACvD,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,KAAK,cAAc,CAAC,UAAU,EACjD,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC;IAC7B,CAAC;IAED,IACE,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC,kBAAkB;QAC/C,CAAA,MAAA,IAAI,CAAC,MAAM,0CAAE,IAAI,MAAK,cAAc,CAAC,kBAAkB;QACvD,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,KAAK,cAAc,CAAC,UAAU,EACjD,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC;IAC7B,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function getType(context, node) {
|
|
2
|
+
var _a, _b;
|
|
3
|
+
const sourceCode = context.getSourceCode();
|
|
4
|
+
const services = sourceCode.parserServices;
|
|
5
|
+
if (!((_a = services === null || services === void 0 ? void 0 : services.program) === null || _a === void 0 ? void 0 : _a.getTypeChecker)) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
const checker = services.program.getTypeChecker();
|
|
9
|
+
try {
|
|
10
|
+
const tsNode = (_b = services.esTreeNodeToTSNodeMap) === null || _b === void 0 ? void 0 : _b.get(node);
|
|
11
|
+
if (!tsNode)
|
|
12
|
+
return;
|
|
13
|
+
const type = checker.getTypeAtLocation(tsNode);
|
|
14
|
+
const typeString = checker.typeToString(type);
|
|
15
|
+
return typeString;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=getType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getType.js","sourceRoot":"","sources":["../../src/utility/getType.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,OAAO,CACrB,OAA0D,EAC1D,IAAmB;;IAEnB,MAAM,UAAU,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;IAC3C,MAAM,QAAQ,GAAG,UAAU,CAAC,cAAc,CAAC;IAE3C,IAAI,CAAC,CAAA,MAAA,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,OAAO,0CAAE,cAAc,CAAA,EAAE,CAAC;QACvC,OAAO;IACT,CAAC;IAED,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;IAElD,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAA,QAAQ,CAAC,qBAAqB,0CAAE,GAAG,CAAC,IAAI,CAAC,CAAC;QACzD,IAAI,CAAC,MAAM;YAAE,OAAO;QAEpB,MAAM,IAAI,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;QAC/C,MAAM,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;QAE9C,OAAO,UAAU,CAAC;IACpB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO;IACT,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eslint-plugin-nima",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"description": "ESLint plugin for NIMA Labs",
|
|
6
|
+
"author": "NIMA",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "rm -rf dist && tsc --project tsconfig.build.json",
|
|
11
|
+
"dev": "tsc --watch",
|
|
12
|
+
"lint": "eslint .",
|
|
13
|
+
"lint:fix": "eslint . --fix",
|
|
14
|
+
"check-types": "tsc --noEmit",
|
|
15
|
+
"validate": "pnpm lint:fix && pnpm check-types",
|
|
16
|
+
"prepare": "husky"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@eslint/core": "^0.15.2",
|
|
27
|
+
"@eslint/js": "^9.32.0",
|
|
28
|
+
"@types/node": "^24.3.0",
|
|
29
|
+
"@typescript-eslint/rule-tester": "^8.40.0",
|
|
30
|
+
"eslint-plugin-perfectionist": "^4.15.0",
|
|
31
|
+
"globals": "^16.3.0",
|
|
32
|
+
"husky": "^9.1.7",
|
|
33
|
+
"jiti": "^2.5.1",
|
|
34
|
+
"typescript": "^5.9.2",
|
|
35
|
+
"typescript-eslint": "^8.39.0"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@typescript-eslint/utils": "^8.40.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"eslint": "^9.0.0"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=16.0.0"
|
|
45
|
+
},
|
|
46
|
+
"packageManager": "pnpm@8.9.0",
|
|
47
|
+
"license": "MIT"
|
|
48
|
+
}
|