eslint-plugin-absolute 0.2.0 → 0.2.1

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.
@@ -7,33 +7,92 @@ type MessageIds =
7
7
  | "secondMustMatch"
8
8
  | "pluralRequired";
9
9
 
10
+ const SPRINGS_SUFFIX = "Springs";
11
+
12
+ const checkUseSpring = (
13
+ context: TSESLint.RuleContext<MessageIds, Options>,
14
+ firstElem: TSESTree.Identifier,
15
+ secondElem: TSESTree.Identifier
16
+ ) => {
17
+ const firstName = firstElem.name;
18
+ const secondName = secondElem.name;
19
+
20
+ if (!firstName.endsWith(SPRINGS_SUFFIX)) {
21
+ context.report({
22
+ messageId: "firstMustEndWithSprings",
23
+ node: firstElem
24
+ });
25
+ return;
26
+ }
27
+
28
+ const base = firstName.slice(0, -SPRINGS_SUFFIX.length);
29
+ if (!base) {
30
+ context.report({
31
+ messageId: "firstMustHaveBase",
32
+ node: firstElem
33
+ });
34
+ return;
35
+ }
36
+
37
+ const expectedSecond = `${base}Api`;
38
+ if (secondName !== expectedSecond) {
39
+ context.report({
40
+ data: { expected: expectedSecond },
41
+ messageId: "secondMustMatch",
42
+ node: secondElem
43
+ });
44
+ }
45
+ };
46
+
47
+ const checkUseSprings = (
48
+ context: TSESLint.RuleContext<MessageIds, Options>,
49
+ firstElem: TSESTree.Identifier,
50
+ secondElem: TSESTree.Identifier
51
+ ) => {
52
+ const firstName = firstElem.name;
53
+ const secondName = secondElem.name;
54
+
55
+ if (!firstName.endsWith(SPRINGS_SUFFIX)) {
56
+ context.report({
57
+ messageId: "firstMustEndWithSprings",
58
+ node: firstElem
59
+ });
60
+ return;
61
+ }
62
+
63
+ const basePlural = firstName.slice(0, -SPRINGS_SUFFIX.length);
64
+ if (!basePlural) {
65
+ context.report({
66
+ messageId: "firstMustHaveBase",
67
+ node: firstElem
68
+ });
69
+ return;
70
+ }
71
+
72
+ if (!basePlural.endsWith("s")) {
73
+ context.report({
74
+ messageId: "pluralRequired",
75
+ node: firstElem
76
+ });
77
+ return;
78
+ }
79
+
80
+ const expectedSecond = `${basePlural}Api`;
81
+ if (secondName !== expectedSecond) {
82
+ context.report({
83
+ data: { expected: expectedSecond },
84
+ messageId: "secondMustMatch",
85
+ node: secondElem
86
+ });
87
+ }
88
+ };
89
+
10
90
  export const springNamingConvention: TSESLint.RuleModule<MessageIds, Options> =
