@atlaskit/eslint-plugin-ui-styling-standard 0.15.2 → 0.16.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.
Files changed (105) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/constellation/consistent-css-prop-usage/usage.mdx +3 -1
  3. package/constellation/index/migration-guide.mdx +309 -0
  4. package/constellation/index/overview.mdx +4 -0
  5. package/constellation/index/props.mdx +9 -5
  6. package/constellation/no-css-tagged-template-expression/usage.mdx +5 -2
  7. package/constellation/no-empty-styled-expression/usage.mdx +5 -2
  8. package/constellation/no-exported-css/usage.mdx +5 -2
  9. package/constellation/no-exported-keyframes/usage.mdx +5 -2
  10. package/constellation/no-invalid-css-map/usage.mdx +5 -2
  11. package/constellation/no-js-xcss/usage.mdx +3 -1
  12. package/constellation/no-keyframes-tagged-template-expression/usage.mdx +5 -2
  13. package/constellation/no-styled-tagged-template-expression/usage.mdx +5 -2
  14. package/constellation/no-suppress-xcss/usage.mdx +6 -3
  15. package/dist/types/rules/no-unsafe-selectors/constants.d.ts +1 -4
  16. package/index.js +9 -9
  17. package/package.json +3 -3
  18. package/scripts/codegen.tsx +223 -284
  19. package/src/rules/__tests__/utils/_tester.tsx +24 -24
  20. package/src/rules/atlaskit-theme/README.md +21 -19
  21. package/src/rules/atlaskit-theme/__tests__/test.ts +53 -53
  22. package/src/rules/atlaskit-theme/index.ts +107 -115
  23. package/src/rules/convert-props-syntax/README.md +32 -22
  24. package/src/rules/convert-props-syntax/__tests__/invalid.test.tsx +46 -46
  25. package/src/rules/convert-props-syntax/__tests__/rule.test.tsx +175 -175
  26. package/src/rules/convert-props-syntax/__tests__/type-annotations.test.tsx +52 -52
  27. package/src/rules/convert-props-syntax/__tests__/unusual-properties.test.tsx +33 -33
  28. package/src/rules/convert-props-syntax/index.tsx +356 -386
  29. package/src/rules/enforce-style-prop/README.md +21 -18
  30. package/src/rules/enforce-style-prop/__tests__/rule.test.tsx +90 -96
  31. package/src/rules/enforce-style-prop/index.tsx +144 -160
  32. package/src/rules/external-rules.tsx +163 -164
  33. package/src/rules/local-cx-xcss/README.md +10 -6
  34. package/src/rules/local-cx-xcss/__tests__/rule.test.tsx +26 -26
  35. package/src/rules/local-cx-xcss/index.tsx +46 -56
  36. package/src/rules/no-array-arguments/README.md +2 -1
  37. package/src/rules/no-array-arguments/__tests__/rule.test.tsx +37 -46
  38. package/src/rules/no-array-arguments/index.tsx +68 -69
  39. package/src/rules/no-classname-prop/README.md +7 -4
  40. package/src/rules/no-classname-prop/__tests__/rule.test.tsx +22 -22
  41. package/src/rules/no-classname-prop/index.tsx +25 -25
  42. package/src/rules/no-container-queries/README.md +11 -9
  43. package/src/rules/no-container-queries/__tests__/rule.test.tsx +47 -50
  44. package/src/rules/no-container-queries/index.tsx +75 -81
  45. package/src/rules/no-dynamic-styles/README.md +16 -19
  46. package/src/rules/no-dynamic-styles/__tests__/rule.test.tsx +58 -67
  47. package/src/rules/no-dynamic-styles/index.tsx +45 -48
  48. package/src/rules/no-exported-styles/README.md +17 -12
  49. package/src/rules/no-exported-styles/__tests__/rule.test.tsx +102 -111
  50. package/src/rules/no-exported-styles/index.tsx +36 -36
  51. package/src/rules/no-global-styles/README.md +15 -14
  52. package/src/rules/no-global-styles/__tests__/no-css-modules.test.tsx +28 -28
  53. package/src/rules/no-global-styles/__tests__/no-global-styles.test.tsx +37 -37
  54. package/src/rules/no-global-styles/index.tsx +59 -62
  55. package/src/rules/no-important-styles/README.md +5 -3
  56. package/src/rules/no-important-styles/__tests__/rule.test.tsx +66 -66
  57. package/src/rules/no-important-styles/index.tsx +49 -49
  58. package/src/rules/no-imported-style-values/README.md +23 -15
  59. package/src/rules/no-imported-style-values/__tests__/rule.test.tsx +123 -123
  60. package/src/rules/no-imported-style-values/index.tsx +116 -120
  61. package/src/rules/no-nested-selectors/README.md +15 -13
  62. package/src/rules/no-nested-selectors/__tests__/rule.test.tsx +141 -128
  63. package/src/rules/no-styled/README.md +14 -9
  64. package/src/rules/no-styled/__tests__/rule.test.tsx +31 -31
  65. package/src/rules/no-styled/index.tsx +34 -41
  66. package/src/rules/no-unsafe-selectors/README.md +103 -93
  67. package/src/rules/no-unsafe-selectors/__tests__/no-grouped-at-rules.test.tsx +21 -24
  68. package/src/rules/no-unsafe-selectors/__tests__/no-increased-specificity.test.tsx +44 -44
  69. package/src/rules/no-unsafe-selectors/__tests__/no-keyframes-at-rules.test.tsx +21 -21
  70. package/src/rules/no-unsafe-selectors/__tests__/no-legacy-pseudo-element-syntax.test.tsx +57 -57
  71. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-at-rules.test.tsx +58 -58
  72. package/src/rules/no-unsafe-selectors/__tests__/no-restricted-pseudos.test.tsx +85 -85
  73. package/src/rules/no-unsafe-selectors/__tests__/no-selectors-object.test.tsx +12 -12
  74. package/src/rules/no-unsafe-selectors/__tests__/no-unparsable-selectors.test.tsx +23 -23
  75. package/src/rules/no-unsafe-selectors/__tests__/rule.test.tsx +133 -114
  76. package/src/rules/no-unsafe-selectors/constants.tsx +69 -77
  77. package/src/rules/no-unsafe-selectors/source-position-utils.tsx +38 -38
  78. package/src/rules/no-unsafe-selectors/walk-css-map.tsx +178 -184
  79. package/src/rules/no-unsafe-values/README.md +34 -40
  80. package/src/rules/no-unsafe-values/__tests__/no-array-values.test.tsx +13 -13
  81. package/src/rules/no-unsafe-values/__tests__/no-conditional-values.test.tsx +18 -18
  82. package/src/rules/no-unsafe-values/__tests__/no-dynamic-keys.test.tsx +104 -104
  83. package/src/rules/no-unsafe-values/__tests__/no-function-calls.test.tsx +128 -128
  84. package/src/rules/no-unsafe-values/__tests__/no-identifier-arguments.test.tsx +23 -23
  85. package/src/rules/no-unsafe-values/__tests__/no-object-access.test.tsx +38 -38
  86. package/src/rules/no-unsafe-values/__tests__/no-object-references.test.tsx +18 -18
  87. package/src/rules/no-unsafe-values/__tests__/no-spread-elements.test.tsx +18 -18
  88. package/src/rules/no-unsafe-values/__tests__/no-unsafe-values.test.tsx +28 -28
  89. package/src/rules/no-unsafe-values/__tests__/no-variables.test.tsx +47 -47
  90. package/src/rules/no-unsafe-values/index.tsx +76 -78
  91. package/src/rules/no-unsafe-values/messages.tsx +12 -12
  92. package/src/rules/no-unsafe-values/utils.tsx +370 -382
  93. package/src/rules/use-compiled/README.md +11 -8
  94. package/src/rules/use-compiled/__tests__/can-auto-fix.test.tsx +32 -32
  95. package/src/rules/use-compiled/__tests__/css.test.tsx +53 -53
  96. package/src/rules/use-compiled/__tests__/imports.test.tsx +90 -95
  97. package/src/rules/use-compiled/__tests__/keyframes.test.tsx +47 -47
  98. package/src/rules/use-compiled/__tests__/pragma.test.tsx +64 -64
  99. package/src/rules/use-compiled/__tests__/styled.test.tsx +53 -53
  100. package/src/rules/use-compiled/__tests__/use-compiled.test.tsx +10 -10
  101. package/src/rules/use-compiled/index.tsx +203 -226
  102. package/src/rules/use-compiled/is-safe.tsx +113 -117
  103. package/src/rules/use-compiled/utils.tsx +14 -18
  104. package/src/rules/utils/create-rule-with-typed-config.tsx +62 -63
  105. package/src/rules/utils/create-rule.tsx +3 -6
