@octanejs/codemod 0.2.2 → 0.2.3

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/dist/transform.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { svgDataAttribute, svgLiteralRootOverride } from './path-svg.js';
2
- import { forwardedProps, hasEval, literalJsxAttribute, nativeOutputOwner, returnedTextReceiver, renderUses, rootHandleEscapes, svgLocalReads, } from './forwarded-props.js';
2
+ import { contains, forwardedProps, hasEval, hasPrototypeAccess, literalJsxAttribute, nativeOutputOwner, returnedTextReceiver, renderUses, rootHandleEscapes, svgLocalReads, } from './forwarded-props.js';
3
3
  import { reactRenderer, staticReactProps, unreadServerStream } from './react-renderer.js';
4
4
  import { linkedRenderState } from './linked-state.js';
5
5
  import { nativeSetupHook } from './local-react-hook.js';
@@ -38,6 +38,22 @@ function directMember(receiver) {
38
38
  return parent.right;
39
39
  return null;
40
40
  }
41
+ function literalDisplayNameAssignment(ref, declaration) {
42
+ const member = ref.parent;
43
+ if (!ts.isPropertyAccessExpression(member) ||
44
+ member.expression !== ref ||
45
+ member.questionDotToken ||
46
+ member.name.text !== 'displayName')
47
+ return false;
48
+ const assignment = member.parent;
49
+ return (ts.isBinaryExpression(assignment) &&
50
+ assignment.left === member &&
51
+ assignment.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
52
+ ts.isExpressionStatement(assignment.parent) &&
53
+ assignment.parent.parent === declaration.parent &&
54
+ assignment.getStart() >= declaration.end &&
55
+ ts.isStringLiteralLike(skipWrappers(assignment.right)));
56
+ }
41
57
  // React Router applications can augment this loader type on the original module.
42
58
  function routerLoadContext(module, name) {
43
59
  return name === 'AppLoadContext' && (module === 'react-router' || module === 'react-router-dom');
@@ -694,6 +710,7 @@ class ModuleTransform {
694
710
  changeSpreads = new Set();
695
711
  /** Proven element slots on converted top-level declarations, for the project caller pass. */
696
712
  elementChildren = [];
713
+ ordinaryElementMetadata;
697
714
  /** Components to mark `descriptorChildren`. */
698
715
  descriptorComponents = new Set();
699
716
  /** Whether a `declare module` now augments `octane/jsx-runtime`. */
@@ -800,6 +817,8 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
800
817
  const reexports = [];
801
818
  const isExported = (statement) => ts.canHaveModifiers(statement) &&
802
819
  ts.getModifiers(statement)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
820
+ const elementChildren = [...this.elementChildren];
821
+ let safeElementAliases;
803
822
  const descriptorChildren = [...this.descriptorComponents].flatMap((d) => {
804
823
  const statement = ts.isVariableDeclaration(d) ? d.parent.parent : d;
805
824
  return d.name && isExported(statement) ? [d.name.getText(this.sf)] : [];
@@ -828,7 +847,9 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
828
847
  for (const element of statement.exportClause.elements) {
829
848
  if (contexts.has((element.propertyName ?? element.name).text))
830
849
  exported.add(element.name.text);
831
- if (!this.descriptorComponents.size || statement.isTypeOnly || element.isTypeOnly)
850
+ if ((!this.descriptorComponents.size && !this.elementChildren.length) ||
851
+ statement.isTypeOnly ||
852
+ element.isTypeOnly)
832
853
  continue;
833
854
  const local = this.checker.getExportSpecifierLocalTargetSymbol(element)?.valueDeclaration;
834
855
  if (local &&
@@ -836,6 +857,22 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
836
857
  this.descriptorComponents.has(local)) {
837
858
  descriptorChildren.push(element.name.text);
838
859
  }
860
+ if (local && ts.isFunctionDeclaration(local) && local.name) {
861
+ const start = local.getStart(this.sf);
862
+ if (this.elementChildren.some((slot) => slot.start === start) &&
863
+ this.localUses(local.name).every((ref) => inTypeNode(ref) ||
864
+ componentTagOrExport(ref) ||
865
+ this.isOrdinaryElementMetadata(ref, local)) &&
866
+ (safeElementAliases ??=
867
+ this.options.mutableExports?.() !== true &&
868
+ !contains(this.sf, (node) => node.kind === ts.SyntaxKind.ThisKeyword ||
869
+ (ts.isIdentifier(node) &&
870
+ /^(?:eval|module|exports|require|arguments)$/.test(node.text) &&
871
+ !isPropertyName(node) &&
872
+ !declaresName(node) &&
873
+ !inTypeNode(node)))))
874
+ elementChildren.push({ start, exported: element.name.text });
875
+ }
839
876
  }
840
877
  }
841
878
  }
@@ -857,11 +894,18 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
857
894
  continue;
858
895
  if (nativeSetupHook(ref, this.localUses, this.checker, undefined, true) !== 'first')
859
896
  continue;
860
- const component = ts.findAncestor(ref, ts.isFunctionDeclaration);
861
- if (component?.modifiers?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword))
862
- storeHookComponents.add('default');
863
- else if (component?.name)
864
- storeHookComponents.add(component.name.text);
897
+ const component = ts.findAncestor(ref, ts.isFunctionLike);
898
+ if (component && ts.isFunctionDeclaration(component)) {
899
+ if (component.modifiers?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword))
900
+ storeHookComponents.add('default');
901
+ else if (component.name)
902
+ storeHookComponents.add(component.name.text);
903
+ }
904
+ else if (component && ts.isVariableDeclaration(component.parent)) {
905
+ const name = component.parent.name;
906
+ if (ts.isIdentifier(name))
907
+ storeHookComponents.add(name.text);
908
+ }
865
909
  }
