@bleedingdev/modern-js-code-tools 3.2.0-ultramodern.111
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/LICENSE +21 -0
- package/dist/cjs/cli/i18n-check.cjs +73 -0
- package/dist/cjs/cli/oxlint.cjs +174 -0
- package/dist/cjs/cli/workspace-source-check.cjs +179 -0
- package/dist/cjs/index.cjs +58 -0
- package/dist/cjs/oxlint-plugin.cjs +354 -0
- package/dist/esm/cli/i18n-check.js +26 -0
- package/dist/esm/cli/oxlint.js +118 -0
- package/dist/esm/cli/workspace-source-check.js +124 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/oxlint-plugin.js +316 -0
- package/dist/esm-node/cli/i18n-check.js +27 -0
- package/dist/esm-node/cli/oxlint.js +119 -0
- package/dist/esm-node/cli/workspace-source-check.js +125 -0
- package/dist/esm-node/index.js +4 -0
- package/dist/esm-node/oxlint-plugin.js +317 -0
- package/dist/types/cli/i18n-check.d.ts +9 -0
- package/dist/types/cli/oxlint.d.ts +22 -0
- package/dist/types/cli/workspace-source-check.d.ts +8 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/oxlint-plugin.d.ts +63 -0
- package/package.json +80 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
const commonOptionsSchema = {
|
|
31
|
+
type: 'object',
|
|
32
|
+
properties: {
|
|
33
|
+
allowElements: {
|
|
34
|
+
type: 'array',
|
|
35
|
+
items: {
|
|
36
|
+
type: 'string'
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
ignoreCommentPattern: {
|
|
40
|
+
type: 'string'
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
additionalProperties: false
|
|
44
|
+
};
|
|
45
|
+
const attributeOptionsSchema = {
|
|
46
|
+
type: 'object',
|
|
47
|
+
properties: {
|
|
48
|
+
...commonOptionsSchema.properties,
|
|
49
|
+
visibleAttributes: {
|
|
50
|
+
type: 'array',
|
|
51
|
+
items: {
|
|
52
|
+
type: 'string'
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
additionalProperties: false
|
|
57
|
+
};
|
|
58
|
+
const translationOptionsSchema = {
|
|
59
|
+
type: 'object',
|
|
60
|
+
properties: {
|
|
61
|
+
translationFunctions: {
|
|
62
|
+
type: 'array',
|
|
63
|
+
items: {
|
|
64
|
+
type: 'string'
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
additionalProperties: false
|
|
69
|
+
};
|
|
70
|
+
const DEFAULT_VISIBLE_ATTRIBUTES = [
|
|
71
|
+
'aria-label',
|
|
72
|
+
"aria-description",
|
|
73
|
+
"aria-roledescription",
|
|
74
|
+
'aria-valuetext',
|
|
75
|
+
'alt',
|
|
76
|
+
'placeholder',
|
|
77
|
+
'title'
|
|
78
|
+
];
|
|
79
|
+
const DEFAULT_ALLOWED_ELEMENTS = [
|
|
80
|
+
'code',
|
|
81
|
+
'kbd',
|
|
82
|
+
'samp'
|
|
83
|
+
];
|
|
84
|
+
const DEFAULT_TRANSLATION_FUNCTIONS = [
|
|
85
|
+
't'
|
|
86
|
+
];
|
|
87
|
+
const DEFAULT_IGNORE_COMMENT_PATTERN = 'i18n-ignore';
|
|
88
|
+
const LETTER_PATTERN = /\p{L}/u;
|
|
89
|
+
const SPLIT_TRANSLATION_KEY_PATTERN = /\.(?:prefix|suffix|before|after)$/u;
|
|
90
|
+
const isRecord = (value)=>Boolean(value) && 'object' == typeof value && !Array.isArray(value);
|
|
91
|
+
const asStringArray = (value, fallback)=>Array.isArray(value) && value.every((item)=>'string' == typeof item) ? value : fallback;
|
|
92
|
+
const getCommonRuleOption = (context, defaults)=>{
|
|
93
|
+
const options = context.options?.[0];
|
|
94
|
+
if (!isRecord(options)) return defaults;
|
|
95
|
+
return {
|
|
96
|
+
allowElements: asStringArray(options.allowElements, defaults.allowElements),
|
|
97
|
+
ignoreCommentPattern: 'string' == typeof options.ignoreCommentPattern ? options.ignoreCommentPattern : defaults.ignoreCommentPattern
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
const getAttributeRuleOption = (context, defaults)=>{
|
|
101
|
+
const options = context.options?.[0];
|
|
102
|
+
const commonOptions = getCommonRuleOption(context, defaults);
|
|
103
|
+
return {
|
|
104
|
+
...commonOptions,
|
|
105
|
+
visibleAttributes: isRecord(options) ? asStringArray(options.visibleAttributes, defaults.visibleAttributes) : defaults.visibleAttributes
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
const getSplitTranslationKeyRuleOption = (context, defaults)=>{
|
|
109
|
+
const options = context.options?.[0];
|
|
110
|
+
if (!isRecord(options)) return defaults;
|
|
111
|
+
return {
|
|
112
|
+
translationFunctions: asStringArray(options.translationFunctions, defaults.translationFunctions)
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
const normalizeVisibleText = (value)=>value.replaceAll(/\s+/gu, ' ').trim();
|
|
116
|
+
const hasLetters = (value)=>LETTER_PATTERN.test(value);
|
|
117
|
+
const getNodeName = (node)=>{
|
|
118
|
+
if (!node) return;
|
|
119
|
+
if ('string' == typeof node.name) return node.name;
|
|
120
|
+
if ('JSXIdentifier' === node.type && isRecord(node.name) && 'string' == typeof node.name.name) return node.name.name;
|
|
121
|
+
if ('JSXIdentifier' === node.type && 'string' == typeof node.name) return node.name;
|
|
122
|
+
if (('JSXMemberExpression' === node.type || 'MemberExpression' === node.type) && isRecord(node.property) && true !== node.computed) {
|
|
123
|
+
const objectName = getNodeName(node.object);
|
|
124
|
+
const propertyName = getNodeName(node.property);
|
|
125
|
+
return objectName && propertyName ? `${objectName}.${propertyName}` : propertyName;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
const getJsxElementName = (node)=>{
|
|
129
|
+
if (node?.type !== 'JSXElement') return;
|
|
130
|
+
return getNodeName(node.openingElement?.name);
|
|
131
|
+
};
|
|
132
|
+
const hasAllowedElementAncestor = (node, allowedElements)=>{
|
|
133
|
+
let current = node;
|
|
134
|
+
while(current){
|
|
135
|
+
const elementName = getJsxElementName(current);
|
|
136
|
+
if (elementName && allowedElements.has(elementName)) return true;
|
|
137
|
+
current = current.parent;
|
|
138
|
+
}
|
|
139
|
+
return false;
|
|
140
|
+
};
|
|
141
|
+
const getTemplateLiteralValue = (node)=>{
|
|
142
|
+
if ('TemplateLiteral' !== node.type || (node.expressions?.length ?? 0) > 0) return;
|
|
143
|
+
const quasi = node.quasis?.[0];
|
|
144
|
+
if (!isRecord(quasi?.value)) return;
|
|
145
|
+
const cooked = quasi.value.cooked;
|
|
146
|
+
const raw = quasi.value.raw;
|
|
147
|
+
return 'string' == typeof cooked ? cooked : 'string' == typeof raw ? raw : void 0;
|
|
148
|
+
};
|
|
149
|
+
const getStringLiteralValue = (node)=>{
|
|
150
|
+
if (!node) return;
|
|
151
|
+
if (('Literal' === node.type || 'StringLiteral' === node.type) && 'string' == typeof node.value) return node.value;
|
|
152
|
+
return getTemplateLiteralValue(node);
|
|
153
|
+
};
|
|
154
|
+
const expressionStringValue = (node)=>getStringLiteralValue(node?.type === 'JSXExpressionContainer' ? node.expression : node);
|
|
155
|
+
const getLine = (node)=>node.loc?.start?.line;
|
|
156
|
+
const hasIgnoreComment = (node, context, pattern)=>{
|
|
157
|
+
const sourceCode = context.getSourceCode?.();
|
|
158
|
+
const nodeLine = getLine(node);
|
|
159
|
+
if (!sourceCode?.getAllComments || void 0 === nodeLine) return false;
|
|
160
|
+
return sourceCode.getAllComments().some((comment)=>{
|
|
161
|
+
const commentValue = String(comment.value ?? '');
|
|
162
|
+
if (!pattern.test(commentValue)) return false;
|
|
163
|
+
const startLine = comment.loc?.start?.line;
|
|
164
|
+
const endLine = comment.loc?.end?.line ?? startLine;
|
|
165
|
+
return void 0 !== startLine && void 0 !== endLine && startLine <= nodeLine + 1 && endLine >= nodeLine - 1;
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
const getIgnorePattern = (options)=>new RegExp(options.ignoreCommentPattern, 'u');
|
|
169
|
+
const reportVisibleText = (context, node, text)=>{
|
|
170
|
+
context.report({
|
|
171
|
+
node,
|
|
172
|
+
message: `Move user-visible JSX text to locale resources: ${JSON.stringify(text)}`
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
const createNoHardcodedJsxTextRule = ()=>({
|
|
176
|
+
meta: {
|
|
177
|
+
type: 'problem',
|
|
178
|
+
docs: {
|
|
179
|
+
description: 'Disallow literal user-visible text in JSX children in UltraModern generated apps.'
|
|
180
|
+
},
|
|
181
|
+
schema: [
|
|
182
|
+
commonOptionsSchema
|
|
183
|
+
]
|
|
184
|
+
},
|
|
185
|
+
create (context) {
|
|
186
|
+
const options = getCommonRuleOption(context, {
|
|
187
|
+
allowElements: DEFAULT_ALLOWED_ELEMENTS,
|
|
188
|
+
ignoreCommentPattern: DEFAULT_IGNORE_COMMENT_PATTERN
|
|
189
|
+
});
|
|
190
|
+
const allowedElements = new Set(options.allowElements);
|
|
191
|
+
const ignorePattern = getIgnorePattern(options);
|
|
192
|
+
const shouldSkipNode = (node)=>hasAllowedElementAncestor(node, allowedElements) || hasIgnoreComment(node, context, ignorePattern);
|
|
193
|
+
return {
|
|
194
|
+
JSXText (node) {
|
|
195
|
+
const text = normalizeVisibleText(String(node.value ?? ''));
|
|
196
|
+
if (!text || !hasLetters(text) || shouldSkipNode(node)) return;
|
|
197
|
+
reportVisibleText(context, node, text);
|
|
198
|
+
},
|
|
199
|
+
JSXExpressionContainer (node) {
|
|
200
|
+
if (node.parent?.type !== 'JSXElement' && node.parent?.type !== 'JSXFragment') return;
|
|
201
|
+
const text = normalizeVisibleText(expressionStringValue(node.expression) ?? '');
|
|
202
|
+
if (!text || !hasLetters(text) || shouldSkipNode(node)) return;
|
|
203
|
+
reportVisibleText(context, node, text);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
const createNoLiteralVisibleJsxAttributesRule = ()=>({
|
|
209
|
+
meta: {
|
|
210
|
+
type: 'problem',
|
|
211
|
+
docs: {
|
|
212
|
+
description: 'Disallow literal user-visible JSX attribute text in UltraModern generated apps.'
|
|
213
|
+
},
|
|
214
|
+
schema: [
|
|
215
|
+
attributeOptionsSchema
|
|
216
|
+
]
|
|
217
|
+
},
|
|
218
|
+
create (context) {
|
|
219
|
+
const options = getAttributeRuleOption(context, {
|
|
220
|
+
allowElements: DEFAULT_ALLOWED_ELEMENTS,
|
|
221
|
+
ignoreCommentPattern: DEFAULT_IGNORE_COMMENT_PATTERN,
|
|
222
|
+
visibleAttributes: DEFAULT_VISIBLE_ATTRIBUTES
|
|
223
|
+
});
|
|
224
|
+
const visibleAttributes = new Set(options.visibleAttributes);
|
|
225
|
+
const ignorePattern = getIgnorePattern(options);
|
|
226
|
+
return {
|
|
227
|
+
JSXAttribute (node) {
|
|
228
|
+
const attributeName = getNodeName(node.name);
|
|
229
|
+
if (!attributeName || !visibleAttributes.has(attributeName)) return;
|
|
230
|
+
const text = normalizeVisibleText(expressionStringValue(node.value) ?? '');
|
|
231
|
+
if (!text || !hasLetters(text) || hasIgnoreComment(node, context, ignorePattern)) return;
|
|
232
|
+
context.report({
|
|
233
|
+
node,
|
|
234
|
+
message: `Move literal ${attributeName} copy to locale resources: ${JSON.stringify(text)}`
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
const getSourceText = (context, node)=>context.getSourceCode?.().getText?.(node) ?? '';
|
|
241
|
+
const looksLikeLocaleTest = (context, node)=>{
|
|
242
|
+
const text = getSourceText(context, node);
|
|
243
|
+
return /\b(?:language|locale|lng|currentLanguage)\b/u.test(text) && /(?:={2,3}|!==?)/u.test(text) && /['"][a-z]{2}(?:-[A-Za-z0-9]+)?['"]/u.test(text);
|
|
244
|
+
};
|
|
245
|
+
const isAllowedBranchLiteral = (text)=>new Set([
|
|
246
|
+
'page',
|
|
247
|
+
'undefined',
|
|
248
|
+
'null',
|
|
249
|
+
'true',
|
|
250
|
+
'false'
|
|
251
|
+
]).has(text);
|
|
252
|
+
const createNoManualLocaleCopyBranchingRule = ()=>({
|
|
253
|
+
meta: {
|
|
254
|
+
type: 'problem',
|
|
255
|
+
docs: {
|
|
256
|
+
description: 'Disallow manual locale conditionals that choose user-visible copy.'
|
|
257
|
+
},
|
|
258
|
+
schema: []
|
|
259
|
+
},
|
|
260
|
+
create (context) {
|
|
261
|
+
const reportBranch = (node, text)=>{
|
|
262
|
+
context.report({
|
|
263
|
+
node,
|
|
264
|
+
message: `Move locale-specific copy branch to i18n resources: ${JSON.stringify(normalizeVisibleText(text))}`
|
|
265
|
+
});
|
|
266
|
+
};
|
|
267
|
+
return {
|
|
268
|
+
ConditionalExpression (node) {
|
|
269
|
+
if (!node.test || !looksLikeLocaleTest(context, node.test)) return;
|
|
270
|
+
for (const branch of [
|
|
271
|
+
node.consequent,
|
|
272
|
+
node.alternate
|
|
273
|
+
]){
|
|
274
|
+
const text = expressionStringValue(branch);
|
|
275
|
+
if (text && hasLetters(text) && !isAllowedBranchLiteral(text.trim())) reportBranch(branch, text);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
const getCallExpressionName = (node)=>getNodeName(node);
|
|
282
|
+
const createNoSplitTranslationKeysRule = ()=>({
|
|
283
|
+
meta: {
|
|
284
|
+
type: 'problem',
|
|
285
|
+
docs: {
|
|
286
|
+
description: 'Disallow split phrase translation key suffixes such as .prefix and .suffix.'
|
|
287
|
+
},
|
|
288
|
+
schema: [
|
|
289
|
+
translationOptionsSchema
|
|
290
|
+
]
|
|
291
|
+
},
|
|
292
|
+
create (context) {
|
|
293
|
+
const options = getSplitTranslationKeyRuleOption(context, {
|
|
294
|
+
translationFunctions: DEFAULT_TRANSLATION_FUNCTIONS
|
|
295
|
+
});
|
|
296
|
+
const translationFunctions = new Set(options.translationFunctions);
|
|
297
|
+
return {
|
|
298
|
+
CallExpression (node) {
|
|
299
|
+
const calleeName = getCallExpressionName(node.callee);
|
|
300
|
+
if (!calleeName || !translationFunctions.has(calleeName)) return;
|
|
301
|
+
const key = expressionStringValue(node.arguments?.[0]);
|
|
302
|
+
if (!key || !SPLIT_TRANSLATION_KEY_PATTERN.test(key)) return;
|
|
303
|
+
context.report({
|
|
304
|
+
node,
|
|
305
|
+
message: 'Keep translator-owned phrases whole instead of using split translation keys.'
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
const createNoLegacyMfBoundaryAttributesRule = ()=>({
|
|
312
|
+
meta: {
|
|
313
|
+
type: 'problem',
|
|
314
|
+
docs: {
|
|
315
|
+
description: 'Disallow legacy Module Federation boundary attributes in generated UltraModern workspaces.'
|
|
316
|
+
},
|
|
317
|
+
schema: []
|
|
318
|
+
},
|
|
319
|
+
create (context) {
|
|
320
|
+
return {
|
|
321
|
+
JSXAttribute (node) {
|
|
322
|
+
const attributeName = getNodeName(node.name);
|
|
323
|
+
if ('data-mf-boundary' !== attributeName && 'data-mf-remote' !== attributeName && 'data-mf-expose' !== attributeName) return;
|
|
324
|
+
context.report({
|
|
325
|
+
node,
|
|
326
|
+
message: 'Use data-modern-boundary-id and data-modern-mf-expose instead of legacy data-mf-* boundary attributes.'
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
const plugin = {
|
|
333
|
+
meta: {
|
|
334
|
+
name: 'ultramodern'
|
|
335
|
+
},
|
|
336
|
+
rules: {
|
|
337
|
+
'no-hardcoded-jsx-text': createNoHardcodedJsxTextRule(),
|
|
338
|
+
'no-legacy-mf-boundary-attributes': createNoLegacyMfBoundaryAttributesRule(),
|
|
339
|
+
'no-literal-visible-jsx-attributes': createNoLiteralVisibleJsxAttributesRule(),
|
|
340
|
+
'no-manual-locale-copy-branching': createNoManualLocaleCopyBranchingRule(),
|
|
341
|
+
'no-split-translation-keys': createNoSplitTranslationKeysRule()
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
const __rspack_default_export = plugin;
|
|
345
|
+
__webpack_require__.d(__webpack_exports__, {}, {
|
|
346
|
+
default: __rspack_default_export
|
|
347
|
+
});
|
|
348
|
+
exports["default"] = __webpack_exports__["default"];
|
|
349
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
350
|
+
"default"
|
|
351
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
352
|
+
Object.defineProperty(exports, '__esModule', {
|
|
353
|
+
value: true
|
|
354
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { printOxlintOutput, runOxlintRules } from "./oxlint.js";
|
|
2
|
+
const SINGLE_APP_I18N_SUCCESS = 'No hardcoded user-visible JSX strings found.';
|
|
3
|
+
const SINGLE_APP_I18N_FAILURE = 'Hardcoded user-visible JSX strings found. Move copy to locale JSON files.';
|
|
4
|
+
const runSingleAppI18nCheck = ({ cwd = process.cwd(), targets = [
|
|
5
|
+
'src'
|
|
6
|
+
] } = {})=>{
|
|
7
|
+
const result = runOxlintRules({
|
|
8
|
+
cwd,
|
|
9
|
+
targets,
|
|
10
|
+
rules: {
|
|
11
|
+
'ultramodern/no-hardcoded-jsx-text': 'error',
|
|
12
|
+
'ultramodern/no-literal-visible-jsx-attributes': 'error'
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
if (0 === result.exitCode) {
|
|
16
|
+
console.log(SINGLE_APP_I18N_SUCCESS);
|
|
17
|
+
return 0;
|
|
18
|
+
}
|
|
19
|
+
console.error(SINGLE_APP_I18N_FAILURE);
|
|
20
|
+
printOxlintOutput(result);
|
|
21
|
+
return result.exitCode;
|
|
22
|
+
};
|
|
23
|
+
const main = ()=>{
|
|
24
|
+
process.exitCode = runSingleAppI18nCheck();
|
|
25
|
+
};
|
|
26
|
+
export { SINGLE_APP_I18N_FAILURE, SINGLE_APP_I18N_SUCCESS, main, runSingleAppI18nCheck };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import node_fs from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import node_os from "node:os";
|
|
5
|
+
import node_path from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
const oxlint_require = createRequire(import.meta.url);
|
|
8
|
+
const ignoredDirectories = new Set([
|
|
9
|
+
'.modern',
|
|
10
|
+
'.modernjs',
|
|
11
|
+
'.output',
|
|
12
|
+
'dist',
|
|
13
|
+
'node_modules'
|
|
14
|
+
]);
|
|
15
|
+
const packageNames = new Set([
|
|
16
|
+
'@modern-js/code-tools',
|
|
17
|
+
'@bleedingdev/modern-js-code-tools'
|
|
18
|
+
]);
|
|
19
|
+
const resolveExistingPath = (candidates)=>candidates.find((candidate)=>node_fs.existsSync(candidate));
|
|
20
|
+
const findPackageRoot = ()=>{
|
|
21
|
+
let directory = node_path.dirname(fileURLToPath(import.meta.url));
|
|
22
|
+
while(directory !== node_path.dirname(directory)){
|
|
23
|
+
const packageJsonPath = node_path.join(directory, 'package.json');
|
|
24
|
+
if (node_fs.existsSync(packageJsonPath)) try {
|
|
25
|
+
const packageJson = JSON.parse(node_fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
26
|
+
if (packageNames.has(packageJson.name)) return directory;
|
|
27
|
+
} catch {
|
|
28
|
+
return directory;
|
|
29
|
+
}
|
|
30
|
+
directory = node_path.dirname(directory);
|
|
31
|
+
}
|
|
32
|
+
throw new Error('Unable to resolve @modern-js/code-tools package root.');
|
|
33
|
+
};
|
|
34
|
+
const resolvePluginPath = ()=>{
|
|
35
|
+
const root = findPackageRoot();
|
|
36
|
+
const sourcePluginPath = node_path.join(root, 'src/oxlint-plugin.ts');
|
|
37
|
+
const pluginPath = resolveExistingPath([
|
|
38
|
+
node_path.join(root, 'dist/esm-node/oxlint-plugin.mjs'),
|
|
39
|
+
node_path.join(root, 'dist/esm-node/oxlint-plugin.js'),
|
|
40
|
+
node_path.join(root, 'dist/esm/oxlint-plugin.mjs'),
|
|
41
|
+
node_path.join(root, 'dist/esm/oxlint-plugin.js'),
|
|
42
|
+
node_path.join(root, 'dist/cjs/oxlint-plugin.js'),
|
|
43
|
+
node_path.join(root, 'dist/cjs/oxlint-plugin.cjs'),
|
|
44
|
+
sourcePluginPath
|
|
45
|
+
]);
|
|
46
|
+
if (!pluginPath) throw new Error('Unable to resolve @modern-js/code-tools Oxlint plugin.');
|
|
47
|
+
return pluginPath;
|
|
48
|
+
};
|
|
49
|
+
const resolveOxlintBin = ()=>{
|
|
50
|
+
const packageJsonPath = oxlint_require.resolve('oxlint/package.json');
|
|
51
|
+
const packageJson = JSON.parse(node_fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
52
|
+
const binRelativePath = 'string' == typeof packageJson.bin ? packageJson.bin : packageJson.bin?.oxlint;
|
|
53
|
+
if (!binRelativePath) throw new Error('Unable to resolve oxlint binary.');
|
|
54
|
+
return node_path.join(node_path.dirname(packageJsonPath), binRelativePath);
|
|
55
|
+
};
|
|
56
|
+
const existingTargets = (cwd, targets)=>targets.map((target)=>node_path.resolve(cwd, target)).filter((target)=>node_fs.existsSync(target));
|
|
57
|
+
const containsLintableSource = (filePath)=>{
|
|
58
|
+
if (!node_fs.existsSync(filePath)) return false;
|
|
59
|
+
const stats = node_fs.statSync(filePath);
|
|
60
|
+
if (stats.isFile()) return /\.(?:js|jsx|ts|tsx)$/u.test(filePath) && !filePath.endsWith('.d.ts');
|
|
61
|
+
if (!stats.isDirectory()) return false;
|
|
62
|
+
for (const entry of node_fs.readdirSync(filePath, {
|
|
63
|
+
withFileTypes: true
|
|
64
|
+
}))if (!(entry.isDirectory() && ignoredDirectories.has(entry.name))) {
|
|
65
|
+
if (containsLintableSource(node_path.join(filePath, entry.name))) return true;
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
};
|
|
69
|
+
const runOxlintRules = ({ cwd, targets, rules })=>{
|
|
70
|
+
const resolvedTargets = existingTargets(cwd, targets);
|
|
71
|
+
if (0 === resolvedTargets.length || !resolvedTargets.some((target)=>containsLintableSource(target))) return {
|
|
72
|
+
exitCode: 0,
|
|
73
|
+
stdout: '',
|
|
74
|
+
stderr: ''
|
|
75
|
+
};
|
|
76
|
+
const tempDir = node_fs.mkdtempSync(node_path.join(node_os.tmpdir(), 'ultramodern-oxlint-'));
|
|
77
|
+
const configPath = node_path.join(tempDir, 'oxlint.config.mjs');
|
|
78
|
+
const pluginPath = resolvePluginPath();
|
|
79
|
+
node_fs.writeFileSync(configPath, `export default {
|
|
80
|
+
jsPlugins: [${JSON.stringify(pluginPath)}],
|
|
81
|
+
rules: ${JSON.stringify(rules, null, 2)}
|
|
82
|
+
};
|
|
83
|
+
`, 'utf-8');
|
|
84
|
+
try {
|
|
85
|
+
const result = spawnSync(process.execPath, [
|
|
86
|
+
resolveOxlintBin(),
|
|
87
|
+
...resolvedTargets,
|
|
88
|
+
'--config',
|
|
89
|
+
configPath,
|
|
90
|
+
'--format',
|
|
91
|
+
'unix',
|
|
92
|
+
'--quiet'
|
|
93
|
+
], {
|
|
94
|
+
cwd,
|
|
95
|
+
encoding: 'utf-8',
|
|
96
|
+
stdio: [
|
|
97
|
+
'ignore',
|
|
98
|
+
'pipe',
|
|
99
|
+
'pipe'
|
|
100
|
+
]
|
|
101
|
+
});
|
|
102
|
+
return {
|
|
103
|
+
exitCode: result.status ?? 1,
|
|
104
|
+
stdout: result.stdout ?? '',
|
|
105
|
+
stderr: result.stderr ?? ''
|
|
106
|
+
};
|
|
107
|
+
} finally{
|
|
108
|
+
node_fs.rmSync(tempDir, {
|
|
109
|
+
recursive: true,
|
|
110
|
+
force: true
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const printOxlintOutput = ({ stdout, stderr })=>{
|
|
115
|
+
if (stdout) process.stdout.write(stdout);
|
|
116
|
+
if (stderr) process.stderr.write(stderr);
|
|
117
|
+
};
|
|
118
|
+
export { printOxlintOutput, runOxlintRules };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import node_fs from "node:fs";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { printOxlintOutput, runOxlintRules } from "./oxlint.js";
|
|
4
|
+
const WORKSPACE_SOURCE_SUCCESS = 'UltraModern i18n and boundary guardrails validated';
|
|
5
|
+
const ignoredDirectories = new Set([
|
|
6
|
+
'.modern',
|
|
7
|
+
'.modernjs',
|
|
8
|
+
'.output',
|
|
9
|
+
'dist',
|
|
10
|
+
'node_modules'
|
|
11
|
+
]);
|
|
12
|
+
const normalizePath = (filePath)=>filePath.replaceAll('\\', '/');
|
|
13
|
+
const relativePath = (root, filePath)=>normalizePath(node_path.relative(root, filePath));
|
|
14
|
+
const walk = (directory, files = [])=>{
|
|
15
|
+
if (!node_fs.existsSync(directory)) return files;
|
|
16
|
+
for (const entry of node_fs.readdirSync(directory, {
|
|
17
|
+
withFileTypes: true
|
|
18
|
+
})){
|
|
19
|
+
if (entry.isDirectory() && ignoredDirectories.has(entry.name)) continue;
|
|
20
|
+
const entryPath = node_path.join(directory, entry.name);
|
|
21
|
+
if (entry.isDirectory()) {
|
|
22
|
+
walk(entryPath, files);
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (entry.isFile()) files.push(entryPath);
|
|
26
|
+
}
|
|
27
|
+
return files;
|
|
28
|
+
};
|
|
29
|
+
const isSourceFile = (filePath)=>/\.(?:ts|tsx|js|jsx)$/u.test(filePath);
|
|
30
|
+
const isLocaleJson = (root, filePath)=>/\/locales\/(?:en|cs)\/[^/]+\.json$/u.test(`/${relativePath(root, filePath)}`);
|
|
31
|
+
const readText = (filePath)=>node_fs.readFileSync(filePath, 'utf-8');
|
|
32
|
+
const checkRuntimeResources = (root, filePath, text)=>{
|
|
33
|
+
const relative = relativePath(root, filePath);
|
|
34
|
+
if (!relative.endsWith('/src/modern.runtime.ts')) return;
|
|
35
|
+
const importsLocaleResources = /import\s+csResource\s+from\s+['"]\.\.\/locales\/cs\/[^'"]+\.json['"]/u.test(text) && /import\s+enResource\s+from\s+['"]\.\.\/locales\/en\/[^'"]+\.json['"]/u.test(text);
|
|
36
|
+
if (!importsLocaleResources || !/initOptions\s*:\s*\{[\s\S]*?\bresources\s*,/u.test(text)) throw new Error(`${relative} must register locale JSON resources in modern.runtime.ts so Worker SSR and hydration use the same first-render translations.`);
|
|
37
|
+
};
|
|
38
|
+
const visitLocaleKeys = (value, visitor, pathParts = [])=>{
|
|
39
|
+
if (!value || 'object' != typeof value || Array.isArray(value)) return;
|
|
40
|
+
for (const [key, child] of Object.entries(value)){
|
|
41
|
+
const nextPath = [
|
|
42
|
+
...pathParts,
|
|
43
|
+
key
|
|
44
|
+
];
|
|
45
|
+
visitor(key, child, nextPath);
|
|
46
|
+
visitLocaleKeys(child, visitor, nextPath);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const checkPluralResources = (root, filePath, json)=>{
|
|
50
|
+
const relative = relativePath(root, filePath);
|
|
51
|
+
const language = relative.split('/locales/')[1]?.split('/')[0];
|
|
52
|
+
const requiredSuffixes = 'cs' === language ? [
|
|
53
|
+
'one',
|
|
54
|
+
'few',
|
|
55
|
+
'many',
|
|
56
|
+
'other'
|
|
57
|
+
] : [
|
|
58
|
+
'one',
|
|
59
|
+
'other'
|
|
60
|
+
];
|
|
61
|
+
const groups = new Map();
|
|
62
|
+
visitLocaleKeys(json, (key, value, pathParts)=>{
|
|
63
|
+
if ('string' != typeof value || !value.includes('{{count}}')) return;
|
|
64
|
+
const suffixMatch = key.match(/^(.*)_(one|few|many|other)$/u);
|
|
65
|
+
if (!suffixMatch) throw new Error(`${relative} key ${pathParts.join('.')} contains {{count}} but is not plural-suffixed.`);
|
|
66
|
+
const [, base = '', suffix = ''] = suffixMatch;
|
|
67
|
+
const parentPath = pathParts.slice(0, -1).join('.');
|
|
68
|
+
const groupKey = `${parentPath}.${base}`;
|
|
69
|
+
const existing = groups.get(groupKey) ?? new Set();
|
|
70
|
+
existing.add(suffix);
|
|
71
|
+
groups.set(groupKey, existing);
|
|
72
|
+
});
|
|
73
|
+
for (const [group, suffixes] of groups)for (const suffix of requiredSuffixes)if (!suffixes.has(suffix)) throw new Error(`${relative} plural group ${group} is missing _${suffix}.`);
|
|
74
|
+
};
|
|
75
|
+
const runRuntimeAndLocaleResourceChecks = (root, sourceRoots)=>{
|
|
76
|
+
const files = sourceRoots.flatMap((sourceRoot)=>walk(node_path.join(root, sourceRoot)));
|
|
77
|
+
for (const filePath of files.filter(isSourceFile))checkRuntimeResources(root, filePath, readText(filePath));
|
|
78
|
+
for (const filePath of files.filter((filePath)=>isLocaleJson(root, filePath)))checkPluralResources(root, filePath, JSON.parse(readText(filePath)));
|
|
79
|
+
};
|
|
80
|
+
const runWorkspaceSourceCheck = ({ cwd = process.cwd(), sourceRoots = [
|
|
81
|
+
'apps',
|
|
82
|
+
'verticals'
|
|
83
|
+
] } = {})=>{
|
|
84
|
+
const oxlintResult = runOxlintRules({
|
|
85
|
+
cwd,
|
|
86
|
+
targets: sourceRoots,
|
|
87
|
+
rules: {
|
|
88
|
+
'ultramodern/no-legacy-mf-boundary-attributes': 'error',
|
|
89
|
+
'ultramodern/no-literal-visible-jsx-attributes': [
|
|
90
|
+
'error',
|
|
91
|
+
{
|
|
92
|
+
visibleAttributes: [
|
|
93
|
+
'aria-label',
|
|
94
|
+
"aria-description",
|
|
95
|
+
"aria-roledescription",
|
|
96
|
+
'aria-valuetext',
|
|
97
|
+
'alt',
|
|
98
|
+
'label',
|
|
99
|
+
'placeholder',
|
|
100
|
+
'title'
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
],
|
|
104
|
+
'ultramodern/no-manual-locale-copy-branching': 'error',
|
|
105
|
+
'ultramodern/no-split-translation-keys': 'error'
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
if (0 !== oxlintResult.exitCode) {
|
|
109
|
+
printOxlintOutput(oxlintResult);
|
|
110
|
+
return oxlintResult.exitCode;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
runRuntimeAndLocaleResourceChecks(cwd, sourceRoots);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
console.error(error instanceof Error ? error.message : 'UltraModern workspace source checks failed.');
|
|
116
|
+
return 1;
|
|
117
|
+
}
|
|
118
|
+
console.log(WORKSPACE_SOURCE_SUCCESS);
|
|
119
|
+
return 0;
|
|
120
|
+
};
|
|
121
|
+
const main = ()=>{
|
|
122
|
+
process.exitCode = runWorkspaceSourceCheck();
|
|
123
|
+
};
|
|
124
|
+
export { WORKSPACE_SOURCE_SUCCESS, main, runWorkspaceSourceCheck };
|