@@ -8,14 +8,8 @@ import format from '@af/formatting/sync';
8
8
 
9
9
  import { createSignedArtifact } from '@atlassian/codegen';
10
10
 
11
- import {
12
- type LintRule,
13
- getPathSafeName,
14
- } from '@atlaskit/eslint-utils/create-rule';
15
- import {
16
- externalRules,
17
- type ExternalRuleMeta,
18
- } from '../src/rules/external-rules';
11
+ import { type LintRule, getPathSafeName } from '@atlaskit/eslint-utils/create-rule';
12
+ import { externalRules, type ExternalRuleMeta } from '../src/rules/external-rules';
19
13
 
20
14
  const packagePluginName = '@atlaskit/eslint-plugin-ui-styling-standard';
21
15
  const pluginName = '@atlaskit/ui-styling-standard';
@@ -23,15 +17,15 @@ const pluginPath = 'eslint-plugin-ui-styling-standard';
23
17
  const codegenCommand = `yarn workspace ${packagePluginName} codegen`;
24
18
 
25
19
  interface FoundRule {
26
- module: LintRule | { meta: ExternalRuleMeta };
27
- /**
28
- * This is used for the directory name, import path, and urls
29
- */
30
- pathSafeName: string;
31
- /**
32
- * Friendly name that can include special characters
33
- */
34
- ruleName: string;
20
+ module: LintRule | { meta: ExternalRuleMeta };
21
+ /**
22
+ * This is used for the directory name, import path, and urls
23
+ */
24
+ pathSafeName: string;
25
+ /**
26
+ * Friendly name that can include special characters
27
+ */
28
+ ruleName: string;
35
29
  }