866
910
  }
867
911
  return {
@@ -869,7 +913,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
869
913
  reexports,
870
914
  ...(descriptorChildren.length ? { descriptorChildren } : {}),
871
915
  ...(storeHookComponents.size ? { storeHookComponents: [...storeHookComponents] } : {}),
872
- ...(this.elementChildren.length ? { elementChildren: this.elementChildren } : {}),
916
+ ...(elementChildren.length ? { elementChildren } : {}),
873
917
  };
874
918
  }
875
919
  // ---------------------------------------------------------------- analysis
@@ -2262,20 +2306,31 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
2262
2306
  if (ts.isFunctionDeclaration(fn) &&
2263
2307
  fn.name &&
2264
2308
  fn.parent === this.sf &&
2265
- this.checker.getSymbolAtLocation(fn.name)?.declarations?.length === 1 &&
2266
2309
  this.acceptsSingleElement(declaration.type)) {
2267
2310
  const flags = ts.getCombinedModifierFlags(fn);
2268
- this.elementChildren.push({
2269
- start: fn.getStart(this.sf),
2270
- exported: flags & ts.ModifierFlags.Export
2271
- ? flags & ts.ModifierFlags.Default
2272
- ? 'default'
2273
- : fn.name.text
2274
- : undefined,
2275
- });
2311
+ const declarations = this.checker.getSymbolAtLocation(fn.name)?.declarations;
2312
+ // TypeScript records a property written on the function as an additional
2313
+ // declaration. Ordinary displayName assignments still have one function body.
2314
+ if (declarations?.includes(fn) &&
2315
+ declarations.every((item) => item === fn ||
2316
+ (!(flags & ts.ModifierFlags.Export) &&
2317
+ ts.isIdentifier(item) &&
2318
+ this.isOrdinaryElementMetadata(item, fn))))
2319
+ this.elementChildren.push({
2320
+ start: fn.getStart(this.sf),
2321
+ exported: flags & ts.ModifierFlags.Export
2322
+ ? flags & ts.ModifierFlags.Default
2323
+ ? 'default'
2324
+ : fn.name.text
2325
+ : undefined,
2326
+ });
2276
2327
  }
2277
2328
  this.note(RULE.elementChildren, 'warning', fn, 'Nested TSRX children arrive as a render block. Pass element values explicitly with `children={...}` to keep this slot’s element type. If it only renders content and should accept nested templates, include `ChildrenBlock` from `octane/tsrx-boundary` in the children type.');
2278
2329
  }
2330
+ isOrdinaryElementMetadata(ref, fn) {
2331
+ return (literalDisplayNameAssignment(ref, fn) &&
2332
+ (this.ordinaryElementMetadata ??= !hasPrototypeAccess(this.sf)));
2333
+ }
2279
2334
  // The caller already established that this is an element-only slot. Array-only
