eslint-config-matsuri 1.4.1-alpha-c387b34.0 → 1.5.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/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const rulesDirPlugin = require("eslint-plugin-rulesdir");
2
- rulesDirPlugin.RULES_DIR = "./rules";
2
+ rulesDirPlugin.RULES_DIR = __dirname + "/rules";
3
3
 
4
4
  /** @type {import('eslint').Linter.BaseConfig} */
5
5
  const config = {
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "eslint-config-matsuri",
3
- "version": "1.4.1-alpha-c387b34.0",
3
+ "version": "1.5.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "files": [
7
- "index.js"
7
+ "index.js",
8
+ "rules"
8
9
  ],
9
10
  "scripts": {
10
11
  "test": "eslint . --max-warnings 0"
@@ -0,0 +1,160 @@
1
+ const REGEX_CAMEL_CASE = /^[a-z][a-zA-Z0-9]*$/;
2
+ const REGEX_PASCAL_CASE = /^[A-Z][a-zA-Z0-9]*$/;
3
+ const REGEX_UPPER_CASE = /^[A-Z][A-Z0-9_]*$/;
4
+ const REGEX_SNAKE_CASE = /^[a-z][a-z0-9_]*$/;
5
+
6
+ const isCamelCase = (name) => REGEX_CAMEL_CASE.test(name);
7
+ const isPascalCase = (name) => REGEX_PASCAL_CASE.test(name);
8
+ const isUpperCase = (name) => REGEX_UPPER_CASE.test(name);
9
+ const isSnakeCase = (name) => REGEX_SNAKE_CASE.test(name);
10
+
11
+ const toCamelCase = (name) => {
12
+ if (isCamelCase(name)) {
13
+ return name;
14
+ }
15
+ if (isPascalCase(name)) {
16
+ return name[0].toLowerCase() + name.slice(1);
17
+ }
18
+ if (isUpperCase(name) || isSnakeCase(name)) {
19
+ const tmp = name
20
+ .split("_")
21
+ .map((word) => word[0].toUpperCase() + word.slice(1))
22
+ .join("");
23
+ return tmp[0].toLowerCase() + tmp.slice(1);
24
+ }
25
+ return name;
26
+ };
27
+
28
+ const checkValidity = (name, format) => {
29
+ switch (format) {
30
+ case "camelCase": {
31
+ return isCamelCase(name);
32
+ }
33
+ case "PascalCase": {
34
+ return isPascalCase(name);
35
+ }
36
+ case "UPPER_CASE": {
37
+ return isUpperCase(name);
38
+ }
39
+ }
40
+ };
41
+
42
+ const trimUnderscore = (name, leadingUnderscore, trailingUnderscore) => {
43
+ if (leadingUnderscore === "allow" && name?.startsWith("_")) {
44
+ return name.slice(1);
45
+ }
46
+ if (trailingUnderscore === "allow" && name?.endsWith("_")) {
47
+ return name.slice(0, -1);
48
+ }
49
+ return name;
50
+ };
51
+
52
+ const reportValidity = (
53
+ context,
54
+ node,
55
+ format,
56
+ fix,
57
+ leadingUnderscore,
58
+ trailingUnderscore
59
+ ) => {
60
+ if (node.id === null) {
61
+ return;
62
+ }
63
+ const valid = format.some((format) => {
64
+ const name = trimUnderscore(
65
+ node.id.name,
66
+ leadingUnderscore,
67
+ trailingUnderscore
68
+ );
69
+ return checkValidity(name, format);
70
+ });
71
+
72
+ if (valid === false) {
73
+ context.report({
74
+ node,
75
+ message: `${node.id.name} must be ${format.join(" or ")}`,
76
+ fix: (fixer) => {
77
+ switch (fix) {
78
+ case "camelCase": {
79
+ return fixer.replaceText(node.id, toCamelCase(node.id.name));
80
+ }
81
+ default: {
82
+ return fixer.replaceText(node.id, node.id.name);
83
+ }
84
+ }
85
+ },
86
+ });
87
+ }
88
+ };
89
+
90
+ const options = {
91
+ validable: {
92
+ format: ["camelCase", "UPPER_CASE"],
93
+ fix: "camelCase",
94
+ leadingUnderscore: "allow",
95
+ trailingUnderscore: "allow",
96
+ },
97
+ function: {
98
+ format: ["camelCase", "PascalCase"],
99
+ fix: "camelCase",
100
+ leadingUnderscore: "allow",
101
+ trailingUnderscore: "allow",
102
+ },
103
+ };
104
+
105
+ /**
106
+ * @type {import("eslint").Rule.RuleModule}
107
+ */
108
+ const rule = {
109
+ meta: {
110
+ type: "problem",
111
+ fixable: "code",
112
+ },
113
+ create: (context) => {
114
+ return {
115
+ VariableDeclarator: (node) => {
116
+ if (node.init?.type === "ArrowFunctionExpression") {
117
+ reportValidity(
118
+ context,
119
+ node,
120
+ options.function.format,
121
+ options.function.fix,
122
+ options.function.leadingUnderscore,
123
+ options.function.trailingUnderscore
124
+ );
125
+ } else {
126
+ reportValidity(
127
+ context,
128
+ node,
129
+ options.validable.format,
130
+ options.validable.fix,
131
+ options.validable.leadingUnderscore,
132
+ options.validable.trailingUnderscore
133
+ );
134
+ }
135
+ },
136
+ FunctionDeclaration: (node) => {
137
+ reportValidity(
138
+ context,
139
+ node,
140
+ options.function.format,
141
+ options.function.fix,
142
+ options.function.leadingUnderscore,
143
+ options.function.trailingUnderscore
144
+ );
145
+ },
146
+ FunctionExpression: (node) => {
147
+ reportValidity(
148
+ context,
149
+ node,
150
+ options.function.format,
151
+ options.function.fix,
152
+ options.function.leadingUnderscore,
153
+ options.function.trailingUnderscore
154
+ );
155
+ },
156
+ };
157
+ },
158
+ };
159
+
160
+ module.exports = rule;