36
30
 
37
31
  /**
@@ -39,169 +33,139 @@ interface FoundRule {
39
33
  * These come from `rules/external-rules.tsx`
40
34
  */
41
35
  interface ExternalRule extends FoundRule {
42
- module: { meta: ExternalRuleMeta };
36
+ module: { meta: ExternalRuleMeta };
43
37
  }
44
38
  interface InternalRule extends FoundRule {
45
- module: LintRule;
39
+ module: LintRule;
46
40
  }
47
41
 
48
42
  interface GeneratedConfig {
49
- name: string;
50
- path: string;
43
+ name: string;
44
+ path: string;
51
45
  }
52
46
 
53
- const ignoreList = [
54
- 'index.codegen.tsx',
55
- 'TEMPLATE.md',
56
- '__tests__',
57
- 'utils',
58
- 'external-rules.tsx',
59
- ];
47
+ const ignoreList = ['index.codegen.tsx', 'TEMPLATE.md', '__tests__', 'utils', 'external-rules.tsx'];
60
48
  const srcDir = join(__dirname, '../src');
61
49
  const rulesDir = join(srcDir, 'rules');
62
50
  const presetsDir = join(srcDir, 'presets');
63
51
  const generatedConfigs: GeneratedConfig[] = [];
64
52
 
65
53
  function ruleIsExternal(rule: FoundRule | ExternalRule): rule is ExternalRule {
66
- return 'isExternal' in rule.module.meta && !!rule.module.meta.isExternal;
54
+ return 'isExternal' in rule.module.meta && !!rule.module.meta.isExternal;
67
55
  }
68
56
  function ruleIsInternal(rule: FoundRule | ExternalRule): rule is InternalRule {
69
- return !ruleIsExternal(rule);
57
+ return !ruleIsExternal(rule);
70
58
  }
71
59
 
72
60
  /**
73
61
  * Get the path to, and file contents for, a rule's README.md file.
74
62
  */
75
63
  async function ruleDocsPath(name: string) {
76
- const absolutePath = join(rulesDir, name, 'README.md');
77
- const relativePath = '.' + absolutePath.replace(process.cwd(), '');
78
-
79
- try {
80
- const file = await fs.readFile(absolutePath, 'utf-8');
81
- return { path: relativePath, file };
82
- } catch (_) {
83
- throw new Error(
84
- `invariant: rule ${name} should have docs at ${absolutePath}`,
85
- );
86
- }
64
+ const absolutePath = join(rulesDir, name, 'README.md');
65
+ const relativePath = '.' + absolutePath.replace(process.cwd(), '');
66
+
67
+ try {
68
+ const file = await fs.readFile(absolutePath, 'utf-8');
69
+ return { path: relativePath, file };
70
+ } catch (_) {
71
+ throw new Error(`invariant: rule ${name} should have docs at ${absolutePath}`);
72
+ }
87
73
  }