11
91
  {
12
- meta: {
13
- type: "problem",
14
- docs: {
15
- description:
16
- "Enforce correct naming for useSpring and useSprings hook destructuring"
17
- },
18
- schema: [],
19
- messages: {
20
- firstMustEndWithSprings:
21
- "The first variable must end with 'Springs'.",
22
- firstMustHaveBase:
23
- "The first variable must have a non-empty name before 'Springs'.",
24
- secondMustMatch:
25
- "The second variable must be named '{{expected}}'.",
26
- pluralRequired:
27
- "The first variable for useSprings should be plural (ending with 's') before 'Springs'."
28
- }
29
- },
30
-
31
- defaultOptions: [],
32
-
33
92
  create(context) {
34
93
  return {
35
94
  VariableDeclarator(node: TSESTree.VariableDeclarator) {
36
- const init = node.init;
95
+ const { init } = node;
37
96
 
38
97
  if (
39
98
  !init ||
@@ -52,13 +111,12 @@ export const springNamingConvention: TSESLint.RuleModule<MessageIds, Options> =
52
111
  return;
53
112
  }
54
113
 
55
- const elements = node.id.elements;
114
+ const { elements } = node.id;
56
115
  if (elements.length < 2) {
57
116
  return;
58
117
  }
59
118
 
60
- const firstElem = elements[0];
61
- const secondElem = elements[1];
119
+ const [firstElem, secondElem] = elements;
62
120
 
63
121
  if (
64
122
  !firstElem ||
@@ -69,77 +127,34 @@ export const springNamingConvention: TSESLint.RuleModule<MessageIds, Options> =
69
127
  return;
70
128
  }
71
129
 
72
- const firstName = firstElem.name;
73
- const secondName = secondElem.name;
74
-
75
130
  if (hookName === "useSpring") {
76
- if (!firstName.endsWith("Springs")) {
77
- context.report({
78
- node: firstElem,
79
- messageId: "firstMustEndWithSprings"
80
- });
81
- return;
82
- }
83
-
84
- const base = firstName.slice(0, -"Springs".length);
85
- if (!base) {
86
- context.report({
87
- node: firstElem,
88
- messageId: "firstMustHaveBase"
89
- });
90
- return;
91
- }
92
-
93
- const expectedSecond = `${base}Api`;
94
- if (secondName !== expectedSecond) {
95
- context.report({
96
- node: secondElem,
97
- messageId: "secondMustMatch",
98
- data: { expected: expectedSecond }
99
- });
100
- }
131
+ checkUseSpring(context, firstElem, secondElem);
101
132
  return;
102
133
  }
103
134
 
104
135
  if (hookName === "useSprings") {
105
- if (!firstName.endsWith("Springs")) {
106
- context.report({
107
- node: firstElem,
108
- messageId: "firstMustEndWithSprings"
109
- });
110
- return;
111
- }
112
-
113
- const basePlural = firstName.slice(
114
- 0,
115
- -"Springs".length
116
- );
117
- if (!basePlural) {
118
- context.report({
119
- node: firstElem,
120
- messageId: "firstMustHaveBase"
121
- });
122
- return;
123
- }
124
-
125
- if (!basePlural.endsWith("s")) {
126
- context.report({
127
- node: firstElem,
128
- messageId: "pluralRequired"
129
- });
130
- return;
131
- }
132
-
133
- const expectedSecond = `${basePlural}Api`;
134
- if (secondName !== expectedSecond) {
135
- context.report({
136
- node: secondElem,
137
- messageId: "secondMustMatch",
138
- data: { expected: expectedSecond }
139
- });
140
- }
136
+ checkUseSprings(context, firstElem, secondElem);
141
137
  }
142
138
  }
143
139
  };
140
+ },
141
+ defaultOptions: [],
142
+ meta: {
143
+ docs: {
144
+ description:
145
+ "Enforce correct naming for useSpring and useSprings hook destructuring"
146
+ },
147
+ messages: {
148
+ firstMustEndWithSprings:
149
+ "The first variable must end with 'Springs'.",
150
+ firstMustHaveBase:
151
+ "The first variable must have a non-empty name before 'Springs'.",
152
+ pluralRequired:
153
+ "The first variable for useSprings should be plural (ending with 's') before 'Springs'.",
154
+ secondMustMatch:
155
+ "The second variable must be named '{{expected}}'."
156
+ },
157
+ schema: [],
158
+ type: "problem"
144
159
  }
145
160
  };
package/tsconfig.json CHANGED
@@ -10,6 +10,8 @@
10
10
  "strict": true,
11
11
  "target": "ESNext",
12
12
  "noUncheckedIndexedAccess": true,
13
+ "incremental": true,
14
+ "tsBuildInfoFile": ".absolutejs/tsconfig.tsbuildinfo",
13
15
  "types": ["bun-types"]
14
16
  }
15
17
  }