2280
2335
  // and parameterized element types still need the author's review.
2281
2336
  acceptsSingleElement(type) {
@@ -3499,6 +3554,11 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
3499
3554
  retained.push(text);
3500
3555
  continue;
3501
3556
  }
3557
+ if (!typeOnly && !mapped && Object.hasOwn(UNSUPPORTED_VALUES, name)) {
3558
+ retained.push(text);
3559
+ this.note(RULE.unsupported, 'error', item, `${UNSUPPORTED_VALUES[name]} This re-export stays on React.`);
3560
+ continue;
3561
+ }
3502
3562
  if (name === 'SubmitEvent' ||
3503
3563
  name === 'SubmitEventHandler' ||
3504
3564
  name === 'ReactEventHandler') {
@@ -4780,7 +4840,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
4780
4840
  return false;
4781
4841
  const simpleName = (name) => ts.isIdentifier(name) || ts.isStringLiteral(name) ? name.text : undefined;
4782
4842
  const required = new Set([key]);
4783
- const numeric = new Set();
4843
+ const coerced = new Set();
4784
4844
  const addField = (name) => {
4785
4845
  if (name === undefined || name === '__proto__')
4786
4846
  return false;
@@ -4812,7 +4872,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
4812
4872
  outer.operator !== ts.SyntaxKind.PlusPlusToken &&
4813
4873
  outer.operator !== ts.SyntaxKind.MinusMinusToken) {
4814
4874
  if (outer.operator !== ts.SyntaxKind.ExclamationToken)
4815
- numeric.add(parent.name.text);
4875
+ coerced.add(parent.name.text);
4816
4876
  return true;
4817
4877
  }
4818
4878
  if (ts.isTypeOfExpression(outer) ||
@@ -4824,11 +4884,11 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
4824
4884
  (ts.isJsxExpression(outer) && ts.isJsxAttribute(outer.parent)) ||
4825
4885
  outer === dependencies)
4826
4886
  return true;
4827
- const coercesNumber = nestedRead(parent);
4828
- if (coercesNumber === undefined)
4887
+ const coercesValue = nestedRead(parent);
4888
+ if (coercesValue === undefined)
4829
4889
  return false;
4830
- if (coercesNumber)
4831
- numeric.add(parent.name.text);
4890
+ if (coercesValue)
4891
+ coerced.add(parent.name.text);
4832
4892
  return true;
4833
4893
  });
4834
4894
  if (!closed)
@@ -4855,7 +4915,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
4855
4915
  const name = simpleName(field.name);
4856
4916
  if (name === undefined || name === '__proto__')
4857
4917
  return false;
4858
- if (numeric.has(name) && !isNumericConstant(field.initializer))
4918
+ if (coerced.has(name) && !isPrimitiveLiteral(field.initializer))
4859
4919
  return false;
4860
4920
  missing.delete(name);
4861
4921
  }
@@ -4889,7 +4949,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
4889
4949
  ownsFields(next.body, prior.name));
4890
4950
  });
4891
4951
  }
4892
- ownDataDependency(member, hook, dependencies, coercesNumber, nestedRead) {
4952
+ ownDataDependency(member, hook, dependencies, coercesValue, nestedRead) {
4893
4953
  const receiver = skipWrappers(member.expression);
4894
4954
  const name = ts.isPropertyAccessExpression(member)
4895
4955
  ? member.name
@@ -4921,25 +4981,119 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
4921
4981
  return false;
4922
4982
  if (!name || (!ts.isIdentifier(name) && !ts.isStringLiteralLike(name))) {
4923
4983
  // A numeric name can override the corresponding string name.
4924
- if (coercesNumber)
4984
+ if (coercesValue)
4925
4985
  return false;
4926
4986
  continue;
4927
4987
  }
4928
4988
  if (name.text !== key)
4929
4989
  continue;
4930
4990
  owns = true;
4931
- if (coercesNumber &&
4932
- !(ts.isPropertyAssignment(field) && isNumericConstant(field.initializer)))
4991
+ if (coercesValue &&
4992
+ !(ts.isPropertyAssignment(field) && isPrimitiveLiteral(field.initializer)))
4933
4993
  return false;
4934
4994
  }
4935
4995
  return owns;
4936
4996
  }