88
74
 
89
75
  /**
90
76
  * Generates the preset config, eg. `src/presets/all.codegen.tsx` and `src/presets/recommended.codegen.tsx`
91
77
  */
92
- async function generatePresetConfig(
93
- name: 'all' | 'recommended',
94
- rules: FoundRule[],
95
- ) {
96
- const externalScopes = externalRules
97
- .filter((rule) => rule.name?.includes('/'))
98
- .map((rule) => {
99
- // Take the first part of the name, eg. `@atlaskit/design-system` and wrap it in quotes to be
100
- const groups = rule.name.split('/');
101
- return `'${groups.slice(0, groups.length - 1).join('/')}'`;
102
- });
103
-
104
- // NOTE: Given we have external plugins, we must include them as plugins as well./
105
- const pluginList = [
106
- `'${pluginName}'`,
107
- ...Array.from(new Set(externalScopes)),
108
- ];
109
-
110
- const code = outdent`
78
+ async function generatePresetConfig(name: 'all' | 'recommended', rules: FoundRule[]) {
79
+ const externalScopes = externalRules
80
+ .filter((rule) => rule.name?.includes('/'))
81
+ .map((rule) => {
82
+ // Take the first part of the name, eg. `@atlaskit/design-system` and wrap it in quotes to be
83
+ const groups = rule.name.split('/');
84
+ return `'${groups.slice(0, groups.length - 1).join('/')}'`;
85
+ });
86
+
87
+ // NOTE: Given we have external plugins, we must include them as plugins as well./
88
+ const pluginList = [`'${pluginName}'`, ...Array.from(new Set(externalScopes))];
89
+
90
+ const code = outdent`
111
91
  export default {
112
92
  plugins: [
113
93
  ${pluginList.join(',\n')}],
114
94
  rules: {
115
95
  ${rules
116
- .map((rule) => {
117
- const { severity, recommended, pluginConfig } =
118
- rule.module.meta?.docs || {};
119
-
120
- // `recommended` is a snowflake at this stage ... it can be a string at runtime
121
- // because of legacy `import { createRule } from 'utils/create-rule'` so we take
122
- // into account this possibility (as a fallback) until everything is moved over
123
- // to the new syntax
124
- const calculatedSeverity =
125
- severity ??
126
- (typeof recommended === 'string' ? String(recommended) : 'error');
127
-
128
- let ruleName = rule.ruleName;
129
- if (ruleIsInternal(rule)) {
130
- ruleName = `${pluginName}/${rule.ruleName}`;
131
- }
132
-
133
- if (typeof pluginConfig === 'object' && pluginConfig) {
134
- return `'${ruleName}': ['${calculatedSeverity}', ${JSON.stringify(
135
- pluginConfig,
136
- )}]`;
137
- }
138
-
139
- return `'${ruleName}': '${calculatedSeverity}'`;
140
- })
141
- .join(',')}
96
+ .map((rule) => {
97
+ const { severity, recommended, pluginConfig } = rule.module.meta?.docs || {};
98
+
99
+ // `recommended` is a snowflake at this stage ... it can be a string at runtime
100
+ // because of legacy `import { createRule } from 'utils/create-rule'` so we take
101
+ // into account this possibility (as a fallback) until everything is moved over
102
+ // to the new syntax
103
+ const calculatedSeverity =
104
+ severity ?? (typeof recommended === 'string' ? String(recommended) : 'error');
105
+
106
+ let ruleName = rule.ruleName;
107
+ if (ruleIsInternal(rule)) {
108
+ ruleName = `${pluginName}/${rule.ruleName}`;
109
+ }
110
+
111
+ if (typeof pluginConfig === 'object' && pluginConfig) {
112
+ return `'${ruleName}': ['${calculatedSeverity}', ${JSON.stringify(pluginConfig)}]`;
113
+ }
114
+
115
+ return `'${ruleName}': '${calculatedSeverity}'`;
116
+ })
117
+ .join(',')}
142
118
  },
143
119
  }
