@octanejs/codemod 0.1.3 → 0.2.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/README.md +305 -5
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +162 -0
- package/dist/compiler.d.ts +11 -0
- package/dist/compiler.js +48 -0
- package/dist/effect-receivers.d.ts +11 -0
- package/dist/effect-receivers.js +315 -0
- package/dist/expression-wrappers.d.ts +6 -0
- package/dist/expression-wrappers.js +16 -0
- package/dist/forwarded-props.d.ts +27 -0
- package/dist/forwarded-props.js +489 -0
- package/dist/index.d.ts +6 -0
- package/{src → dist}/index.js +1 -0
- package/dist/linked-state.d.ts +17 -0
- package/dist/linked-state.js +126 -0
- package/dist/local-calls.d.ts +20 -0
- package/dist/local-calls.js +143 -0
- package/dist/local-react-hook.d.ts +15 -0
- package/dist/local-react-hook.js +353 -0
- package/dist/local-uses.d.ts +13 -0
- package/dist/local-uses.js +987 -0
- package/dist/mappings.d.ts +57 -0
- package/dist/mappings.js +343 -0
- package/dist/path-svg.d.ts +60 -0
- package/dist/path-svg.js +207 -0
- package/dist/project.d.ts +9 -0
- package/dist/project.js +1568 -0
- package/dist/react-intl.d.ts +4 -0
- package/dist/react-intl.js +389 -0
- package/dist/react-renderer.d.ts +9 -0
- package/dist/react-renderer.js +106 -0
- package/dist/scoped-signal.d.ts +4 -0
- package/dist/scoped-signal.js +480 -0
- package/dist/scoped-store-contract.d.ts +13 -0
- package/dist/scoped-store-contract.js +741 -0
- package/dist/store-hook-uses.d.ts +18 -0
- package/dist/store-hook-uses.js +403 -0
- package/dist/store-inputs.d.ts +16 -0
- package/dist/store-inputs.js +391 -0
- package/dist/strong.d.ts +24 -0
- package/dist/strong.js +142 -0
- package/dist/transform.d.ts +27 -0
- package/dist/transform.js +4890 -0
- package/dist/types.d.ts +203 -0
- package/dist/types.js +1 -0
- package/package.json +19 -6
- package/src/path-svg.ts +218 -0
- package/templates/octane-path-svg.ts +56 -0
- package/templates/octane-store-context.tsrx +49 -0
- package/templates/octane-store-hooks.ts +61 -0
- package/templates/react-intl.tsrx +116 -0
- package/src/bin.js +0 -151
- package/src/compiler.js +0 -61
- package/src/index.d.ts +0 -95
- package/src/mappings.js +0 -429
- package/src/project.js +0 -670
- package/src/strong.js +0 -149
- package/src/transform.js +0 -3425
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { TransformOptions } from './types.js';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
/** Select whole named imports that the shared adapter can replace without changing renderer-owned values. */
|
|
4
|
+
export declare function intlImports(file: ts.SourceFile, importedText: TransformOptions['forwardedText'], checker: ts.TypeChecker, typeOf: (expression: ts.Expression) => ts.Type, isComponent: (functionNode: ts.FunctionDeclaration | ts.ArrowFunction | ts.FunctionExpression) => boolean, inReact: (element: ts.JsxElement | ts.JsxSelfClosingElement) => boolean, canPassToReact: (element: ts.JsxElement | ts.JsxSelfClosingElement) => boolean, returnedReceiver: (tag: ts.JsxOpeningLikeElement) => boolean, originalReact: () => boolean): Set<ts.ImportSpecifier>;
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { closedForwardedAttribute, forwardedProps, hasEval, hasPrototypeAccess, renderUses, } from './forwarded-props.js';
|
|
2
|
+
import { localUses, portableArguments, scalar } from './local-uses.js';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
const scalarMethods = new Set([
|
|
5
|
+
'formatDate',
|
|
6
|
+
'formatTime',
|
|
7
|
+
'formatDateTimeRange',
|
|
8
|
+
'formatRelativeTime',
|
|
9
|
+
'formatNumber',
|
|
10
|
+
'formatPlural',
|
|
11
|
+
'formatDisplayName',
|
|
12
|
+
]);
|
|
13
|
+
const descriptor = new Set(['id', 'description', 'defaultMessage', 'ignoreTag']);
|
|
14
|
+
/** Select whole named imports that the shared adapter can replace without changing renderer-owned values. */
|
|
15
|
+
export function intlImports(file, importedText, checker, typeOf, isComponent, inReact, canPassToReact, returnedReceiver, originalReact) {
|
|
16
|
+
const candidates = file.statements.filter(ts.isImportDeclaration).flatMap((decl) => {
|
|
17
|
+
const named = decl.importClause?.namedBindings;
|
|
18
|
+
if (!ts.isStringLiteral(decl.moduleSpecifier) ||
|
|
19
|
+
decl.moduleSpecifier.text !== 'react-intl' ||
|
|
20
|
+
decl.attributes ||
|
|
21
|
+
decl.importClause?.isTypeOnly ||
|
|
22
|
+
!named ||
|
|
23
|
+
!ts.isNamedImports(named) ||
|
|
24
|
+
/\/\*|\/\//.test(decl.getText(file)))
|
|
25
|
+
return [];
|
|
26
|
+
return named.elements.filter((item) => !item.isTypeOnly &&
|
|
27
|
+
['useIntl', 'IntlProvider', 'RawIntlProvider', 'FormattedMessage'].includes((item.propertyName ?? item.name).text));
|
|
28
|
+
});
|
|
29
|
+
const selected = new Set();
|
|
30
|
+
if (!candidates.length)
|
|
31
|
+
return selected;
|
|
32
|
+
const uses = localUses(file, checker);
|
|
33
|
+
const ordinaryCaller = !hasPrototypeAccess(file);
|
|
34
|
+
const forwards = forwardedProps(file, checker, 'text', uses, ordinaryCaller);
|
|
35
|
+
// Even without substitution values a provider can format rich text into React elements.
|
|
36
|
+
// Follow const aliases only when every consumer explicitly coerces or compares the result.
|
|
37
|
+
const localSinks = new Map();
|
|
38
|
+
const forwardingSinks = new Map();
|
|
39
|
+
function textSink(value, allowForwarding) {
|
|
40
|
+
const knownSinks = allowForwarding ? forwardingSinks : localSinks;
|
|
41
|
+
const cached = knownSinks.get(value);
|
|
42
|
+
if (cached !== undefined)
|
|
43
|
+
return cached;
|
|
44
|
+
knownSinks.set(value, false); // A cycle cannot establish safe use.
|
|
45
|
+
const safe = checkSink(value, allowForwarding);
|
|
46
|
+
knownSinks.set(value, safe);
|
|
47
|
+
return safe;
|
|
48
|
+
}
|
|
49
|
+
function checkSink(value, allowForwarding) {
|
|
50
|
+
const use = value.parent;
|
|
51
|
+
if (ts.isParenthesizedExpression(use) ||
|
|
52
|
+
ts.isAsExpression(use) ||
|
|
53
|
+
ts.isNonNullExpression(use) ||
|
|
54
|
+
ts.isSatisfiesExpression(use))
|
|
55
|
+
return textSink(use, allowForwarding);
|
|
56
|
+
if ((ts.isTemplateSpan(use) && !ts.isTaggedTemplateExpression(use.parent.parent)) ||
|
|
57
|
+
ts.isTypeOfExpression(use) ||
|
|
58
|
+
ts.isVoidExpression(use) ||
|
|
59
|
+
(ts.isPrefixUnaryExpression(use) && use.operator === ts.SyntaxKind.ExclamationToken))
|
|
60
|
+
return true;
|
|
61
|
+
if (ts.isCallExpression(use) &&
|
|
62
|
+
use.arguments.length === 1 &&
|
|
63
|
+
use.arguments[0] === value &&
|
|
64
|
+
ts.isIdentifier(use.expression) &&
|
|
65
|
+
use.expression.text === 'String' &&
|
|
66
|
+
checker
|
|
67
|
+
.getSymbolAtLocation(use.expression)
|
|
68
|
+
?.declarations?.every((d) => d.getSourceFile().hasNoDefaultLib))
|
|
69
|
+
return true;
|
|
70
|
+
if (ts.isConditionalExpression(use))
|
|
71
|
+
return use.condition === value || textSink(use, allowForwarding);
|
|
72
|
+
if (ts.isBinaryExpression(use)) {
|
|
73
|
+
const op = use.operatorToken.kind;
|
|
74
|
+
if (op === ts.SyntaxKind.AmpersandAmpersandToken ||
|
|
75
|
+
op === ts.SyntaxKind.BarBarToken ||
|
|
76
|
+
op === ts.SyntaxKind.QuestionQuestionToken)
|
|
77
|
+
return textSink(use, allowForwarding);
|
|
78
|
+
return ([
|
|
79
|
+
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
|
80
|
+
ts.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
81
|
+
ts.SyntaxKind.EqualsEqualsToken,
|
|
82
|
+
ts.SyntaxKind.ExclamationEqualsToken,
|
|
83
|
+
].includes(op) ||
|
|
84
|
+
(op === ts.SyntaxKind.PlusToken &&
|
|
85
|
+
(ts.isStringLiteralLike(use.left) || ts.isStringLiteralLike(use.right))));
|
|
86
|
+
}
|
|
87
|
+
if (ts.isVariableDeclaration(use) &&
|
|
88
|
+
use.initializer === value &&
|
|
89
|
+
ts.isIdentifier(use.name) &&
|
|
90
|
+
ts.getCombinedNodeFlags(use) & ts.NodeFlags.Const)
|
|
91
|
+
return uses(use.name).every((ref) => textSink(ref, allowForwarding));
|
|
92
|
+
if (ts.isJsxExpression(use) && ts.isJsxAttribute(use.parent)) {
|
|
93
|
+
const attr = use.parent;
|
|
94
|
+
const tag = attr.parent.parent;
|
|
95
|
+
if (!ts.isIdentifier(attr.name) || !ts.isIdentifier(tag.tagName))
|
|
96
|
+
return false;
|
|
97
|
+
const prop = attr.name.text;
|
|
98
|
+
if (/^[a-z][a-z0-9]*$/.test(tag.tagName.text))
|
|
99
|
+
return (prop === 'title' ||
|
|
100
|
+
prop.startsWith('aria-') ||
|
|
101
|
+
(prop === 'placeholder' && ['input', 'textarea'].includes(tag.tagName.text)) ||
|
|
102
|
+
(prop === 'alt' && ['img', 'input', 'area'].includes(tag.tagName.text)) ||
|
|
103
|
+
(prop === 'label' && ['option', 'optgroup', 'track'].includes(tag.tagName.text)) ||
|
|
104
|
+
prop.startsWith('data-'));
|
|
105
|
+
if (!allowForwarding ||
|
|
106
|
+
!/^[A-Z]/.test(tag.tagName.text) ||
|
|
107
|
+
tag.attributes.properties.some((a) => ts.isJsxSpreadAttribute(a) || (ts.isJsxAttribute(a) && a.name.getText(file) === 'is')))
|
|
108
|
+
return false;
|
|
109
|
+
const symbol = checker.getSymbolAtLocation(tag.tagName);
|
|
110
|
+
const decl = symbol?.declarations?.[0];
|
|
111
|
+
if (decl && ts.isImportSpecifier(decl)) {
|
|
112
|
+
const module = decl.parent.parent.parent.moduleSpecifier;
|
|
113
|
+
if (!ts.isStringLiteral(module) || !importedText)
|
|
114
|
+
return false;
|
|
115
|
+
const name = (decl.propertyName ?? decl.name).text;
|
|
116
|
+
if (importedText(module.text, name, prop, ordinaryCaller))
|
|
117
|
+
return true;
|
|
118
|
+
return (!decl.isTypeOnly &&
|
|
119
|
+
!decl.parent.parent.isTypeOnly &&
|
|
120
|
+
!decl.parent.parent.parent.attributes &&
|
|
121
|
+
ordinaryCaller &&
|
|
122
|
+
!/@jsx(?:ImportSource|Runtime|Frag)?\b/i.test(file.text) &&
|
|
123
|
+
closedForwardedAttribute(attr) &&
|
|
124
|
+
returnedReceiver(tag) &&
|
|
125
|
+
importedText(module.text, name, prop, true, true));
|
|
126
|
+
}
|
|
127
|
+
return forwards(symbol, prop);
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
function closedChild(child) {
|
|
132
|
+
if (ts.isJsxText(child))
|
|
133
|
+
return true;
|
|
134
|
+
if (ts.isJsxExpression(child))
|
|
135
|
+
return !child.expression || scalar(typeOf(child.expression));
|
|
136
|
+
if (ts.isJsxFragment(child))
|
|
137
|
+
return child.children.every(closedChild);
|
|
138
|
+
if (!ts.isJsxElement(child) && !ts.isJsxSelfClosingElement(child))
|
|
139
|
+
return false;
|
|
140
|
+
const opening = ts.isJsxElement(child) ? child.openingElement : child;
|
|
141
|
+
if (!ts.isIdentifier(opening.tagName) || !/^[a-z]/.test(opening.tagName.text))
|
|
142
|
+
return false;
|
|
143
|
+
return (opening.attributes.properties.every((p) => ts.isJsxAttribute(p) && p.name.getText(file) !== 'children') &&
|
|
144
|
+
(!ts.isJsxElement(child) || child.children.every(closedChild)));
|
|
145
|
+
}
|
|
146
|
+
// A substitution object is shared with the installed React formatter. Check
|
|
147
|
+
// its data properties before any JSX in this module changes renderer.
|
|
148
|
+
const portable = portableArguments(checker, typeOf);
|
|
149
|
+
function scalarValues(values) {
|
|
150
|
+
return (ts.isObjectLiteralExpression(values) &&
|
|
151
|
+
values.properties.every((property) => {
|
|
152
|
+
const value = ts.isShorthandPropertyAssignment(property)
|
|
153
|
+
? property.name
|
|
154
|
+
: ts.isPropertyAssignment(property) && !ts.isComputedPropertyName(property.name)
|
|
155
|
+
? property.initializer
|
|
156
|
+
: undefined;
|
|
157
|
+
if (!value || !portable(value))
|
|
158
|
+
return false;
|
|
159
|
+
let source = value;
|
|
160
|
+
while (ts.isAsExpression(source) ||
|
|
161
|
+
ts.isNonNullExpression(source) ||
|
|
162
|
+
ts.isParenthesizedExpression(source) ||
|
|
163
|
+
ts.isSatisfiesExpression(source))
|
|
164
|
+
source = source.expression;
|
|
165
|
+
return scalar(typeOf(source));
|
|
166
|
+
}));
|
|
167
|
+
}
|
|
168
|
+
function callsScalarMethod(receiver) {
|
|
169
|
+
const access = receiver.parent;
|
|
170
|
+
if (!ts.isPropertyAccessExpression(access) ||
|
|
171
|
+
access.expression !== receiver ||
|
|
172
|
+
access.questionDotToken)
|
|
173
|
+
return false;
|
|
174
|
+
const call = access.parent;
|
|
175
|
+
if (!ts.isCallExpression(call) || call.expression !== access)
|
|
176
|
+
return false;
|
|
177
|
+
if (scalarMethods.has(access.name.text))
|
|
178
|
+
return true;
|
|
179
|
+
if (access.name.text !== 'formatMessage' ||
|
|
180
|
+
call.arguments.length < 1 ||
|
|
181
|
+
call.arguments.length > 3 ||
|
|
182
|
+
call.arguments.some(ts.isSpreadElement) ||
|
|
183
|
+
!textSink(call, call.arguments.length <= 2 &&
|
|
184
|
+
ts.isObjectLiteralExpression(call.arguments[0]) &&
|
|
185
|
+
call.arguments[0].properties.every((field) => ts.isPropertyAssignment(field) &&
|
|
186
|
+
(ts.isIdentifier(field.name) || ts.isStringLiteral(field.name)) &&
|
|
187
|
+
descriptor.has(field.name.text) &&
|
|
188
|
+
ts.isStringLiteralLike(field.initializer))))
|
|
189
|
+
return false;
|
|
190
|
+
const values = call.arguments[1];
|
|
191
|
+
return !values || scalarValues(values);
|
|
192
|
+
}
|
|
193
|
+
function localeView(owner, decl, name) {
|
|
194
|
+
const body = owner.body?.statements;
|
|
195
|
+
const [bind, output] = body ?? [];
|
|
196
|
+
if (owner.parameters.length ||
|
|
197
|
+
body?.length !== 2 ||
|
|
198
|
+
!ts.isVariableStatement(bind) ||
|
|
199
|
+
bind.declarationList !== decl.parent ||
|
|
200
|
+
bind.declarationList.declarations.length !== 1 ||
|
|
201
|
+
!ts.isReturnStatement(output) ||
|
|
202
|
+
!output.expression)
|
|
203
|
+
return false;
|
|
204
|
+
const view = output.expression;
|
|
205
|
+
const tag = ts.isJsxElement(view) ? view.openingElement : view;
|
|
206
|
+
if ((!ts.isJsxOpeningElement(tag) && !ts.isJsxSelfClosingElement(tag)) ||
|
|
207
|
+
!ts.isIdentifier(tag.tagName) ||
|
|
208
|
+
!/^(?:div|span|p|b|strong|em|small|label|output|h[1-6])$/.test(tag.tagName.text))
|
|
209
|
+
return false;
|
|
210
|
+
const readsLocale = (node) => !node.dotDotDotToken &&
|
|
211
|
+
!!node.expression &&
|
|
212
|
+
ts.isIdentifier(node.expression) &&
|
|
213
|
+
node.expression.text === name.text;
|
|
214
|
+
const [attr, extra] = tag.attributes.properties;
|
|
215
|
+
if (extra ||
|
|
216
|
+
(attr &&
|
|
217
|
+
(!ts.isJsxAttribute(attr) ||
|
|
218
|
+
!ts.isIdentifier(attr.name) ||
|
|
219
|
+
attr.name.text !== 'lang' ||
|
|
220
|
+
!attr.initializer ||
|
|
221
|
+
!ts.isJsxExpression(attr.initializer) ||
|
|
222
|
+
!readsLocale(attr.initializer))))
|
|
223
|
+
return false;
|
|
224
|
+
const children = ts.isJsxElement(view)
|
|
225
|
+
? view.children.filter((child) => !ts.isJsxText(child) || child.text.trim())
|
|
226
|
+
: [];
|
|
227
|
+
const [child, sibling] = children;
|
|
228
|
+
return !sibling && (child ? ts.isJsxExpression(child) && readsLocale(child) : !!attr);
|
|
229
|
+
}
|
|
230
|
+
// Only a private component with a closed JSX use can prove its nearest provider.
|
|
231
|
+
function localeProvider(owner) {
|
|
232
|
+
if (!ordinaryCaller ||
|
|
233
|
+
/@jsx(?:ImportSource|Runtime|Frag)?\b/i.test(file.text) ||
|
|
234
|
+
owner.parent !== file ||
|
|
235
|
+
owner.asteriskToken ||
|
|
236
|
+
owner.modifiers ||
|
|
237
|
+
!owner.name)
|
|
238
|
+
return;
|
|
239
|
+
const [ref, extra] = renderUses(uses(owner.name));
|
|
240
|
+
const leaf = ref?.parent;
|
|
241
|
+
if (extra ||
|
|
242
|
+
!leaf ||
|
|
243
|
+
!ts.isJsxSelfClosingElement(leaf) ||
|
|
244
|
+
leaf.tagName !== ref ||
|
|
245
|
+
leaf.typeArguments ||
|
|
246
|
+
leaf.attributes.properties.length)
|
|
247
|
+
return;
|
|
248
|
+
const tree = leaf.parent;
|
|
249
|
+
if (!ts.isJsxElement(tree) ||
|
|
250
|
+
inReact(tree) ||
|
|
251
|
+
!tree.children.every((child) => child === leaf || (ts.isJsxText(child) && !child.text.trim())))
|
|
252
|
+
return;
|
|
253
|
+
const { tagName, attributes } = tree.openingElement;
|
|
254
|
+
const closing = tree.closingElement.tagName;
|
|
255
|
+
const [attr, other] = attributes.properties;
|
|
256
|
+
if (!ts.isIdentifier(tagName) ||
|
|
257
|
+
!ts.isIdentifier(closing) ||
|
|
258
|
+
other ||
|
|
259
|
+
!attr ||
|
|
260
|
+
!ts.isJsxAttribute(attr) ||
|
|
261
|
+
!ts.isIdentifier(attr.name) ||
|
|
262
|
+
attr.name.text !== 'locale' ||
|
|
263
|
+
!attr.initializer ||
|
|
264
|
+
!ts.isStringLiteral(attr.initializer) ||
|
|
265
|
+
/[&\r\n]/.test(attr.initializer.text))
|
|
266
|
+
return;
|
|
267
|
+
const provider = candidates.find((item) => {
|
|
268
|
+
if ((item.propertyName ?? item.name).text !== 'IntlProvider')
|
|
269
|
+
return false;
|
|
270
|
+
const refs = renderUses(uses(item.name));
|
|
271
|
+
return refs.length === 2 && refs.includes(tagName) && refs.includes(closing);
|
|
272
|
+
});
|
|
273
|
+
if (!provider || !originalReact())
|
|
274
|
+
return;
|
|
275
|
+
let output = tree;
|
|
276
|
+
while (ts.isParenthesizedExpression(output.parent))
|
|
277
|
+
output = output.parent;
|
|
278
|
+
if (!ts.isReturnStatement(output.parent))
|
|
279
|
+
return;
|
|
280
|
+
const root = ts.findAncestor(output.parent, ts.isFunctionLike);
|
|
281
|
+
if (root &&
|
|
282
|
+
(ts.isFunctionDeclaration(root) ||
|
|
283
|
+
ts.isFunctionExpression(root) ||
|
|
284
|
+
ts.isArrowFunction(root)) &&
|
|
285
|
+
!root.asteriskToken &&
|
|
286
|
+
!root.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) &&
|
|
287
|
+
isComponent(root))
|
|
288
|
+
return provider;
|
|
289
|
+
}
|
|
290
|
+
function readsIntl(id, allowLocale = false) {
|
|
291
|
+
const call = id.parent;
|
|
292
|
+
if (!ts.isCallExpression(call) || call.expression !== id || call.arguments.length)
|
|
293
|
+
return false;
|
|
294
|
+
const owner = ts.findAncestor(call, ts.isFunctionLike);
|
|
295
|
+
if (!owner ||
|
|
296
|
+
!(ts.isFunctionDeclaration(owner) ||
|
|
297
|
+
ts.isFunctionExpression(owner) ||
|
|
298
|
+
ts.isArrowFunction(owner)) ||
|
|
299
|
+
!isComponent(owner))
|
|
300
|
+
return false;
|
|
301
|
+
if (callsScalarMethod(call))
|
|
302
|
+
return true;
|
|
303
|
+
const access = call.parent;
|
|
304
|
+
const directLocale = ts.isPropertyAccessExpression(access) &&
|
|
305
|
+
access.expression === call &&
|
|
306
|
+
!access.questionDotToken &&
|
|
307
|
+
access.name.text === 'locale';
|
|
308
|
+
const initializer = directLocale ? access : call;
|
|
309
|
+
const decl = initializer.parent;
|
|
310
|
+
if (!ts.isVariableDeclaration(decl) ||
|
|
311
|
+
decl.initializer !== initializer ||
|
|
312
|
+
!(ts.getCombinedNodeFlags(decl) & ts.NodeFlags.Const))
|
|
313
|
+
return false;
|
|
314
|
+
let name;
|
|
315
|
+
if (ts.isIdentifier(decl.name)) {
|
|
316
|
+
if (!directLocale)
|
|
317
|
+
return uses(decl.name).every(callsScalarMethod);
|
|
318
|
+
name = decl.name;
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
if (directLocale || !ts.isObjectBindingPattern(decl.name) || decl.name.elements.length !== 1)
|
|
322
|
+
return false;
|
|
323
|
+
const [binding] = decl.name.elements;
|
|
324
|
+
const key = binding.propertyName ?? binding.name;
|
|
325
|
+
if (binding.dotDotDotToken ||
|
|
326
|
+
binding.initializer ||
|
|
327
|
+
!ts.isIdentifier(binding.name) ||
|
|
328
|
+
(!ts.isIdentifier(key) && !ts.isStringLiteral(key)) ||
|
|
329
|
+
key.text !== 'locale')
|
|
330
|
+
return false;
|
|
331
|
+
name = binding.name;
|
|
332
|
+
}
|
|
333
|
+
// A custom provider can perform React DOM resource work in its locale getter.
|
|
334
|
+
return (allowLocale &&
|
|
335
|
+
ts.isFunctionDeclaration(owner) &&
|
|
336
|
+
localeView(owner, decl, name) &&
|
|
337
|
+
(localeProvider(owner) ?? false));
|
|
338
|
+
}
|
|
339
|
+
for (const item of candidates) {
|
|
340
|
+
const name = (item.propertyName ?? item.name).text;
|
|
341
|
+
const locations = uses(item.name);
|
|
342
|
+
if (!locations.length)
|
|
343
|
+
continue;
|
|
344
|
+
if (name === 'useIntl') {
|
|
345
|
+
// Direct eval can read the imported hook or its formatter through an untracked name.
|
|
346
|
+
if (hasEval(file))
|
|
347
|
+
continue;
|
|
348
|
+
const proof = locations.length === 1
|
|
349
|
+
? readsIntl(locations[0], true)
|
|
350
|
+
: locations.every((id) => readsIntl(id));
|
|
351
|
+
if (proof) {
|
|
352
|
+
selected.add(item);
|
|
353
|
+
if (proof !== true)
|
|
354
|
+
selected.add(proof);
|
|
355
|
+
}
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
const supported = locations.every((id) => {
|
|
359
|
+
const tag = id.parent;
|
|
360
|
+
if (ts.isJsxClosingElement(tag))
|
|
361
|
+
return tag.tagName === id && name !== 'FormattedMessage';
|
|
362
|
+
if ((!ts.isJsxSelfClosingElement(tag) && !ts.isJsxOpeningElement(tag)) || tag.tagName !== id)
|
|
363
|
+
return false;
|
|
364
|
+
if (inReact(ts.isJsxOpeningElement(tag) ? tag.parent : tag))
|
|
365
|
+
return false;
|
|
366
|
+
if (name === 'FormattedMessage' && !ts.isJsxSelfClosingElement(tag))
|
|
367
|
+
return false;
|
|
368
|
+
if (name !== 'FormattedMessage' &&
|
|
369
|
+
ts.isJsxOpeningElement(tag) &&
|
|
370
|
+
!(ts.isJsxElement(tag.parent) && tag.parent.children.every(closedChild)))
|
|
371
|
+
return false;
|
|
372
|
+
return tag.attributes.properties.every((attr) => ts.isJsxAttribute(attr) &&
|
|
373
|
+
ts.isIdentifier(attr.name) &&
|
|
374
|
+
(name === 'FormattedMessage'
|
|
375
|
+
? descriptor.has(attr.name.text) ||
|
|
376
|
+
(attr.name.text === 'values' &&
|
|
377
|
+
!!attr.initializer &&
|
|
378
|
+
ts.isJsxExpression(attr.initializer) &&
|
|
379
|
+
!!attr.initializer.expression &&
|
|
380
|
+
scalarValues(attr.initializer.expression) &&
|
|
381
|
+
ts.isJsxSelfClosingElement(tag) &&
|
|
382
|
+
canPassToReact(tag))
|
|
383
|
+
: !['ref', 'children', 'defaultRichTextElements', 'textComponent'].includes(attr.name.text)));
|
|
384
|
+
});
|
|
385
|
+
if (supported)
|
|
386
|
+
selected.add(item);
|
|
387
|
+
}
|
|
388
|
+
return selected;
|
|
389
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
/** The props object for a self-closing React element with closed literal attributes. */
|
|
3
|
+
export declare function staticReactProps(element: ts.JsxSelfClosingElement): string | null;
|
|
4
|
+
/** A React-owned value or renderer in an inline options object. */
|
|
5
|
+
export declare function reactRenderer(property: ts.ObjectLiteralElementLike): {
|
|
6
|
+
body: ts.ConciseBody;
|
|
7
|
+
value: boolean;
|
|
8
|
+
args: string;
|
|
9
|
+
} | undefined;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
/** The props object for a self-closing React element with closed literal attributes. */
|
|
3
|
+
export function staticReactProps(element) {
|
|
4
|
+
if (element.typeArguments)
|
|
5
|
+
return null;
|
|
6
|
+
const props = [];
|
|
7
|
+
const seen = new Set(['key', 'ref', '__proto__', '__self', '__source']);
|
|
8
|
+
for (const attr of element.attributes.properties) {
|
|
9
|
+
if (!ts.isJsxAttribute(attr) || !ts.isIdentifier(attr.name) || seen.has(attr.name.text))
|
|
10
|
+
return null;
|
|
11
|
+
seen.add(attr.name.text);
|
|
12
|
+
const init = attr.initializer;
|
|
13
|
+
let value;
|
|
14
|
+
if (!init)
|
|
15
|
+
value = 'true';
|
|
16
|
+
else if (ts.isStringLiteral(init) && !/[&\r\n]/.test(init.text))
|
|
17
|
+
value = JSON.stringify(init.text);
|
|
18
|
+
else if (ts.isJsxExpression(init) && init.expression && !init.dotDotDotToken) {
|
|
19
|
+
const expr = init.expression;
|
|
20
|
+
if (!(ts.isStringLiteralLike(expr) ||
|
|
21
|
+
ts.isNumericLiteral(expr) ||
|
|
22
|
+
expr.kind === ts.SyntaxKind.TrueKeyword ||
|
|
23
|
+
expr.kind === ts.SyntaxKind.FalseKeyword ||
|
|
24
|
+
expr.kind === ts.SyntaxKind.NullKeyword))
|
|
25
|
+
return null;
|
|
26
|
+
value = ts.isStringLiteralLike(expr) ? JSON.stringify(expr.text) : expr.getText();
|
|
27
|
+
}
|
|
28
|
+
else
|
|
29
|
+
return null;
|
|
30
|
+
props.push(JSON.stringify(attr.name.text) + ': ' + value);
|
|
31
|
+
}
|
|
32
|
+
return '{ ' + props.join(', ') + ' }';
|
|
33
|
+
}
|
|
34
|
+
/** A React-owned value or renderer in an inline options object. */
|
|
35
|
+
export function reactRenderer(property) {
|
|
36
|
+
if (!ts.isPropertyAssignment(property) || !ts.isIdentifier(property.name))
|
|
37
|
+
return;
|
|
38
|
+
const initializer = property.initializer;
|
|
39
|
+
const arrow = ts.isArrowFunction(initializer) ? initializer : null;
|
|
40
|
+
if (arrow &&
|
|
41
|
+
(arrow.modifiers || arrow.type || arrow.typeParameters || arrow.parameters.length > 1))
|
|
42
|
+
return;
|
|
43
|
+
const param = arrow?.parameters[0];
|
|
44
|
+
if (param &&
|
|
45
|
+
(!ts.isIdentifier(param.name) ||
|
|
46
|
+
param.type ||
|
|
47
|
+
param.dotDotDotToken ||
|
|
48
|
+
param.initializer ||
|
|
49
|
+
param.questionToken ||
|
|
50
|
+
param.modifiers))
|
|
51
|
+
return;
|
|
52
|
+
let body = arrow?.body ?? initializer;
|
|
53
|
+
while (ts.isParenthesizedExpression(body))
|
|
54
|
+
body = body.expression;
|
|
55
|
+
if (!arrow && !ts.isJsxSelfClosingElement(body))
|
|
56
|
+
return;
|
|
57
|
+
const tag = ts.isJsxElement(body)
|
|
58
|
+
? body.openingElement
|
|
59
|
+
: ts.isJsxSelfClosingElement(body)
|
|
60
|
+
? body
|
|
61
|
+
: null;
|
|
62
|
+
if (!tag || !ts.isIdentifier(tag.tagName) || !/^[a-z][a-z0-9]*$/.test(tag.tagName.text))
|
|
63
|
+
return;
|
|
64
|
+
const props = [];
|
|
65
|
+
let key;
|
|
66
|
+
for (const attr of tag.attributes.properties) {
|
|
67
|
+
if (!ts.isJsxAttribute(attr) ||
|
|
68
|
+
!ts.isIdentifier(attr.name) ||
|
|
69
|
+
!attr.initializer ||
|
|
70
|
+
!ts.isStringLiteral(attr.initializer) ||
|
|
71
|
+
/[&\r\n]/.test(attr.initializer.text) ||
|
|
72
|
+
!/^[a-z][a-zA-Z0-9-]*$/.test(attr.name.text) ||
|
|
73
|
+
/^(?:ref|children|dangerouslySetInnerHTML|is|style)$|^on/i.test(attr.name.text))
|
|
74
|
+
return;
|
|
75
|
+
const value = JSON.stringify(attr.initializer.text);
|
|
76
|
+
if (attr.name.text === 'key')
|
|
77
|
+
key = value;
|
|
78
|
+
else
|
|
79
|
+
props.push(JSON.stringify(attr.name.text) + ': ' + value);
|
|
80
|
+
}
|
|
81
|
+
if (ts.isJsxElement(body)) {
|
|
82
|
+
// JSX discards indentation on empty lines, but a space beside a child is visible.
|
|
83
|
+
const children = body.children.filter((child) => !(ts.isJsxText(child) && /^[ \t\r\n]*[\r\n][ \t\r\n]*$/.test(child.text)));
|
|
84
|
+
const [child] = children;
|
|
85
|
+
if (!param ||
|
|
86
|
+
children.length !== 1 ||
|
|
87
|
+
!ts.isJsxExpression(child) ||
|
|
88
|
+
child.dotDotDotToken ||
|
|
89
|
+
!child.expression ||
|
|
90
|
+
!ts.isIdentifier(child.expression) ||
|
|
91
|
+
child.expression.text !== param.name.text)
|
|
92
|
+
return;
|
|
93
|
+
props.push('children: ' + child.expression.text);
|
|
94
|
+
}
|
|
95
|
+
else if (param)
|
|
96
|
+
return;
|
|
97
|
+
return {
|
|
98
|
+
body,
|
|
99
|
+
value: arrow === null,
|
|
100
|
+
args: JSON.stringify(tag.tagName.text) +
|
|
101
|
+
', { ' +
|
|
102
|
+
props.join(', ') +
|
|
103
|
+
' }' +
|
|
104
|
+
(key === undefined ? '' : ', ' + key),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { runCodemod } from './project.js';
|
|
2
|
+
import type { ScopedSignalOptions } from './types.js';
|
|
3
|
+
/** Create a CLI recipe for one scoped producer and its direct consumers. */
|
|
4
|
+
export declare function createScopedSignalRecipe(config: ScopedSignalOptions): typeof runCodemod;
|