4997
+ copiesOwnGetter(spread, hook) {
4998
+ let object = skipWrappers(spread.expression);
4999
+ // An inline operand can invoke a getter before the outer copy begins.
5000
+ if (ts.isObjectLiteralExpression(object) &&
5001
+ object.properties.some((field) => ts.isSpreadAssignment(field) && this.copiesOwnGetter(field, hook)))
5002
+ return true;
5003
+ if (ts.isIdentifier(object)) {
5004
+ const owner = ts.findAncestor(hook, ts.isFunctionLike);
5005
+ const init = this.constValue(object);
5006
+ if (!init ||
5007
+ !owner ||
5008
+ !fixedBinding(this.checker, object, hook) ||
5009
+ !initializedBefore(this.checker, object, hook) ||
5010
+ this.localUses(object).some((name) => !declaresName(name) && !inTypeNode(name)) ||
5011
+ reads(owner, (name) => name.text === 'eval'))
5012
+ return false;
5013
+ object = skipWrappers(init);
5014
+ }
5015
+ if (!ts.isObjectLiteralExpression(object))
5016
+ return false;
5017
+ const overwritten = new Set();
5018
+ for (let i = object.properties.length - 1; i >= 0; i--) {
5019
+ const field = object.properties[i];
5020
+ // A later setter preserves an existing getter for its key.
5021
+ if (ts.isSetAccessorDeclaration(field))
5022
+ continue;
5023
+ if (ts.isGetAccessorDeclaration(field) && overwritten.size === 0)
5024
+ return true;
5025
+ const computed = field.name && ts.isComputedPropertyName(field.name);
5026
+ const name = computed ? skipWrappers(field.name.expression) : field.name;
5027
+ if (!name ||
5028
+ !((!computed && ts.isIdentifier(name)) ||
5029
+ ts.isStringLiteralLike(name) ||
5030
+ ts.isNumericLiteral(name)))
5031
+ return false;
5032
+ if (!computed && ts.isPropertyAssignment(field) && name.text === '__proto__')
5033
+ continue;
5034
+ if (ts.isGetAccessorDeclaration(field) && !overwritten.has(name.text))
5035
+ return true;
5036
+ overwritten.add(name.text);
5037
+ }
5038
+ return false;
5039
+ }
4937
5040
  checkHookDependencyReads(node, deps) {
4938
5041
  const list = skipWrappers(deps);
4939
5042
  if (!this.emitsTsrx || !ts.isArrayLiteralExpression(list))
4940
5043
  return;
4941
- const read = (expr, visit, coercesNumber = false) => {
5044
+ let memberReads;
5045
+ const recordStateRead = (member, coercesValue) => {
5046
+ memberReads?.set(member, coercesValue);
5047
+ return undefined;
5048
+ };
5049
+ const read = (expr, visit, coercesValue = false) => {
4942
5050
  const member = skipWrappers(expr);
5051
+ if (ts.isSpreadElement(member)) {
5052
+ const value = skipWrappers(member.expression);
5053
+ const stateProof = visit === recordStateRead;
5054
+ if (stateProof && !ts.isArrayLiteralExpression(value))
5055
+ return;
5056
+ // A custom iterator can observe elements of a fresh array.
5057
+ return read(value, visit, coercesValue || stateProof);
5058
+ }
5059
+ if (ts.isArrayLiteralExpression(member)) {
5060
+ for (const item of member.elements) {
5061
+ const found = read(item, visit, coercesValue);
5062
+ if (found)
5063
+ return found;
5064
+ }
5065
+ return;
5066
+ }
5067
+ if (ts.isObjectLiteralExpression(member)) {
5068
+ for (const property of member.properties) {
5069
+ if (ts.isSpreadAssignment(property)) {
5070
+ if (visit !== recordStateRead && this.copiesOwnGetter(property, node))
5071
+ return property;
5072
+ continue;
5073
+ }
5074
+ const name = property.name;
5075
+ if (name && ts.isComputedPropertyName(name)) {
5076
+ const found = read(name.expression, visit, true);
5077
+ if (found)
5078
+ return found;
5079
+ }
5080
+ // Method and accessor bodies are deferred. Outer coercion or iteration can read any field.
5081
+ if (ts.isPropertyAssignment(property)) {
5082
+ const found = read(property.initializer, visit, coercesValue);
5083
+ if (found)
5084
+ return found;
5085
+ }
5086
+ }
5087
+ return;
5088
+ }
5089
+ if (ts.isTemplateExpression(member)) {
5090
+ for (const span of member.templateSpans) {
5091
+ const found = read(span.expression, visit, true);
5092
+ if (found)
5093
+ return found;
5094
+ }
5095
+ return;
5096
+ }
4943
5097
  if (ts.isTypeOfExpression(member) || ts.isVoidExpression(member))
4944
5098
  return read(member.expression, visit);
4945
5099
  if (ts.isPrefixUnaryExpression(member) &&
@@ -4949,20 +5103,33 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
4949
5103
  if (ts.isConditionalExpression(member)) {
4950
5104
  const condition = skipWrappers(member.condition);
4951
5105
  if (condition.kind === ts.SyntaxKind.TrueKeyword)
4952
- return read(member.whenTrue, visit, coercesNumber);
5106
+ return read(member.whenTrue, visit, coercesValue);
4953
5107
  if (condition.kind === ts.SyntaxKind.FalseKeyword)
4954
- return read(member.whenFalse, visit, coercesNumber);
5108
+ return read(member.whenFalse, visit, coercesValue);
4955
5109
  return (read(condition, visit) ??
4956
- read(member.whenTrue, visit, coercesNumber) ??
4957
- read(member.whenFalse, visit, coercesNumber));
5110
+ read(member.whenTrue, visit, coercesValue) ??
5111
+ read(member.whenFalse, visit, coercesValue));
4958
5112
  }
4959
5113
  if (ts.isBinaryExpression(member)) {
4960
5114
  const op = member.operatorToken.kind;
5115
+ // The left value is read and discarded. Only the right value reaches outer coercion.
5116
+ if (op === ts.SyntaxKind.CommaToken)
5117
+ return read(member.left, visit) ?? read(member.right, visit, coercesValue);
5118
+ // Strict comparison reads both operands and does not coerce either.
5119
+ if (op === ts.SyntaxKind.EqualsEqualsEqualsToken ||
5120
+ op === ts.SyntaxKind.ExclamationEqualsEqualsToken)
5121
+ return read(member.left, visit) ?? read(member.right, visit);
5122
+ // Relational comparison can coerce either operand through user code.
5123
+ if (op === ts.SyntaxKind.LessThanToken ||
5124
+ op === ts.SyntaxKind.LessThanEqualsToken ||
5125
+ op === ts.SyntaxKind.GreaterThanToken ||
5126
+ op === ts.SyntaxKind.GreaterThanEqualsToken)
5127
+ return read(member.left, visit, true) ?? read(member.right, visit, true);
4961
5128
  if (op === ts.SyntaxKind.AmpersandAmpersandToken ||
4962
5129
  op === ts.SyntaxKind.BarBarToken ||
4963
5130
  op === ts.SyntaxKind.QuestionQuestionToken) {
4964
5131
  // Only a falsy left value can become the result of &&.
4965
- const left = read(member.left, visit, coercesNumber && op !== ts.SyntaxKind.AmpersandAmpersandToken);
5132
+ const left = read(member.left, visit, coercesValue && op !== ts.SyntaxKind.AmpersandAmpersandToken);
4966
5133
  if (left)
4967
5134
  return left;
4968
5135
  const condition = literalCondition(member.left);
@@ -4970,31 +5137,25 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
4970
5137
  (op === ts.SyntaxKind.AmpersandAmpersandToken && condition === 'truthy') ||
4971
5138
  (op === ts.SyntaxKind.BarBarToken && condition !== 'truthy') ||
4972
5139
  (op === ts.SyntaxKind.QuestionQuestionToken && condition === 'nullish'))
4973
- return read(member.right, visit, coercesNumber);
5140
+ return read(member.right, visit, coercesValue);
4974
5141
  }
4975
5142
  }
4976
5143
  if (ts.isPropertyAccessExpression(member) || ts.isElementAccessExpression(member))
4977
- return visit(member, coercesNumber);
5144
+ return visit(member, coercesValue);
4978
5145
  };
4979
5146
  // The closed-state proof must account for every field read in this list.
4980
- let members;
4981
5147
  const nestedRead = (member) => {
4982
- if (!members) {
4983
- const reachable = new Map();
4984
- for (const entry of list.elements) {
4985
- read(entry, (dep, coercesNumber) => {
4986
- reachable.set(dep, coercesNumber);
4987
- return undefined;
4988
- });
4989
- }
4990
- members = reachable;
5148
+ if (!memberReads) {
5149
+ memberReads = new Map();
5150
+ for (const entry of list.elements)
5151
+ read(entry, recordStateRead);
4991
5152
  }
4992
- return members.get(member);
5153
+ return memberReads.get(member);
4993
5154
  };
4994
5155
  let opaqueCoercion = false;
4995
- const check = (member, coercesNumber) => {
4996
- if (!this.ownDataDependency(member, node, list, coercesNumber, nestedRead)) {
4997
- opaqueCoercion = coercesNumber;
5156
+ const check = (member, coercesValue) => {
5157
+ if (!this.ownDataDependency(member, node, list, coercesValue, nestedRead)) {
5158
+ opaqueCoercion = coercesValue;
4998
5159
  return member;
4999
5160
  }
5000
5161
  };
@@ -5005,9 +5166,11 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
5005
5166
  break;
5006
5167
  }
5007
5168
  if (opaque)
5008
- this.note(RULE.deps, 'error', opaque, opaqueCoercion
5009
- ? 'This hook dependency reads a property during render. It may run a getter owned by React or execute user code during numeric coercion. Keep the component in React or verify the property before moving it to native SSR.'
5010
- : 'This hook dependency reads a property during render and may run a getter owned by React. Keep the component in React or verify the getter before moving it to native SSR.');
5169
+ this.note(RULE.deps, 'error', opaque, ts.isSpreadAssignment(opaque)
5170
+ ? 'This hook dependency can invoke an own getter while spreading an object. Keep the component in React or verify the getter before moving it to native SSR.'
5171
+ : opaqueCoercion
5172
+ ? 'This hook dependency reads a property during render. It may run a getter owned by React or execute user code during value coercion. Keep the component in React or verify the property before moving it to native SSR.'
5173
+ : 'This hook dependency reads a property during render and may run a getter owned by React. Keep the component in React or verify the getter before moving it to native SSR.');
5011
5174
  }
5012
5175
  /**
5013
5176
  * React runs a hook with an omitted or undefined list on every render.
@@ -5814,7 +5977,8 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
5814
5977
  this.count(RULE.className);
5815
5978
  out += 'class' + this.emitInitializer(attribute);
5816
5979
  }
5817
- else if ((name === 'onChange' || name === 'onChangeCapture') && this.isTextEntry(element)) {
5980
+ else if ((name === 'onChange' || name === 'onChangeCapture') &&
5981
+ this.isTextEntry(element, name === 'onChange')) {
5818
5982
  const input = name === 'onChange' ? 'onInput' : 'onInputCapture';
5819
5983
  const existing = attributeOf(element, input);
5820
5984
  const changesOrder = !spread &&
@@ -5867,11 +6031,60 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
5867
6031
  return (this.source.slice(attribute.name.end, attribute.initializer.getStart(this.sf)) +
5868
6032
  this.emit(attribute.initializer, PLAIN));
5869
6033
  }
6034
+ textInputResult(expression) {
6035
+ if (!expression)
6036
+ return false;
6037
+ const value = skipWrappers(expression);
6038
+ if (ts.isConditionalExpression(value))
6039
+ return this.textInputResult(value.whenTrue) && this.textInputResult(value.whenFalse);
6040
+ return (ts.isStringLiteralLike(value) && (value.text === 'date' || TEXT_INPUT_TYPES.has(value.text)));
6041
+ }
6042
+ textInputTypeCall(call) {
6043
+ if (!ts.isIdentifier(call.expression) || !ts.isExternalModule(this.sf))
6044
+ return false;
6045
+ const symbol = this.checker.getSymbolAtLocation(call.expression);
6046
+ const fn = symbol?.valueDeclaration;
6047
+ if (symbol?.declarations?.length !== 1 ||
6048
+ !fn ||
6049
+ !ts.isFunctionDeclaration(fn) ||
6050
+ !fn.name ||
6051
+ !fn.body ||
6052
+ fn.parent !== this.sf ||
6053
+ fn.asteriskToken ||
6054
+ fn.modifiers?.length)
6055
+ return false;
6056
+ const [statement] = fn.body.statements;
6057
+ // Unmatched switch values and empty trailing clauses return undefined, the default text type.
6058
+ const knownResult = this.textInputResult(singleCallbackExpression(fn.body)) ||
6059
+ (fn.body.statements.length === 1 &&
6060
+ ts.isSwitchStatement(statement) &&
6061
+ statement.caseBlock.clauses.every(({ statements }) => {
6062
+ const [first] = statements;
6063
+ return (!first ||
6064
+ (statements.length === 1 &&
6065
+ ts.isReturnStatement(first) &&
6066
+ this.textInputResult(first.expression)));
6067
+ }));
6068
+ return (knownResult &&
6069
+ (!/eval|\\u/.test(this.source) || !hasEval(this.sf)) &&
6070
+ this.localUses(fn.name).every((ref) => inTypeNode(ref) || (ts.isCallExpression(ref.parent) && ref.parent.expression === ref)));
6071
+ }
6072
+ hasAncestorInputHandler(element) {
6073
+ for (let parent = element.parent; !ts.isFunctionLike(parent) && !ts.isSourceFile(parent); parent = parent.parent) {
6074
+ if (!ts.isJsxElement(parent) || parent.openingElement === element)
6075
+ continue;
6076
+ if (parent.openingElement.attributes.properties.some((attribute) => ts.isJsxSpreadAttribute(attribute) ||
6077
+ (ts.isIdentifier(attribute.name) &&
6078
+ /^on(?:Input|Change)(?:Capture)?$/.test(attribute.name.text))))
6079
+ return true;
6080
+ }
6081
+ return false;
6082
+ }
5870
6083
  /**
5871
6084
  * Whether `element` is a text-entry control, where React's `onChange` fires
5872
6085
  * per edit and the native `change` fires on commit.
5873
6086
  */
5874
- isTextEntry(element) {
6087
+ isTextEntry(element, ordinaryChange) {
5875
6088
  const tag = element.tagName.getText(this.sf);
5876
6089
  if (tag === 'textarea')
5877
6090
  return true;
@@ -5886,13 +6099,21 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
5886
6099
  if (tag === 'input' && value && ts.isJsxExpression(value) && value.expression) {
5887
6100
  const expression = skipWrappers(value.expression);
5888
6101
  // A spread can also supply the input handler that a rename would overwrite.
5889
- if (ts.isStringLiteralLike(expression) &&
5890
- TEXT_INPUT_TYPES.has(expression.text) &&
5891
- !element.attributes.properties.some((attribute) => ts.isJsxSpreadAttribute(attribute) ||
5892
- (attribute !== type &&
5893
- ts.isIdentifier(attribute.name) &&
5894
- attribute.name.text === 'type'))) {
5895
- return true;
6102
+ const fixedType = !element.attributes.properties.some((attribute) => ts.isJsxSpreadAttribute(attribute) ||
6103
+ (attribute !== type && ts.isIdentifier(attribute.name) && attribute.name.text === 'type'));
6104
+ if (fixedType) {
6105
+ if (ts.isStringLiteralLike(expression) && TEXT_INPUT_TYPES.has(expression.text))
6106
+ return true;
6107
+ // React dispatches native input and synthetic change in separate passes.
6108
+ if (this.target === 'tsrx' &&
6109
+ ordinaryChange &&
6110
+ !attributeOf(element, 'ref') &&
6111
+ !attributeOf(element, 'onInputCapture') &&
6112
+ !attributeOf(element, 'onChangeCapture') &&
6113
+ ((ts.isConditionalExpression(expression) && this.textInputResult(expression)) ||
6114
+ (ts.isCallExpression(expression) && this.textInputTypeCall(expression))) &&
6115
+ !this.hasAncestorInputHandler(element))
6116
+ return true;
5896
6117
  }
5897
6118
  }
5898
6119
  this.note(RULE.nativeChange, 'warning', element, 'This `<input>` has a dynamic `type`; if it can be a text field, use `onInput` for per-edit updates.');
@@ -5970,7 +6191,7 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
5970
6191
  if (!entry || entry.namespace || entry.typeOnly)
5971
6192
  return false;
5972
6193
  const name = entry.imported === 'default' ? entry.local : entry.imported;
5973
- const context = this.options.isContext?.(entry.module, entry.imported, entry.local);
6194
+ const context = this.options.isContext?.(entry.module, entry.imported, entry.local, declaration != null && !!ts.findAncestor(declaration, ts.isImportDeclaration)?.attributes);
5974
6195
  if (context !== 'review-alias')
5975
6196
  return context ?? /Context$/.test(name);
5976
6197
  if (member && !ts.isJsxClosingElement(member.parent))
@@ -6620,6 +6841,16 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
6620
6841
  this.note(RULE.for, 'error', node, 'An @for row would discard this callback’s promise or iterator. Review the retained .map() call for suspension or iteration.');
6621
6842
  return null;
6622
6843
  }
6844
+ if (ts.isFunctionExpression(fn) &&
6845
+ fn.name &&
6846
+ (this.localUses(fn.name).length ||
6847
+ reads(fn, (name) => name.text === 'eval' &&
6848
+ !isPropertyName(name) &&
6849
+ !declaresName(name) &&
6850
+ !inTypeNode(name)))) {
6851
+ this.note(MAP_CALLBACK_SCOPE, 'error', node, 'An @for row cannot preserve this map callback’s local function name. Keep the original map callback until these references are migrated explicitly.');
6852
+ return null;
6853
+ }
6623
6854
  if (fn.parameters.some((parameter) => readsFunctionContext(parameter, this.checker))) {
6624
6855
  this.note(MAP_CALLBACK_SCOPE, 'error', node, 'An @for row cannot preserve this callback’s access to this, arguments, or new.target. Keep the original map callback until these reads are migrated explicitly.');
6625
6856
  return null;
@@ -6800,6 +7031,17 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
6800
7031
  (!ts.isBinaryExpression(receiver) && literalCondition(receiver) !== 'nullish')) &&
6801
7032
  readsOrdinal(read.argumentExpression));
6802
7033
  }
7034
+ if (ts.isNewExpression(read)) {
7035
+ const constructor = skipWrappers(read.expression);
7036
+ // A plain inline class finishes defining itself before the arguments run.
7037
+ return (ts.isClassExpression(constructor) &&
7038
+ !constructor.heritageClauses?.length &&
7039
+ !constructor.modifiers?.length &&
7040
+ constructor.members.every((member) => ts.isConstructorDeclaration(member) &&
7041
+ !member.modifiers?.length &&
7042
+ member.parameters.every((parameter) => !ts.getDecorators(parameter)?.length)) &&
7043
+ readsInOrder(read.arguments ?? []));
7044
+ }
6803
7045
  if (ts.isCallExpression(read)) {
6804
7046
  if (readsOrdinal(read.expression))
6805
7047
  return true;
package/dist/types.d.ts CHANGED
@@ -22,6 +22,8 @@ export interface TransformOptions {
22
22
  strong?: boolean;
23
23
  /** The project checks with `strictNullChecks`, so TypeScript types can prove text holes and boolean guards. */
24
24
  strictNullChecks?: boolean;
25
+ /** Whether the project can expose the selected module through mutable exports. */
26
+ mutableExports?(): boolean;
25
27
  /** This module in a program over the whole project, whose resolved imports type what the module's own program cannot. */
26
28
  types?: {
27
29
  sourceFile: ts.SourceFile;
@@ -79,7 +81,7 @@ export interface TransformOptions {
79
81
  imported: string;
80
82
  }): boolean;
81
83
  /** Whether an imported name is a context, with separate review for selected alias targets. */
82
- isContext?(specifier: string, name: string, local: string): boolean | 'review-alias' | null;
84
+ isContext?(specifier: string, name: string, local: string, attributed?: boolean): boolean | 'review-alias' | null;
83
85
  /** The React package an import specifier names when the codemod keeps it on React, or null. */
84
86
  unported?(specifier: string): string | null;
85
87
  /** The project has React and React DOM 19.2 or newer in the React 19 series, so `ReactCompat` can host an unported package's components. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octanejs/codemod",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=22.22.2"
@@ -47,7 +47,7 @@
47
47
  "dependencies": {
48
48
  "@octanejs/cli": "0.3.6",
49
49
  "@tsrx/oxc": "0.23.0",
50
- "octane": "0.19.0",
50
+ "octane": "0.19.1",
51
51
  "parse5": "8.0.1",
52
52
  "picocolors": "^1.1.1",
53
53
  "typescript": "^5.9.3"