144
120
  `;
145
121
 
146
- const filepath = join(presetsDir, `${name}.codegen.tsx`);
147
- await writeFile(filepath, code);
122
+ const filepath = join(presetsDir, `${name}.codegen.tsx`);
123
+ await writeFile(filepath, code);
148
124
 
149
- generatedConfigs.push({
150
- name: camelCase(name),
151
- path: './' + relative(srcDir, join(presetsDir, `${name}.codegen`)),
152
- });
125
+ generatedConfigs.push({
126
+ name: camelCase(name),
127
+ path: './' + relative(srcDir, join(presetsDir, `${name}.codegen`)),
128
+ });
153
129
  }
154
130
 
155
131
  /**
156
132
  * Write contents to a given file, creating a signed artifact if it's not a markdown file.
157
133
  */
158
134
  async function writeFile(filepath: string, code: string) {
159
- await fs.writeFile(
160
- filepath,
161
- extname(filepath).includes('.md')
162
- ? format(code, 'markdown')
163
- : createSignedArtifact(
164
- format(code, 'typescript'),
165
- codegenCommand,
166
- ),
167
- );
135
+ await fs.writeFile(
136
+ filepath,
137
+ extname(filepath).includes('.md')
138
+ ? format(code, 'markdown')
139
+ : createSignedArtifact(format(code, 'typescript'), codegenCommand),
140
+ );
168
141
  }
169
142
 
170
143
  /**
171
144
  * Generates the `src/rules/index.codegen.tsx` entrypoint, exporting all rules.
172
145
  */
173
146
  async function generateRuleIndex(rules: FoundRule[]) {
174
- const internalRules = rules.filter(ruleIsInternal);
147
+ const internalRules = rules.filter(ruleIsInternal);
175
148
 
176
- const code = outdent`
149
+ const code = outdent`
177
150
  ${internalRules
178
- .map(
179
- (rule) =>
180
- `import ${camelCase(rule.pathSafeName)} from './${
181
- rule.pathSafeName
182
- }'`,
183
- )
184
- .join('\n')}
151
+ .map((rule) => `import ${camelCase(rule.pathSafeName)} from './${rule.pathSafeName}'`)
152
+ .join('\n')}
185
153
 
186
154
  export default {
187
- ${internalRules
188
- .map((rule) => `'${rule.ruleName}': ${camelCase(rule.pathSafeName)}`)
189
- .join(',')}
155
+ ${internalRules.map((rule) => `'${rule.ruleName}': ${camelCase(rule.pathSafeName)}`).join(',')}
190
156
  }
191
157
  `;
192
158
 
193
- const filepath = join(rulesDir, 'index.codegen.tsx');
194
- await writeFile(filepath, code);
159
+ const filepath = join(rulesDir, 'index.codegen.tsx');
160
+ await writeFile(filepath, code);
195
161
  }
196
162
 
197
163
  /**
198
164
  * Generates the `src/index.codegen.tsx` entrypoint, exporting all the preset configs.
199
165
  */
200
166
  async function generatePluginIndex() {
201
- const code = outdent`
202
- ${generatedConfigs
203
- .map((config) => `import ${config.name} from '${config.path}'`)
204
- .join('\n')}
167
+ const code = outdent`
168
+ ${generatedConfigs.map((config) => `import ${config.name} from '${config.path}'`).join('\n')}
205
169
 
206
170
  export { default as rules } from './rules/index.codegen'
207
171
 
@@ -210,225 +174,200 @@ async function generatePluginIndex() {
210
174
  }
211
175
  `;
212
176
 
213
- const filepath = join(srcDir, 'index.codegen.tsx');
214
- await writeFile(filepath, code);
177
+ const filepath = join(srcDir, 'index.codegen.tsx');
178
+ await writeFile(filepath, code);
215
179
  }
216
180
 
217
- const conditional = (cond: string, content?: boolean | string) =>
218
- content ? cond : '';
181
+ const conditional = (cond: string, content?: boolean | string) => (content ? cond : '');
219
182
  const optionallyLinkTo = (content: string, url?: string) =>
220
- url ? `<a href="${url}">${content}</a>` : content;
183
+ url ? `<a href="${url}">${content}</a>` : content;
221
184
 
222
185
  /**
223
186
  * Generate each individual rules' documentation pages, eg. `constellation/icon-label/usage.mdx`
224
187
  */
