@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.
@@ -0,0 +1,317 @@
1
+ import "node:module";
2
+ const commonOptionsSchema = {
3
+ type: 'object',
4
+ properties: {
5
+ allowElements: {
6
+ type: 'array',
7
+ items: {
8
+ type: 'string'
9
+ }
10
+ },
11
+ ignoreCommentPattern: {
12
+ type: 'string'
13
+ }
14
+ },
15
+ additionalProperties: false
16
+ };
17
+ const attributeOptionsSchema = {
18
+ type: 'object',
19
+ properties: {
20
+ ...commonOptionsSchema.properties,
21
+ visibleAttributes: {
22
+ type: 'array',
23
+ items: {
24
+ type: 'string'
25
+ }
26
+ }
27
+ },
28
+ additionalProperties: false
29
+ };
30
+ const translationOptionsSchema = {
31
+ type: 'object',
32
+ properties: {
33
+ translationFunctions: {
34
+ type: 'array',
35
+ items: {
36
+ type: 'string'
37
+ }
38
+ }
39
+ },
40
+ additionalProperties: false
41
+ };
42
+ const DEFAULT_VISIBLE_ATTRIBUTES = [
43
+ 'aria-label',
44
+ "aria-description",
45
+ "aria-roledescription",
46
+ 'aria-valuetext',
47
+ 'alt',
48
+ 'placeholder',
49
+ 'title'
50
+ ];
51
+ const DEFAULT_ALLOWED_ELEMENTS = [
52
+ 'code',
53
+ 'kbd',
54
+ 'samp'
55
+ ];
56
+ const DEFAULT_TRANSLATION_FUNCTIONS = [
57
+ 't'
58
+ ];
59
+ const DEFAULT_IGNORE_COMMENT_PATTERN = 'i18n-ignore';
60
+ const LETTER_PATTERN = /\p{L}/u;
61
+ const SPLIT_TRANSLATION_KEY_PATTERN = /\.(?:prefix|suffix|before|after)$/u;
62
+ const isRecord = (value)=>Boolean(value) && 'object' == typeof value && !Array.isArray(value);
63
+ const asStringArray = (value, fallback)=>Array.isArray(value) && value.every((item)=>'string' == typeof item) ? value : fallback;
64
+ const getCommonRuleOption = (context, defaults)=>{
65
+ const options = context.options?.[0];
66
+ if (!isRecord(options)) return defaults;
67
+ return {
68
+ allowElements: asStringArray(options.allowElements, defaults.allowElements),
69
+ ignoreCommentPattern: 'string' == typeof options.ignoreCommentPattern ? options.ignoreCommentPattern : defaults.ignoreCommentPattern
70
+ };
71
+ };
72
+ const getAttributeRuleOption = (context, defaults)=>{
73
+ const options = context.options?.[0];
74
+ const commonOptions = getCommonRuleOption(context, defaults);
75
+ return {
76
+ ...commonOptions,
77
+ visibleAttributes: isRecord(options) ? asStringArray(options.visibleAttributes, defaults.visibleAttributes) : defaults.visibleAttributes
78
+ };
79
+ };
80
+ const getSplitTranslationKeyRuleOption = (context, defaults)=>{
81
+ const options = context.options?.[0];
82
+ if (!isRecord(options)) return defaults;
83
+ return {
84
+ translationFunctions: asStringArray(options.translationFunctions, defaults.translationFunctions)
85
+ };
86
+ };
87
+ const normalizeVisibleText = (value)=>value.replaceAll(/\s+/gu, ' ').trim();
88
+ const hasLetters = (value)=>LETTER_PATTERN.test(value);
89
+ const getNodeName = (node)=>{
90
+ if (!node) return;
91
+ if ('string' == typeof node.name) return node.name;
92
+ if ('JSXIdentifier' === node.type && isRecord(node.name) && 'string' == typeof node.name.name) return node.name.name;
93
+ if ('JSXIdentifier' === node.type && 'string' == typeof node.name) return node.name;
94
+ if (('JSXMemberExpression' === node.type || 'MemberExpression' === node.type) && isRecord(node.property) && true !== node.computed) {
95
+ const objectName = getNodeName(node.object);
96
+ const propertyName = getNodeName(node.property);
97
+ return objectName && propertyName ? `${objectName}.${propertyName}` : propertyName;
98
+ }
99
+ };
100
+ const getJsxElementName = (node)=>{
101
+ if (node?.type !== 'JSXElement') return;
102
+ return getNodeName(node.openingElement?.name);
103
+ };
104
+ const hasAllowedElementAncestor = (node, allowedElements)=>{
105
+ let current = node;
106
+ while(current){
107
+ const elementName = getJsxElementName(current);
108
+ if (elementName && allowedElements.has(elementName)) return true;
109
+ current = current.parent;
110
+ }
111
+ return false;
112
+ };
113
+ const getTemplateLiteralValue = (node)=>{
114
+ if ('TemplateLiteral' !== node.type || (node.expressions?.length ?? 0) > 0) return;
115
+ const quasi = node.quasis?.[0];
116
+ if (!isRecord(quasi?.value)) return;
117
+ const cooked = quasi.value.cooked;
118
+ const raw = quasi.value.raw;
119
+ return 'string' == typeof cooked ? cooked : 'string' == typeof raw ? raw : void 0;
120
+ };
121
+ const getStringLiteralValue = (node)=>{
122
+ if (!node) return;
123
+ if (('Literal' === node.type || 'StringLiteral' === node.type) && 'string' == typeof node.value) return node.value;
124
+ return getTemplateLiteralValue(node);
125
+ };
126
+ const expressionStringValue = (node)=>getStringLiteralValue(node?.type === 'JSXExpressionContainer' ? node.expression : node);
127
+ const getLine = (node)=>node.loc?.start?.line;
128
+ const hasIgnoreComment = (node, context, pattern)=>{
129
+ const sourceCode = context.getSourceCode?.();
130
+ const nodeLine = getLine(node);
131
+ if (!sourceCode?.getAllComments || void 0 === nodeLine) return false;
132
+ return sourceCode.getAllComments().some((comment)=>{
133
+ const commentValue = String(comment.value ?? '');
134
+ if (!pattern.test(commentValue)) return false;
135
+ const startLine = comment.loc?.start?.line;
136
+ const endLine = comment.loc?.end?.line ?? startLine;
137
+ return void 0 !== startLine && void 0 !== endLine && startLine <= nodeLine + 1 && endLine >= nodeLine - 1;
138
+ });
139
+ };
140
+ const getIgnorePattern = (options)=>new RegExp(options.ignoreCommentPattern, 'u');
141
+ const reportVisibleText = (context, node, text)=>{
142
+ context.report({
143
+ node,
144
+ message: `Move user-visible JSX text to locale resources: ${JSON.stringify(text)}`
145
+ });
146
+ };
147
+ const createNoHardcodedJsxTextRule = ()=>({
148
+ meta: {
149
+ type: 'problem',
150
+ docs: {
151
+ description: 'Disallow literal user-visible text in JSX children in UltraModern generated apps.'
152
+ },
153
+ schema: [
154
+ commonOptionsSchema
155
+ ]
156
+ },
157
+ create (context) {
158
+ const options = getCommonRuleOption(context, {
159
+ allowElements: DEFAULT_ALLOWED_ELEMENTS,
160
+ ignoreCommentPattern: DEFAULT_IGNORE_COMMENT_PATTERN
161
+ });
162
+ const allowedElements = new Set(options.allowElements);
163
+ const ignorePattern = getIgnorePattern(options);
164
+ const shouldSkipNode = (node)=>hasAllowedElementAncestor(node, allowedElements) || hasIgnoreComment(node, context, ignorePattern);
165
+ return {
166
+ JSXText (node) {
167
+ const text = normalizeVisibleText(String(node.value ?? ''));
168
+ if (!text || !hasLetters(text) || shouldSkipNode(node)) return;
169
+ reportVisibleText(context, node, text);
170
+ },
171
+ JSXExpressionContainer (node) {
172
+ if (node.parent?.type !== 'JSXElement' && node.parent?.type !== 'JSXFragment') return;
173
+ const text = normalizeVisibleText(expressionStringValue(node.expression) ?? '');
174
+ if (!text || !hasLetters(text) || shouldSkipNode(node)) return;
175
+ reportVisibleText(context, node, text);
176
+ }
177
+ };
178
+ }
179
+ });
180
+ const createNoLiteralVisibleJsxAttributesRule = ()=>({
181
+ meta: {
182
+ type: 'problem',
183
+ docs: {
184
+ description: 'Disallow literal user-visible JSX attribute text in UltraModern generated apps.'
185
+ },
186
+ schema: [
187
+ attributeOptionsSchema
188
+ ]
189
+ },
190
+ create (context) {
191
+ const options = getAttributeRuleOption(context, {
192
+ allowElements: DEFAULT_ALLOWED_ELEMENTS,
193
+ ignoreCommentPattern: DEFAULT_IGNORE_COMMENT_PATTERN,
194
+ visibleAttributes: DEFAULT_VISIBLE_ATTRIBUTES
195
+ });
196
+ const visibleAttributes = new Set(options.visibleAttributes);
197
+ const ignorePattern = getIgnorePattern(options);
198
+ return {
199
+ JSXAttribute (node) {
200
+ const attributeName = getNodeName(node.name);
201
+ if (!attributeName || !visibleAttributes.has(attributeName)) return;
202
+ const text = normalizeVisibleText(expressionStringValue(node.value) ?? '');
203
+ if (!text || !hasLetters(text) || hasIgnoreComment(node, context, ignorePattern)) return;
204
+ context.report({
205
+ node,
206
+ message: `Move literal ${attributeName} copy to locale resources: ${JSON.stringify(text)}`
207
+ });
208
+ }
209
+ };
210
+ }
211
+ });
212
+ const getSourceText = (context, node)=>context.getSourceCode?.().getText?.(node) ?? '';
213
+ const looksLikeLocaleTest = (context, node)=>{
214
+ const text = getSourceText(context, node);
215
+ 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);
216
+ };
217
+ const isAllowedBranchLiteral = (text)=>new Set([
218
+ 'page',
219
+ 'undefined',
220
+ 'null',
221
+ 'true',
222
+ 'false'
223
+ ]).has(text);
224
+ const createNoManualLocaleCopyBranchingRule = ()=>({
225
+ meta: {
226
+ type: 'problem',
227
+ docs: {
228
+ description: 'Disallow manual locale conditionals that choose user-visible copy.'
229
+ },
230
+ schema: []
231
+ },
232
+ create (context) {
233
+ const reportBranch = (node, text)=>{
234
+ context.report({
235
+ node,
236
+ message: `Move locale-specific copy branch to i18n resources: ${JSON.stringify(normalizeVisibleText(text))}`
237
+ });
238
+ };
239
+ return {
240
+ ConditionalExpression (node) {
241
+ if (!node.test || !looksLikeLocaleTest(context, node.test)) return;
242
+ for (const branch of [
243
+ node.consequent,
244
+ node.alternate
245
+ ]){
246
+ const text = expressionStringValue(branch);
247
+ if (text && hasLetters(text) && !isAllowedBranchLiteral(text.trim())) reportBranch(branch, text);
248
+ }
249
+ }
250
+ };
251
+ }
252
+ });
253
+ const getCallExpressionName = (node)=>getNodeName(node);
254
+ const createNoSplitTranslationKeysRule = ()=>({
255
+ meta: {
256
+ type: 'problem',
257
+ docs: {
258
+ description: 'Disallow split phrase translation key suffixes such as .prefix and .suffix.'
259
+ },
260
+ schema: [
261
+ translationOptionsSchema
262
+ ]
263
+ },
264
+ create (context) {
265
+ const options = getSplitTranslationKeyRuleOption(context, {
266
+ translationFunctions: DEFAULT_TRANSLATION_FUNCTIONS
267
+ });
268
+ const translationFunctions = new Set(options.translationFunctions);
269
+ return {
270
+ CallExpression (node) {
271
+ const calleeName = getCallExpressionName(node.callee);
272
+ if (!calleeName || !translationFunctions.has(calleeName)) return;
273
+ const key = expressionStringValue(node.arguments?.[0]);
274
+ if (!key || !SPLIT_TRANSLATION_KEY_PATTERN.test(key)) return;
275
+ context.report({
276
+ node,
277
+ message: 'Keep translator-owned phrases whole instead of using split translation keys.'
278
+ });
279
+ }
280
+ };
281
+ }
282
+ });
283
+ const createNoLegacyMfBoundaryAttributesRule = ()=>({
284
+ meta: {
285
+ type: 'problem',
286
+ docs: {
287
+ description: 'Disallow legacy Module Federation boundary attributes in generated UltraModern workspaces.'
288
+ },
289
+ schema: []
290
+ },
291
+ create (context) {
292
+ return {
293
+ JSXAttribute (node) {
294
+ const attributeName = getNodeName(node.name);
295
+ if ('data-mf-boundary' !== attributeName && 'data-mf-remote' !== attributeName && 'data-mf-expose' !== attributeName) return;
296
+ context.report({
297
+ node,
298
+ message: 'Use data-modern-boundary-id and data-modern-mf-expose instead of legacy data-mf-* boundary attributes.'
299
+ });
300
+ }
301
+ };
302
+ }
303
+ });
304
+ const oxlint_plugin_plugin = {
305
+ meta: {
306
+ name: 'ultramodern'
307
+ },
308
+ rules: {
309
+ 'no-hardcoded-jsx-text': createNoHardcodedJsxTextRule(),
310
+ 'no-legacy-mf-boundary-attributes': createNoLegacyMfBoundaryAttributesRule(),
311
+ 'no-literal-visible-jsx-attributes': createNoLiteralVisibleJsxAttributesRule(),
312
+ 'no-manual-locale-copy-branching': createNoManualLocaleCopyBranchingRule(),
313
+ 'no-split-translation-keys': createNoSplitTranslationKeysRule()
314
+ }
315
+ };
316
+ const oxlint_plugin = oxlint_plugin_plugin;
317
+ export default oxlint_plugin;
@@ -0,0 +1,9 @@
1
+ type SingleAppI18nCheckOptions = {
2
+ readonly cwd?: string;
3
+ readonly targets?: readonly string[];
4
+ };
5
+ export declare const SINGLE_APP_I18N_SUCCESS = "No hardcoded user-visible JSX strings found.";
6
+ export declare const SINGLE_APP_I18N_FAILURE = "Hardcoded user-visible JSX strings found. Move copy to locale JSON files.";
7
+ export declare const runSingleAppI18nCheck: ({ cwd, targets, }?: SingleAppI18nCheckOptions) => number;
8
+ export declare const main: () => void;
9
+ export {};
@@ -0,0 +1,22 @@
1
+ type OxlintRuleConfig = string | readonly [
2
+ string,
3
+ {
4
+ readonly [key: string]: unknown;
5
+ }
6
+ ];
7
+ type OxlintRules = {
8
+ readonly [ruleName: string]: OxlintRuleConfig;
9
+ };
10
+ type OxlintRulesOptions = {
11
+ readonly cwd: string;
12
+ readonly targets: readonly string[];
13
+ readonly rules: OxlintRules;
14
+ };
15
+ type OxlintRulesResult = {
16
+ readonly exitCode: number;
17
+ readonly stdout: string;
18
+ readonly stderr: string;
19
+ };
20
+ export declare const runOxlintRules: ({ cwd, targets, rules, }: OxlintRulesOptions) => OxlintRulesResult;
21
+ export declare const printOxlintOutput: ({ stdout, stderr, }: OxlintRulesResult) => void;
22
+ export {};
@@ -0,0 +1,8 @@
1
+ type WorkspaceSourceCheckOptions = {
2
+ readonly cwd?: string;
3
+ readonly sourceRoots?: readonly string[];
4
+ };
5
+ export declare const WORKSPACE_SOURCE_SUCCESS = "UltraModern i18n and boundary guardrails validated";
6
+ export declare const runWorkspaceSourceCheck: ({ cwd, sourceRoots, }?: WorkspaceSourceCheckOptions) => number;
7
+ export declare const main: () => void;
8
+ export {};
@@ -0,0 +1,3 @@
1
+ export { runSingleAppI18nCheck } from './cli/i18n-check';
2
+ export { runWorkspaceSourceCheck } from './cli/workspace-source-check';
3
+ export { default as oxlintPlugin } from './oxlint-plugin';
@@ -0,0 +1,63 @@
1
+ type AstNode = {
2
+ readonly type?: string;
3
+ readonly parent?: AstNode;
4
+ readonly loc?: {
5
+ readonly start?: {
6
+ readonly line?: number;
7
+ };
8
+ readonly end?: {
9
+ readonly line?: number;
10
+ };
11
+ };
12
+ readonly value?: unknown;
13
+ readonly raw?: unknown;
14
+ readonly name?: unknown;
15
+ readonly openingElement?: AstNode;
16
+ readonly expression?: AstNode;
17
+ readonly expressions?: readonly AstNode[];
18
+ readonly quasis?: readonly AstNode[];
19
+ readonly test?: AstNode;
20
+ readonly consequent?: AstNode;
21
+ readonly alternate?: AstNode;
22
+ readonly arguments?: readonly AstNode[];
23
+ readonly callee?: AstNode;
24
+ readonly object?: AstNode;
25
+ readonly property?: AstNode;
26
+ readonly computed?: boolean;
27
+ };
28
+ type RuleContext = {
29
+ readonly options?: readonly unknown[];
30
+ readonly filename?: string;
31
+ getSourceCode?: () => {
32
+ readonly text?: string;
33
+ getAllComments?: () => readonly AstNode[];
34
+ getText?: (node: AstNode) => string;
35
+ };
36
+ report: (descriptor: {
37
+ readonly node: AstNode;
38
+ readonly message: string;
39
+ }) => void;
40
+ };
41
+ type Rule = {
42
+ readonly meta: {
43
+ readonly type: string;
44
+ readonly docs: {
45
+ readonly description: string;
46
+ };
47
+ readonly schema: unknown[];
48
+ };
49
+ create(context: RuleContext): Record<string, (node: AstNode) => void>;
50
+ };
51
+ declare const plugin: {
52
+ meta: {
53
+ name: string;
54
+ };
55
+ rules: {
56
+ 'no-hardcoded-jsx-text': Rule;
57
+ 'no-legacy-mf-boundary-attributes': Rule;
58
+ 'no-literal-visible-jsx-attributes': Rule;
59
+ 'no-manual-locale-copy-branching': Rule;
60
+ 'no-split-translation-keys': Rule;
61
+ };
62
+ };
63
+ export default plugin;
package/package.json ADDED
@@ -0,0 +1,80 @@
1
+ {
2
+ "name": "@bleedingdev/modern-js-code-tools",
3
+ "description": "Code analysis tooling for Modern.js projects.",
4
+ "homepage": "https://github.com/BleedingDev/ultramodern.js#readme",
5
+ "bugs": {
6
+ "url": "https://github.com/BleedingDev/ultramodern.js/issues"
7
+ },
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/BleedingDev/ultramodern.js.git",
11
+ "directory": "packages/toolkit/code-tools"
12
+ },
13
+ "license": "MIT",
14
+ "keywords": [
15
+ "react",
16
+ "framework",
17
+ "modern",
18
+ "modern.js",
19
+ "lint",
20
+ "oxlint"
21
+ ],
22
+ "type": "module",
23
+ "engines": {
24
+ "node": ">=20"
25
+ },
26
+ "version": "3.2.0-ultramodern.111",
27
+ "types": "./dist/types/index.d.ts",
28
+ "main": "./dist/esm-node/index.js",
29
+ "typesVersions": {
30
+ "*": {
31
+ ".": [
32
+ "./dist/types/index.d.ts"
33
+ ],
34
+ "oxlint-plugin": [
35
+ "./dist/types/oxlint-plugin.d.ts"
36
+ ]
37
+ }
38
+ },
39
+ "exports": {
40
+ ".": {
41
+ "types": "./dist/types/index.d.ts",
42
+ "node": {
43
+ "import": "./dist/esm-node/index.js",
44
+ "require": "./dist/cjs/index.cjs"
45
+ },
46
+ "default": "./dist/esm-node/index.js"
47
+ },
48
+ "./oxlint-plugin": {
49
+ "types": "./dist/types/oxlint-plugin.d.ts",
50
+ "node": {
51
+ "import": "./dist/esm-node/oxlint-plugin.js",
52
+ "require": "./dist/cjs/oxlint-plugin.cjs"
53
+ },
54
+ "default": "./dist/esm-node/oxlint-plugin.js"
55
+ }
56
+ },
57
+ "files": [
58
+ "dist"
59
+ ],
60
+ "dependencies": {
61
+ "oxlint": "1.68.0"
62
+ },
63
+ "devDependencies": {
64
+ "@rslib/core": "0.21.5",
65
+ "@types/node": "^25.9.1",
66
+ "@typescript/native-preview": "7.0.0-dev.20260606.1",
67
+ "@scripts/rstest-config": "2.66.0"
68
+ },
69
+ "sideEffects": false,
70
+ "publishConfig": {
71
+ "registry": "https://registry.npmjs.org/",
72
+ "access": "public"
73
+ },
74
+ "modern:source": "./src/index.ts",
75
+ "scripts": {
76
+ "build": "rm -rf dist && rslib build -c rslib.config.mts && pnpm -w tsgo:dts \"$PWD\"",
77
+ "dev": "rslib build -c rslib.config.mts -w",
78
+ "test": "rstest --passWithNoTests"
79
+ }
80
+ }