eslint-plugin-absolute 0.2.7 → 0.3.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.
@@ -1,111 +0,0 @@
1
- /**
2
- * @fileoverview Enforce that the key prop is only used on components
3
- * rendered as part of an array mapping. This rule disallows having a key prop
4
- * on a JSX element when it is not part of a mapping, except when the element is
5
- * returned from a helper render function.
6
- *
7
- * Note: This rule does not auto-fix.
8
- */
9
-
10
- import { TSESLint, TSESTree } from "@typescript-eslint/utils";
11
-
12
- type Options = [];
13
- type MessageIds = "unnecessaryKey";
14
-
15
- const isMapCallExpression = (node: TSESTree.Node) => {
16
- if (
17
- node.type !== "CallExpression" ||
18
- node.callee.type !== "MemberExpression"
19
- ) {
20
- return false;
21
- }
22
-
23
- const { property } = node.callee;
24
- return (
25
- (property.type === "Identifier" && property.name === "map") ||
26
- (property.type === "Literal" && property.value === "map")
27
- );
28
- };
29
-
30
- export const noUnnecessaryKey: TSESLint.RuleModule<MessageIds, Options> = {
31
- create(context) {
32
- // Polyfill for context.getAncestors if it's not available.
33
- const getAncestors = (node: TSESTree.Node) => {
34
- const ancestors: TSESTree.Node[] = [];
35
- let current: TSESTree.Node | null | undefined = node.parent;
36
- while (current) {
37
- ancestors.push(current);
38
- current = current.parent;
39
- }
40
- return ancestors;
41
- };
42
-
43
- /**
44
- * Checks if any of the ancestors is a CallExpression
45
- * representing an array mapping.
46
- */
47
- const isInsideMapCall = (ancestors: TSESTree.Node[]) =>
48
- ancestors.some(isMapCallExpression);
49
-
50
- /**
51
- * Checks whether the JSX element is being returned from a helper render
52
- * function.
53
- */
54
- const isReturnedFromFunction = (ancestors: TSESTree.Node[]) =>
55
- ancestors.some((ancestor) => ancestor.type === "ReturnStatement");
56
-
57
- /**
58
- * Reports a JSX element if it has a key prop and is not rendered as part
59
- * of an inline mapping (and not simply returned from a render helper function).
60
- */
61
- const checkJSXOpeningElement = (node: TSESTree.JSXOpeningElement) => {
62
- // Find a key attribute.
63
- const keyAttribute = node.attributes.find(
64
- (attr) =>
65
- attr.type === "JSXAttribute" &&
66
- attr.name.type === "JSXIdentifier" &&
67
- attr.name.name === "key"
68
- );
69
-
70
- if (!keyAttribute) {
71
- return;
72
- }
73
-
74
- // Retrieve ancestors.
75
- const ancestors = getAncestors(node);
76
-
77
- // If the element is (directly or indirectly) part of a map call, allow it.
78
- if (isInsideMapCall(ancestors)) {
79
- return;
80
- }
81
-
82
- // If the element is simply returned from a helper function, allow it.
83
- if (isReturnedFromFunction(ancestors)) {
84
- return;
85
- }
86
-
87
- // Otherwise, report the key prop as unnecessary.
88
- context.report({
89
- messageId: "unnecessaryKey",
90
- node: keyAttribute
91
- });
92
- };
93
-
94
- return {
95
- JSXOpeningElement: checkJSXOpeningElement
96
- };
97
- },
98
- defaultOptions: [],
99
- meta: {
100
- docs: {
101
- description:
102
- "enforce that the key prop is only used on components rendered as part of a mapping"
103
- },
104
- messages: {
105
- unnecessaryKey:
106
- "The key prop should only be used on elements that are directly rendered as part of an array mapping."
107
- },
108
- schema: [],
109
- type: "problem"
110
- }
111
- };
@@ -1,56 +0,0 @@
1
- import { TSESLint, TSESTree } from "@typescript-eslint/utils";
2
-
3
- type Options = [];
4
- type MessageIds = "uselessFunction";
5
-
6
- export const noUselessFunction: TSESLint.RuleModule<MessageIds, Options> = {
7
- create(context) {
8
- const isCallbackFunction = (node: TSESTree.ArrowFunctionExpression) => {
9
- const { parent } = node;
10
- if (!parent || parent.type !== "CallExpression") {
11
- return false;
12
- }
13
-
14
- for (const arg of parent.arguments) {
15
- if (arg === node) {
16
- return true;
17
- }
18
- }
19
-
20
- return false;
21
- };
22
-
23
- return {
24
- ArrowFunctionExpression(node: TSESTree.ArrowFunctionExpression) {
25
- // Check for functions with no parameters and a body that's an ObjectExpression
26
- if (
27
- node.params.length === 0 &&
28
- node.body &&
29
- node.body.type === "ObjectExpression"
30
- ) {
31
- // If the function is used as a callback (like in react-spring), skip reporting.
32
- if (isCallbackFunction(node)) {
33
- return;
34
- }
35
- context.report({
36
- messageId: "uselessFunction",
37
- node
38
- });
39
- }
40
- }
41
- };
42
- },
43
- defaultOptions: [],
44
- meta: {
45
- docs: {
46
- description:
47
- "Disallow functions that have no parameters and just return an object literal; consider exporting the object directly, unless the function is used as a callback (e.g., in react-spring)."
48
- },
49
- messages: {
50
- uselessFunction:
51
- "This function has no parameters and simply returns an object. Consider exporting the object directly instead of wrapping it in a function."
52
- },
53
- schema: [],
54
- type: "suggestion"
55
- }
56
- };
@@ -1,79 +0,0 @@
1
- import { TSESLint, TSESTree } from "@typescript-eslint/utils";
2
-
3
- type Options = [];
4
- type MessageIds = "moveToFile";
5
-
6
- export const seperateStyleFiles: TSESLint.RuleModule<MessageIds, Options> = {
7
- create(context) {
8
- // Only run this rule on .tsx or .jsx files.
9
- const { filename } = context;
10
- if (!filename.endsWith(".tsx") && !filename.endsWith(".jsx")) {
11
- return {};
12
- }
13
-
14
- return {
15
- VariableDeclarator(node: TSESTree.VariableDeclarator) {
16
- // Ensure this is a variable declaration with an Identifier.
17
- if (!node.id || node.id.type !== "Identifier") {
18
- return;
19
- }
20
-
21
- const identifier = node.id;
22
-
23
- // Check if there's a type annotation on the variable.
24
- const idTypeAnnotation = identifier.typeAnnotation;
25
- if (
26
- !idTypeAnnotation ||
27
- idTypeAnnotation.type !== "TSTypeAnnotation"
28
- ) {
29
- return;
30
- }
31
-
32
- const typeNode = idTypeAnnotation.typeAnnotation;
33
- if (!typeNode || typeNode.type !== "TSTypeReference") {
34
- return;
35
- }
36
-
37
- // Handle both Identifier and TSQualifiedName cases.
38
- const typeNameNode = typeNode.typeName;
39
- let typeName: string | null = null;
40
-
41
- // When typeName is a simple Identifier.
42
- if (typeNameNode.type === "Identifier") {
43
- typeName = typeNameNode.name;
44
- }
45
- // When typeName is a TSQualifiedName, e.g., React.CSSProperties.
46
- else if (typeNameNode.type === "TSQualifiedName") {
47
- const { right } = typeNameNode;
48
- typeName = right.name;
49
- }
50
-
51
- // Report if the type name is CSSProperties.
52
- if (typeName === "CSSProperties") {
53
- context.report({
54
- data: {
55
- name: identifier.name,
56
- typeName
57
- },
58
- messageId: "moveToFile",
59
- node
60
- });
61
- }
62
- }
63
- };
64
- },
65
- defaultOptions: [],
66
- meta: {
67
- docs: {
68
- description:
69
- "Warn when a component file (.jsx or .tsx) contains a style object typed as CSSProperties. " +
70
- "Style objects should be moved to their own file under the style folder."
71
- },
72
- messages: {
73
- moveToFile:
74
- 'Style object "{{name}}" is typed as {{typeName}}. Move it to its own file under the style folder.'
75
- },
76
- schema: [],
77
- type: "suggestion"
78
- }
79
- };