225
188
  async function generateRulePages(rules: FoundRule[]): Promise<void> {
226
- const docsDir = join(srcDir, '../constellation');
227
-
228
- for await (const rule of rules) {
229
- const ruleDir = join(docsDir, rule.pathSafeName);
230
- const destination = join(ruleDir, 'usage.mdx');
231
- await fs.mkdir(ruleDir, { recursive: true });
232
-
233
- if (ruleIsExternal(rule)) {
234
- await generateExternalRulePage(rule, destination);
235
- } else if (ruleIsInternal(rule)) {
236
- await generateInternalRulePage(rule, destination);
237
- } else {
238
- throw new Error(
239
- `invariant: found an unexpected rule type ${rule.ruleName}`,
240
- );
241
- }
242
- }
189
+ const docsDir = join(srcDir, '../constellation');
190
+
191
+ for await (const rule of rules) {
192
+ const ruleDir = join(docsDir, rule.pathSafeName);
193
+ const destination = join(ruleDir, 'usage.mdx');
194
+ await fs.mkdir(ruleDir, { recursive: true });
195
+
196
+ if (ruleIsExternal(rule)) {
197
+ await generateExternalRulePage(rule, destination);
198
+ } else if (ruleIsInternal(rule)) {
199
+ await generateInternalRulePage(rule, destination);
200
+ } else {
201
+ throw new Error(`invariant: found an unexpected rule type ${rule.ruleName}`);
202
+ }
203
+ }
243
204
  }
244
205
 
245
- async function generateExternalRulePage(
246
- rule: ExternalRule,
247
- destination: string,
248
- ) {
249
- if (existsSync(destination)) {
250
- return true;
251
- }
206
+ async function generateExternalRulePage(rule: ExternalRule, destination: string) {
207
+ if (existsSync(destination)) {
208
+ return true;
209
+ }
252
210
 
253
- const ruleContent = outdent`
211
+ const ruleContent = outdent`
254
212
  # ${rule.ruleName}
255
213
  ${rule.module.meta?.docs?.description || ''}
256
214
 
257
215
  ${
258
- rule.module.meta?.docs?.externalUrl
259
- ? `This rule is maintained by another package and configured here; refer to ${rule.module.meta.docs.externalUrl} for more details.`
260
- : ''
261
- }
216
+ rule.module.meta?.docs?.externalUrl
217
+ ? `This rule is maintained by another package and configured here; refer to ${rule.module.meta.docs.externalUrl} for more details.`
218
+ : ''
219
+ }
262
220
  `;
263
221
 
264
- await writeFile(destination, ruleContent);
222
+ await writeFile(destination, ruleContent);
265
223
  }
266
224
 
267
- async function generateInternalRulePage(
268
- rule: InternalRule,
269
- destination: string,
270
- ) {
271
- const { file } = await ruleDocsPath(rule.pathSafeName);
272
- if (file.match(/^# /)) {
273
- throw new Error(
274
- `invariant: ${rule.pathSafeName} README doc should not include a h1 heading as it is injected automatically.`,
275
- );
276
- }
277
-
278
- const ruleContent = outdent`
225
+ async function generateInternalRulePage(rule: InternalRule, destination: string) {
226
+ const { file } = await ruleDocsPath(rule.pathSafeName);
227
+ if (file.match(/^# /)) {
228
+ throw new Error(
229
+ `invariant: ${rule.pathSafeName} README doc should not include a h1 heading as it is injected automatically.`,
230
+ );
231
+ }
232
+
233
+ const ruleContent = outdent`
279
234
  # ${rule.ruleName}
280
235
  ${file}
281
236
  `;
282
237
 
283
- await writeFile(destination, ruleContent);
238
+ await writeFile(destination, ruleContent);
284
239
  }
285
240
 
286
241
  /**
287
242
  * Generates the `RULE_TABLE_CODEGEN` in `constellation/index/usage.mdx` and `README.md`.
288
243
  * This is the list of all rules and their metadata.
289
244
  */
290
- async function generateRuleTable(
291
- filepath: string,
292
- linkTo: 'docs' | 'repo',
293
- rules: FoundRule[],
294
- ) {
295
- const file = await fs.readFile(filepath, 'utf-8');
296
- const rows: string[] = [];
297
-
298
- for (const rule of rules) {
299
- let docsPath: string | undefined;
300
-
301
- if (linkTo === 'docs') {
302
- docsPath = `/components/${pluginPath}/${rule.pathSafeName}/usage`;
303
- } else if (ruleIsInternal(rule)) {
304
- const result = await ruleDocsPath(rule.pathSafeName);
305
- docsPath = result.path;
306
- } else {
307
- docsPath = rule.module.meta?.docs?.url;
308
- }
309
-
310
- const link = optionallyLinkTo(rule.ruleName, docsPath);
311
- const description = rule.module.meta?.docs?.description || '';
312
- const recommended = conditional('Yes', rule.module.meta?.docs?.recommended);
313
- const fixable = conditional('Yes', rule.module.meta?.fixable);
314
- const suggestions = conditional('Yes', rule.module.meta?.hasSuggestions);
315
-
316
- rows.push(outdent`
245
+ async function generateRuleTable(filepath: string, linkTo: 'docs' | 'repo', rules: FoundRule[]) {
246
+ const file = await fs.readFile(filepath, 'utf-8');
247
+ const rows: string[] = [];
248
+
249
+ for (const rule of rules) {
250
+ let docsPath: string | undefined;
251
+
252
+ if (linkTo === 'docs') {
253
+ docsPath = `/components/${pluginPath}/${rule.pathSafeName}/usage`;
254
+ } else if (ruleIsInternal(rule)) {
255
+ const result = await ruleDocsPath(rule.pathSafeName);
256
+ docsPath = result.path;
257
+ } else {
258
+ docsPath = rule.module.meta?.docs?.url;
259
+ }
260
+
261
+ const link = optionallyLinkTo(rule.ruleName, docsPath);
262
+ const description = rule.module.meta?.docs?.description || '';
263
+ const recommended = conditional('Yes', rule.module.meta?.docs?.recommended);
264
+ const fixable = conditional('Yes', rule.module.meta?.fixable);
265
+ const suggestions = conditional('Yes', rule.module.meta?.hasSuggestions);
266
+
267
+ rows.push(outdent`
317
268
  | ${link} | ${description} | ${recommended} | ${fixable} | ${suggestions} |
318
269
  `);
319
- }
270
+ }
320
271
 
321
- const code = outdent`
272
+ const code = outdent`
322
273
  | Rule | Description | Recommended | Fixable | Suggestions |
323
274
  | ---- | -- | -- | -- | -- |
324
275
  ${rows.join('\n')}
325
276
  `;
326
277
 
327
- const found =
328
- /<!-- START_RULE_TABLE_CODEGEN -->(.|\n)*<!-- END_RULE_TABLE_CODEGEN -->/.exec(
329
- file,
330
- );
278
+ const found = /<!-- START_RULE_TABLE_CODEGEN -->(.|\n)*<!-- END_RULE_TABLE_CODEGEN -->/.exec(
279
+ file,
280
+ );
331
281
 
332
- if (!found) {
333
- throw new Error(
334
- `invariant: could not find the partial codegen section in ${filepath}`,
335
- );
336
- }
282
+ if (!found) {
283
+ throw new Error(`invariant: could not find the partial codegen section in ${filepath}`);
284
+ }
337
285
 
338
- const updatedFile = file.replace(
339
- found[0],
340
- outdent`
286
+ const updatedFile = file.replace(
287
+ found[0],
288
+ outdent`
341
289
  <!-- START_RULE_TABLE_CODEGEN -->
342
290
  <!-- @codegenCommand ${codegenCommand} -->
343
291
  ${code}
344
292
  <!-- END_RULE_TABLE_CODEGEN -->
345
293
  `,
346
- );
294
+ );
347
295
 
348
- await writeFile(filepath, updatedFile);
296
+ await writeFile(filepath, updatedFile);
349
297
  }
350
298
 
351
299
  const validateInternalRule = (rule: InternalRule) => {
352
- if (typeof rule.ruleName !== 'string' || !rule.ruleName) {
353
- throw new Error(
354
- `invariant: The ${rule.pathSafeName} rule must use the createLintRule() function`,
355
- );
356
- }
357
-
358
- if (ruleIsInternal(rule) && rule.pathSafeName !== rule.ruleName) {
359
- throw new Error(
360
- `invariant: module name ${rule.pathSafeName} does not match lint rule name ${rule.ruleName}`,
361
- );
362
- }
363
-
364
- if (rule.pathSafeName.includes('@') || rule.pathSafeName.includes('/')) {
365
- throw new Error(
366
- `invariant: name ${rule.pathSafeName} is not valid, specify a path-safe name`,
367
- );
368
- }
300
+ if (typeof rule.ruleName !== 'string' || !rule.ruleName) {
301
+ throw new Error(
302
+ `invariant: The ${rule.pathSafeName} rule must use the createLintRule() function`,
303
+ );
304
+ }
305
+
306
+ if (ruleIsInternal(rule) && rule.pathSafeName !== rule.ruleName) {
307
+ throw new Error(
308
+ `invariant: module name ${rule.pathSafeName} does not match lint rule name ${rule.ruleName}`,
309
+ );
310
+ }
311
+
312
+ if (rule.pathSafeName.includes('@') || rule.pathSafeName.includes('/')) {
313
+ throw new Error(`invariant: name ${rule.pathSafeName} is not valid, specify a path-safe name`);
314
+ }
369
315
  };
370
316
 
371
317
  /**
372
318
  * Fetch all rules in `rules/*` and generate all associated contents.
373
319
  */
374
320
  async function generate() {
375
- const rulePaths = await fs.readdir(rulesDir);
376
- const recommended: FoundRule[] = [];
377
- const rules: FoundRule[] = [];
378
-
379
- for (const filename of rulePaths) {
380
- if (ignoreList.includes(filename)) {
381
- continue;
382
- }
383
-
384
- const dirname = filename.replace(extname(filename), '');
385
- const filenameWithExt = filename.endsWith('.tsx')
386
- ? filename + '.tsx'
387
- : filename;
388
-
389
- const rule: LintRule = (await import(join(rulesDir, filenameWithExt)))
390
- .default;
391
-
392
- const foundRule: InternalRule = {
393
- module: rule,
394
- pathSafeName: dirname,
395
- ruleName: rule.name || rule.meta.name,
396
- };
397
-
398
- validateInternalRule(foundRule);
399
-
400
- rules.push(foundRule);
401
- }
402
-
403
- for (const rule of externalRules) {
404
- const foundRule: ExternalRule = {
405
- module: { meta: rule },
406
- pathSafeName: rule.displayName || getPathSafeName(rule.name),
407
- ruleName: rule.name,
408
- };
409
-
410
- // We don't validate these; types should do their job instead.
411
- rules.push(foundRule);
412
- }
413
-
414
- rules.forEach((rule) => {
415
- if (rule.module.meta?.docs?.recommended) {
416
- recommended.push(rule);
417
- }
418
- });
419
-
420
- // NOTE: Some of these could be awaited in parallel, but it's not worth the complexity for <1s diff.
421
- await generateRuleIndex(rules);
422
- await generatePresetConfig('all', rules);
423
- await generatePresetConfig('recommended', recommended);
424
- await generatePluginIndex();
425
- await generateRulePages(rules);
426
- await generateRuleTable(join(srcDir, '../README.md'), 'repo', rules);
427
- await generateRuleTable(
428
- join(srcDir, '../constellation/index/overview.mdx'),
429
- 'docs',
430
- rules,
431
- );
321
+ const rulePaths = await fs.readdir(rulesDir);
322
+ const recommended: FoundRule[] = [];
323
+ const rules: FoundRule[] = [];
324
+
325
+ for (const filename of rulePaths) {
326
+ if (ignoreList.includes(filename)) {
327
+ continue;
328
+ }
329
+
330
+ const dirname = filename.replace(extname(filename), '');
331
+ const filenameWithExt = filename.endsWith('.tsx') ? filename + '.tsx' : filename;
332
+
333
+ const rule: LintRule = (await import(join(rulesDir, filenameWithExt))).default;
334
+
335
+ const foundRule: InternalRule = {
336
+ module: rule,
337
+ pathSafeName: dirname,
338
+ ruleName: rule.name || rule.meta.name,
339
+ };
340
+
341
+ validateInternalRule(foundRule);
342
+
343
+ rules.push(foundRule);
344
+ }
345
+
346
+ for (const rule of externalRules) {
347
+ const foundRule: ExternalRule = {
348
+ module: { meta: rule },
349
+ pathSafeName: rule.displayName || getPathSafeName(rule.name),
350
+ ruleName: rule.name,
351
+ };
352
+
353
+ // We don't validate these; types should do their job instead.
354
+ rules.push(foundRule);
355
+ }
356
+
357
+ rules.forEach((rule) => {
358
+ if (rule.module.meta?.docs?.recommended) {
359
+ recommended.push(rule);
360
+ }
361
+ });
362
+
363
+ // NOTE: Some of these could be awaited in parallel, but it's not worth the complexity for <1s diff.
364
+ await generateRuleIndex(rules);
365
+ await generatePresetConfig('all', rules);
366
+ await generatePresetConfig('recommended', recommended);
367
+ await generatePluginIndex();
368
+ await generateRulePages(rules);
369
+ await generateRuleTable(join(srcDir, '../README.md'), 'repo', rules);
370
+ await generateRuleTable(join(srcDir, '../constellation/index/overview.mdx'), 'docs', rules);
432
371
  }
433
372
 
434
373
  generate();