@octanejs/codemod 0.0.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -6
- package/package.json +3 -3
- package/src/bin.js +13 -0
- package/src/index.d.ts +12 -1
- package/src/mappings.js +184 -3
- package/src/project.js +79 -56
- package/src/transform.js +791 -48
package/src/transform.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
import {
|
|
3
3
|
DEPENDENCY_HOOKS,
|
|
4
|
+
RADIX_TYPES,
|
|
4
5
|
REACT_MODULES,
|
|
5
6
|
TEXT_INPUT_TYPES,
|
|
6
7
|
UNSUPPORTED_VALUES,
|
|
8
|
+
mapPackageExport,
|
|
7
9
|
mapQualifiedType,
|
|
8
10
|
mapReactImport,
|
|
11
|
+
missingExport,
|
|
12
|
+
radixNamespace,
|
|
9
13
|
} from './mappings.js';
|
|
10
14
|
|
|
11
15
|
/**
|
|
@@ -28,6 +32,17 @@ import {
|
|
|
28
32
|
* null to keep it.
|
|
29
33
|
* @property {(specifier: string, name: string) => boolean | null} [isContext]
|
|
30
34
|
* whether an imported name is a context, or null when it cannot be known
|
|
35
|
+
* @property {(specifier: string) => string | null} [unported] the React package
|
|
36
|
+
* an import specifier names when that package has no Octane port, or null
|
|
37
|
+
* @property {boolean} [reactCompat] the project has React and React DOM 19.2 or
|
|
38
|
+
* newer in the React 19 series, so `ReactCompat` can host an unported
|
|
39
|
+
* package's components
|
|
40
|
+
*
|
|
41
|
+
* @typedef {Object} Unported A React package with no Octane port, as this
|
|
42
|
+
* module uses it
|
|
43
|
+
* @property {string} name
|
|
44
|
+
* @property {number} islands elements now rendered in a `ReactCompat` island
|
|
45
|
+
* @property {number} blocked uses that only a port can support
|
|
31
46
|
*
|
|
32
47
|
* @typedef {Object} TransformResult
|
|
33
48
|
* @property {string} code
|
|
@@ -39,6 +54,7 @@ import {
|
|
|
39
54
|
* @property {Record<string, number>} rules how often each rule fired
|
|
40
55
|
* @property {ModuleExports} exports what the module exports that other modules'
|
|
41
56
|
* rewrites depend on
|
|
57
|
+
* @property {Unported[]} unported
|
|
42
58
|
*
|
|
43
59
|
* @typedef {Object} ModuleExports
|
|
44
60
|
* @property {string[]} contexts exported names whose value is a context this
|
|
@@ -49,7 +65,7 @@ import {
|
|
|
49
65
|
|
|
50
66
|
/**
|
|
51
67
|
* @typedef {{ template: boolean }} Context
|
|
52
|
-
* @typedef {{ module: string, local: string, imported: string, namespace: boolean, typeOnly: boolean }} Import
|
|
68
|
+
* @typedef {{ module: string, local: string, imported: string, namespace: boolean, typeOnly: boolean, port: Unported | null }} Import
|
|
53
69
|
*/
|
|
54
70
|
|
|
55
71
|
/** Marks emitted text whose whole line should go. */
|
|
@@ -65,9 +81,11 @@ const PLAIN = { template: false };
|
|
|
65
81
|
|
|
66
82
|
/** Rule names, as reported. */
|
|
67
83
|
const RULE = {
|
|
84
|
+
binding: 'bindings',
|
|
68
85
|
classComponent: 'class-component',
|
|
69
86
|
consumer: 'context-consumer',
|
|
70
87
|
defaultProps: 'default-props',
|
|
88
|
+
descriptorChildren: 'descriptor-children',
|
|
71
89
|
deps: 'hook-dependencies',
|
|
72
90
|
eventObject: 'event-object',
|
|
73
91
|
forwardRef: 'forward-ref',
|
|
@@ -89,6 +107,8 @@ const RULE = {
|
|
|
89
107
|
try: 'template-try',
|
|
90
108
|
unsupported: 'unsupported-api',
|
|
91
109
|
strong: 'strong',
|
|
110
|
+
bindings: 'bindings',
|
|
111
|
+
reactCompat: 'react-compat',
|
|
92
112
|
};
|
|
93
113
|
|
|
94
114
|
/**
|
|
@@ -193,6 +213,21 @@ function isJsx(node) {
|
|
|
193
213
|
return ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node) || ts.isJsxFragment(node);
|
|
194
214
|
}
|
|
195
215
|
|
|
216
|
+
/**
|
|
217
|
+
* What a module's import of a package with no Octane port leaves to do.
|
|
218
|
+
*
|
|
219
|
+
* @param {Unported} port
|
|
220
|
+
*/
|
|
221
|
+
function portComment({ name, islands, blocked }) {
|
|
222
|
+
const missing = `No Octane port of \`${name}\` exists`;
|
|
223
|
+
if (blocked > 0) {
|
|
224
|
+
return `${missing}, and this module needs one: Octane cannot call its hooks, read its contexts, or pass Octane content to its components.`;
|
|
225
|
+
}
|
|
226
|
+
return islands > 0
|
|
227
|
+
? `${missing}, so its components render in React through ReactCompat.`
|
|
228
|
+
: `${missing}; React stays a dependency for it.`;
|
|
229
|
+
}
|
|
230
|
+
|
|
196
231
|
/**
|
|
197
232
|
* Whether a hole is already spelled as text, or as a literal the cast would
|
|
198
233
|
* only clutter.
|
|
@@ -274,6 +309,20 @@ function nodeSpanning(sf, start, end) {
|
|
|
274
309
|
}
|
|
275
310
|
}
|
|
276
311
|
|
|
312
|
+
/**
|
|
313
|
+
* The attribute an element sets by `name`.
|
|
314
|
+
*
|
|
315
|
+
* @param {ts.JsxOpeningLikeElement} element
|
|
316
|
+
* @param {string} name
|
|
317
|
+
*/
|
|
318
|
+
function attributeOf(element, name) {
|
|
319
|
+
return /** @type {ts.JsxAttribute | undefined} */ (
|
|
320
|
+
element.attributes.properties.find(
|
|
321
|
+
(a) => ts.isJsxAttribute(a) && ts.isIdentifier(a.name) && a.name.text === name,
|
|
322
|
+
)
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
277
326
|
/**
|
|
278
327
|
* The elements a list row can render: the row itself, or each arm of a
|
|
279
328
|
* conditional row. Null when an arm renders anything else.
|
|
@@ -579,11 +628,27 @@ class ModuleTransform {
|
|
|
579
628
|
this.bare = new Set();
|
|
580
629
|
/** Functions whose parameter list a rule replaced. */
|
|
581
630
|
this.parameters = new Map();
|
|
631
|
+
/** @type {Set<ts.BindingElement>} Rest props that give up `onChange` to a text field. */
|
|
632
|
+
this.restChange = new Set();
|
|
633
|
+
/** @type {Set<ts.JsxSpreadAttribute>} Spreads of those rest props, which pass it as `onInput`. */
|
|
634
|
+
this.changeSpreads = new Set();
|
|
635
|
+
/** @type {Set<ts.VariableDeclaration | ts.FunctionDeclaration>} Components to mark `descriptorChildren`. */
|
|
636
|
+
this.descriptorComponents = new Set();
|
|
637
|
+
/** Whether a `declare module` now augments `octane/jsx-runtime`. */
|
|
638
|
+
this.augments = false;
|
|
639
|
+
/** @type {Map<string, Unported | null>} Each import specifier's unported package. */
|
|
640
|
+
this.ports = new Map();
|
|
641
|
+
/** @type {Map<string, Unported>} Unported packages, by name. */
|
|
642
|
+
this.unported = new Map();
|
|
643
|
+
/** @type {Set<ts.JsxElement | ts.JsxSelfClosingElement>} Elements to host in a `ReactCompat` island. */
|
|
644
|
+
this.islands = new Set();
|
|
582
645
|
/** @type {ts.ImportDeclaration[]} Placeholder index -> import declaration. */
|
|
583
646
|
this.importSlots = [];
|
|
584
647
|
this.hasJsx = false;
|
|
585
648
|
/** Whether the module imports React, rather than already being Octane. */
|
|
586
649
|
this.fromReact = false;
|
|
650
|
+
/** @type {Map<string, string | null>} Each specifier's new spelling, or null to keep it. */
|
|
651
|
+
this.resolved = new Map();
|
|
587
652
|
}
|
|
588
653
|
|
|
589
654
|
/** @returns {TransformResult} */
|
|
@@ -611,6 +676,7 @@ class ModuleTransform {
|
|
|
611
676
|
notes: this.notes.sort((a, b) => a.line - b.line),
|
|
612
677
|
rules: this.rules,
|
|
613
678
|
exports: this.exportsOf(),
|
|
679
|
+
unported: [...this.unported.values()],
|
|
614
680
|
};
|
|
615
681
|
}
|
|
616
682
|
|
|
@@ -665,7 +731,28 @@ class ModuleTransform {
|
|
|
665
731
|
// ---------------------------------------------------------------- analysis
|
|
666
732
|
|
|
667
733
|
analyze() {
|
|
734
|
+
// Added imports quote their module as the module's own imports do.
|
|
735
|
+
const first = this.sf.statements.find(ts.isImportDeclaration);
|
|
736
|
+
this.quote = first ? first.moduleSpecifier.getText(this.sf)[0] : "'";
|
|
737
|
+
/** Locals imported from a package with no Octane port. */
|
|
738
|
+
const portLocals = new Set();
|
|
668
739
|
for (const statement of this.sf.statements) {
|
|
740
|
+
if (
|
|
741
|
+
ts.isExportDeclaration(statement) &&
|
|
742
|
+
!statement.isTypeOnly &&
|
|
743
|
+
statement.moduleSpecifier &&
|
|
744
|
+
ts.isStringLiteral(statement.moduleSpecifier)
|
|
745
|
+
) {
|
|
746
|
+
const port = this.portOf(statement.moduleSpecifier.text);
|
|
747
|
+
if (port) {
|
|
748
|
+
this.note(
|
|
749
|
+
RULE.bindings,
|
|
750
|
+
'warning',
|
|
751
|
+
statement,
|
|
752
|
+
`\`${port.name}\` has no Octane port: import its components from it where they render, so they can render in a \`ReactCompat\` island.`,
|
|
753
|
+
);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
669
756
|
if (!ts.isImportDeclaration(statement) || !statement.importClause) continue;
|
|
670
757
|
const module = /** @type {ts.StringLiteral} */ (statement.moduleSpecifier).text;
|
|
671
758
|
if (module in REACT_MODULES) this.fromReact = true;
|
|
@@ -677,8 +764,17 @@ class ModuleTransform {
|
|
|
677
764
|
/** @type {string} */ imported,
|
|
678
765
|
/** @type {boolean} */ typeOnly,
|
|
679
766
|
) => {
|
|
680
|
-
const
|
|
767
|
+
const port = typeOnly ? null : this.portOf(module);
|
|
768
|
+
const entry = {
|
|
769
|
+
module,
|
|
770
|
+
local: name.text,
|
|
771
|
+
imported,
|
|
772
|
+
namespace: imported === '*',
|
|
773
|
+
typeOnly,
|
|
774
|
+
port,
|
|
775
|
+
};
|
|
681
776
|
this.imports.set(declaration, entry);
|
|
777
|
+
if (port) portLocals.add(name.text);
|
|
682
778
|
if (isApiModule(module)) this.apiLocals.set(name.text, entry);
|
|
683
779
|
};
|
|
684
780
|
// A default import of React is the namespace in practice; any other
|
|
@@ -697,12 +793,396 @@ class ModuleTransform {
|
|
|
697
793
|
}
|
|
698
794
|
}
|
|
699
795
|
}
|
|
796
|
+
/** @type {ts.JsxOpeningLikeElement[]} */
|
|
797
|
+
const fields = [];
|
|
798
|
+
/** @type {ts.Expression[]} Values read as an element: cloned, inspected, or a `render` prop. */
|
|
799
|
+
const elements = [];
|
|
700
800
|
const visit = (/** @type {ts.Node} */ node) => {
|
|
701
801
|
if (ts.isIdentifier(node) && declaresName(node)) this.declared.add(node.text);
|
|
802
|
+
if (ts.isIdentifier(node) && portLocals.has(node.text)) this.planUnported(node);
|
|
702
803
|
if (isJsx(node)) this.hasJsx = true;
|
|
804
|
+
if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) fields.push(node);
|
|
805
|
+
if (
|
|
806
|
+
ts.isJsxAttribute(node) &&
|
|
807
|
+
node.name.getText(this.sf) === 'render' &&
|
|
808
|
+
node.initializer &&
|
|
809
|
+
ts.isJsxExpression(node.initializer) &&
|
|
810
|
+
node.initializer.expression
|
|
811
|
+
) {
|
|
812
|
+
elements.push(node.initializer.expression);
|
|
813
|
+
}
|
|
814
|
+
if (ts.isCallExpression(node) && node.arguments[0] && this.readsElement(node.expression)) {
|
|
815
|
+
elements.push(node.arguments[0]);
|
|
816
|
+
}
|
|
703
817
|
ts.forEachChild(node, visit);
|
|
704
818
|
};
|
|
705
819
|
visit(this.sf);
|
|
820
|
+
for (const field of fields) if (this.takesChange(field)) this.planRestChange(field);
|
|
821
|
+
for (const element of elements) this.planDescriptorChildren(element);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* The package with no Octane port that a specifier names, or null. It
|
|
826
|
+
* still runs on React, which only the codemod's report and a comment say.
|
|
827
|
+
*
|
|
828
|
+
* @param {string} module
|
|
829
|
+
*/
|
|
830
|
+
portOf(module) {
|
|
831
|
+
let port = this.ports.get(module);
|
|
832
|
+
if (port === undefined) {
|
|
833
|
+
const name = isApiModule(module) ? null : (this.options.unported?.(module) ?? null);
|
|
834
|
+
port = name === null ? null : (this.unported.get(name) ?? { name, islands: 0, blocked: 0 });
|
|
835
|
+
if (port) this.unported.set(port.name, port);
|
|
836
|
+
this.ports.set(module, port);
|
|
837
|
+
}
|
|
838
|
+
return port;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* A component from a package with no Octane port renders in React, inside
|
|
843
|
+
* a `ReactCompat` island, when its element hands React nothing Octane
|
|
844
|
+
* renders. Its hooks and contexts cannot run in an Octane component, so
|
|
845
|
+
* they are reported.
|
|
846
|
+
*
|
|
847
|
+
* @param {ts.Identifier} node
|
|
848
|
+
*/
|
|
849
|
+
planUnported(node) {
|
|
850
|
+
const port = this.inImport(node) ? null : this.importOf(node)?.port;
|
|
851
|
+
if (!port) return;
|
|
852
|
+
// `Toast.Bar`, through a namespace or default import, is the member.
|
|
853
|
+
const member =
|
|
854
|
+
ts.isPropertyAccessExpression(node.parent) && node.parent.expression === node
|
|
855
|
+
? node.parent
|
|
856
|
+
: node;
|
|
857
|
+
const name = ts.isPropertyAccessExpression(member) ? member.name.text : node.text;
|
|
858
|
+
const text = member.getText(this.sf);
|
|
859
|
+
const parent = member.parent;
|
|
860
|
+
const block = (/** @type {string} */ message) => {
|
|
861
|
+
port.blocked++;
|
|
862
|
+
this.note(RULE.bindings, 'error', member, `${message} \`${port.name}\` has no Octane port.`);
|
|
863
|
+
};
|
|
864
|
+
if (
|
|
865
|
+
(ts.isJsxOpeningElement(parent) || ts.isJsxSelfClosingElement(parent)) &&
|
|
866
|
+
parent.tagName === member
|
|
867
|
+
) {
|
|
868
|
+
const element = ts.isJsxOpeningElement(parent) ? parent.parent : parent;
|
|
869
|
+
if (this.inIsland(element)) return;
|
|
870
|
+
if (!this.hostsInReact(element)) {
|
|
871
|
+
block(
|
|
872
|
+
`\`<${text}>\` takes children or props that Octane renders, which cannot cross into a React island.`,
|
|
873
|
+
);
|
|
874
|
+
} else if (!this.options.reactCompat) {
|
|
875
|
+
block(
|
|
876
|
+
`\`<${text}>\` can render in a \`ReactCompat\` island only with React and React DOM 19.2 or newer in the React 19 series.`,
|
|
877
|
+
);
|
|
878
|
+
} else {
|
|
879
|
+
port.islands++;
|
|
880
|
+
this.islands.add(element);
|
|
881
|
+
}
|
|
882
|
+
} else if (ts.isCallExpression(parent) && parent.expression === member) {
|
|
883
|
+
if (/^use[A-Z0-9]/.test(name))
|
|
884
|
+
block(`\`${text}\` is a React hook, which Octane cannot call.`);
|
|
885
|
+
} else if (
|
|
886
|
+
ts.isCallExpression(parent) &&
|
|
887
|
+
/^(?:use|useContext)$/.test(this.calleeName(parent.expression) ?? '')
|
|
888
|
+
) {
|
|
889
|
+
block(`\`${text}\` is a React context, which Octane cannot read.`);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
/**
|
|
894
|
+
* Whether a React component's element can be the root of a `ReactCompat`
|
|
895
|
+
* island: React renders all of it, so its children are text and no prop
|
|
896
|
+
* holds an element, as JSX here, through a value this module declares, or
|
|
897
|
+
* by its type.
|
|
898
|
+
*
|
|
899
|
+
* @param {ts.JsxElement | ts.JsxSelfClosingElement} element
|
|
900
|
+
*/
|
|
901
|
+
hostsInReact(element) {
|
|
902
|
+
// By type: a cast to `any` or `ReactNode` proves nothing.
|
|
903
|
+
const text = (/** @type {ts.JsxChild} */ child) =>
|
|
904
|
+
ts.isJsxText(child) ||
|
|
905
|
+
(ts.isJsxExpression(child) &&
|
|
906
|
+
(!child.expression || textKind(this.typeOf(child.expression)) !== 0));
|
|
907
|
+
if (ts.isJsxElement(element) && !element.children.every(text)) return false;
|
|
908
|
+
const seen = new Set();
|
|
909
|
+
/** @type {(node: ts.Node) => boolean} */
|
|
910
|
+
const rendersOctane = (node) => {
|
|
911
|
+
if (isJsx(node)) return true;
|
|
912
|
+
if (ts.isIdentifier(node)) {
|
|
913
|
+
const project = this.projectType(node);
|
|
914
|
+
const type = (project?.checker ?? this.checker).typeToString(
|
|
915
|
+
project?.type ?? this.checker.getTypeAtLocation(node),
|
|
916
|
+
undefined,
|
|
917
|
+
ts.TypeFormatFlags.NoTruncation,
|
|
918
|
+
);
|
|
919
|
+
if (/\b(?:React(?:Node|Element)|JSX\.Element)\b/.test(type)) return true;
|
|
920
|
+
const declaration = this.declarationOf(node);
|
|
921
|
+
if (declaration?.getSourceFile() === this.sf && !seen.has(declaration)) {
|
|
922
|
+
seen.add(declaration);
|
|
923
|
+
if (rendersOctane(declaration)) return true;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
return ts.forEachChild(node, rendersOctane) ?? false;
|
|
927
|
+
};
|
|
928
|
+
return !rendersOctane(
|
|
929
|
+
ts.isJsxElement(element) ? element.openingElement.attributes : element.attributes,
|
|
930
|
+
);
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
/**
|
|
934
|
+
* Whether an element is already the child of a `ReactCompat` island.
|
|
935
|
+
*
|
|
936
|
+
* @param {ts.JsxElement | ts.JsxSelfClosingElement} element
|
|
937
|
+
*/
|
|
938
|
+
inIsland(element) {
|
|
939
|
+
const parent = element.parent;
|
|
940
|
+
if (!ts.isJsxElement(parent)) return false;
|
|
941
|
+
const entry = this.importOf(parent.openingElement.tagName);
|
|
942
|
+
return entry?.module === 'octane/react' && entry.imported === 'ReactCompat';
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
/**
|
|
946
|
+
* Whether a callee reads its argument as an element: `cloneElement`,
|
|
947
|
+
* `isValidElement`, or a `Children` method.
|
|
948
|
+
*
|
|
949
|
+
* @param {ts.Expression} callee
|
|
950
|
+
*/
|
|
951
|
+
readsElement(callee) {
|
|
952
|
+
const name = this.calleeName(callee);
|
|
953
|
+
if (name === 'cloneElement' || name === 'isValidElement') return true;
|
|
954
|
+
return (
|
|
955
|
+
ts.isPropertyAccessExpression(callee) && this.calleeName(callee.expression) === 'Children'
|
|
956
|
+
);
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
/**
|
|
960
|
+
* React passes a component its JSX children as elements, which
|
|
961
|
+
* `render={children}` and `cloneElement(children)` rely on. Octane passes a
|
|
962
|
+
* compiled render body unless the component is marked `descriptorChildren`,
|
|
963
|
+
* so mark the component whose own `children` this reads.
|
|
964
|
+
*
|
|
965
|
+
* @param {ts.Expression} expression
|
|
966
|
+
*/
|
|
967
|
+
planDescriptorChildren(expression) {
|
|
968
|
+
let value = expression;
|
|
969
|
+
while (
|
|
970
|
+
ts.isParenthesizedExpression(value) ||
|
|
971
|
+
ts.isAsExpression(value) ||
|
|
972
|
+
ts.isSatisfiesExpression(value) ||
|
|
973
|
+
ts.isNonNullExpression(value) ||
|
|
974
|
+
ts.isTypeAssertionExpression(value)
|
|
975
|
+
) {
|
|
976
|
+
value = value.expression;
|
|
977
|
+
}
|
|
978
|
+
const component = this.childrenOwner(value);
|
|
979
|
+
if (!component) return;
|
|
980
|
+
// `memo(…)` and `forwardRef(…)` wrap it; the declaration holds the call.
|
|
981
|
+
let holder = component.parent;
|
|
982
|
+
while (
|
|
983
|
+
ts.isCallExpression(holder) &&
|
|
984
|
+
/^(?:memo|forwardRef)$/.test(this.calleeName(holder.expression) ?? '')
|
|
985
|
+
) {
|
|
986
|
+
holder = holder.parent;
|
|
987
|
+
}
|
|
988
|
+
if (ts.isCallExpression(holder) && this.calleeName(holder.expression) === 'descriptorChildren')
|
|
989
|
+
return;
|
|
990
|
+
// The compiler reads the marker from module-level `const` bindings only.
|
|
991
|
+
if (
|
|
992
|
+
ts.isVariableDeclaration(holder) &&
|
|
993
|
+
ts.isIdentifier(holder.name) &&
|
|
994
|
+
ts.getCombinedNodeFlags(holder) & ts.NodeFlags.Const &&
|
|
995
|
+
holder.parent.parent.parent === this.sf
|
|
996
|
+
) {
|
|
997
|
+
this.descriptorComponents.add(holder);
|
|
998
|
+
} else if (
|
|
999
|
+
ts.isFunctionDeclaration(component) &&
|
|
1000
|
+
component.parent === this.sf &&
|
|
1001
|
+
component.name &&
|
|
1002
|
+
!component.modifiers?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword) &&
|
|
1003
|
+
// Overload signatures would stay `function` declarations.
|
|
1004
|
+
this.checker.getSymbolAtLocation(component.name)?.declarations?.length === 1 &&
|
|
1005
|
+
this.declaredBeforeUse(component)
|
|
1006
|
+
) {
|
|
1007
|
+
this.descriptorComponents.add(component);
|
|
1008
|
+
} else {
|
|
1009
|
+
this.note(
|
|
1010
|
+
RULE.descriptorChildren,
|
|
1011
|
+
'warning',
|
|
1012
|
+
expression,
|
|
1013
|
+
'This reads `children` as an element, which Octane passes as a compiled render body; mark the component with `descriptorChildren` so callers pass element descriptors.',
|
|
1014
|
+
);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
/**
|
|
1019
|
+
* The component whose own `children` prop `node` is: `children` from its
|
|
1020
|
+
* props pattern, or `props.children`.
|
|
1021
|
+
*
|
|
1022
|
+
* @param {ts.Expression} node
|
|
1023
|
+
* @returns {ts.SignatureDeclaration | null}
|
|
1024
|
+
*/
|
|
1025
|
+
childrenOwner(node) {
|
|
1026
|
+
/** @type {ts.Node | null} */
|
|
1027
|
+
let props = null;
|
|
1028
|
+
if (ts.isPropertyAccessExpression(node) && node.name.text === 'children') {
|
|
1029
|
+
props = ts.isIdentifier(node.expression) ? this.declarationOf(node.expression) : null;
|
|
1030
|
+
} else if (ts.isIdentifier(node)) {
|
|
1031
|
+
const declaration = this.declarationOf(node);
|
|
1032
|
+
if (
|
|
1033
|
+
declaration &&
|
|
1034
|
+
ts.isBindingElement(declaration) &&
|
|
1035
|
+
(declaration.propertyName ?? declaration.name).getText(this.sf) === 'children'
|
|
1036
|
+
) {
|
|
1037
|
+
props = declaration.parent.parent;
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
if (!props || !ts.isParameter(props)) return null;
|
|
1041
|
+
const component = /** @type {ts.SignatureDeclaration} */ (props.parent);
|
|
1042
|
+
return component.parameters[0] === props && isFunctionLike(component) ? component : null;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
/**
|
|
1046
|
+
* Whether no code that runs while the module loads can reach `fn` before
|
|
1047
|
+
* its declaration, so it can become a `const`, which is not hoisted. Any
|
|
1048
|
+
* function there may run (an IIFE, a callback, a method an imported call
|
|
1049
|
+
* invokes) except one it only defines: a declaration or a variable's value.
|
|
1050
|
+
* Those run only when called or passed on, which counts as reaching `fn`
|
|
1051
|
+
* for any binding the module declares.
|
|
1052
|
+
*
|
|
1053
|
+
* @param {ts.FunctionDeclaration} fn
|
|
1054
|
+
*/
|
|
1055
|
+
declaredBeforeUse(fn) {
|
|
1056
|
+
let safe = true;
|
|
1057
|
+
const visit = (/** @type {ts.Node} */ node) => {
|
|
1058
|
+
if (!safe) return;
|
|
1059
|
+
if (isFunctionLike(node) && this.onlyDefines(node)) return;
|
|
1060
|
+
if (ts.isIdentifier(node) && node !== fn.name) {
|
|
1061
|
+
const declaration = this.declarationOf(node);
|
|
1062
|
+
const call = node.parent;
|
|
1063
|
+
const handed =
|
|
1064
|
+
(ts.isCallExpression(call) || ts.isNewExpression(call)) &&
|
|
1065
|
+
(call.expression === node || call.arguments?.includes(node));
|
|
1066
|
+
if (
|
|
1067
|
+
declaration === fn ||
|
|
1068
|
+
(handed && declaration?.getSourceFile() === this.sf && !this.importOf(node))
|
|
1069
|
+
) {
|
|
1070
|
+
safe = false;
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
ts.forEachChild(node, visit);
|
|
1074
|
+
};
|
|
1075
|
+
for (const statement of this.sf.statements) {
|
|
1076
|
+
if (statement === fn) break;
|
|
1077
|
+
visit(statement);
|
|
1078
|
+
}
|
|
1079
|
+
return safe;
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
/**
|
|
1083
|
+
* Whether a function is only defined where it stands: a declaration, or a
|
|
1084
|
+
* variable's value, also through `memo` or `forwardRef`, which do not call it.
|
|
1085
|
+
*
|
|
1086
|
+
* @param {ts.Node} fn
|
|
1087
|
+
*/
|
|
1088
|
+
onlyDefines(fn) {
|
|
1089
|
+
if (ts.isFunctionDeclaration(fn)) return true;
|
|
1090
|
+
let parent = fn.parent;
|
|
1091
|
+
while (
|
|
1092
|
+
ts.isParenthesizedExpression(parent) ||
|
|
1093
|
+
(ts.isCallExpression(parent) &&
|
|
1094
|
+
/^(?:memo|forwardRef)$/.test(this.calleeName(parent.expression) ?? ''))
|
|
1095
|
+
) {
|
|
1096
|
+
parent = parent.parent;
|
|
1097
|
+
}
|
|
1098
|
+
return ts.isVariableDeclaration(parent);
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
/**
|
|
1102
|
+
* Whether an `onChange` a spread passes this element fires per edit in
|
|
1103
|
+
* React: a text field with no `onChange` or `onInput` of its own.
|
|
1104
|
+
*
|
|
1105
|
+
* @param {ts.JsxOpeningLikeElement} element
|
|
1106
|
+
*/
|
|
1107
|
+
takesChange(element) {
|
|
1108
|
+
if (!/^(?:input|textarea)$/.test(element.tagName.getText(this.sf))) return false;
|
|
1109
|
+
if (attributeOf(element, 'onChange') || attributeOf(element, 'onInput')) return false;
|
|
1110
|
+
const type = attributeOf(element, 'type')?.initializer;
|
|
1111
|
+
return !type || !ts.isStringLiteral(type) || TEXT_INPUT_TYPES.has(type.text);
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
/**
|
|
1115
|
+
* A text field that spreads a component's rest props passes on any
|
|
1116
|
+
* `onChange` in them, which React fires per edit and Octane on commit. Take
|
|
1117
|
+
* it out of the rest and pass it as `onInput`, before the spread, so a
|
|
1118
|
+
* caller's own `onInput` still wins. Only when the rest is read nowhere
|
|
1119
|
+
* else, since those reads would lose it.
|
|
1120
|
+
*
|
|
1121
|
+
* @param {ts.JsxOpeningLikeElement} field
|
|
1122
|
+
*/
|
|
1123
|
+
planRestChange(field) {
|
|
1124
|
+
for (const spread of field.attributes.properties) {
|
|
1125
|
+
if (!ts.isJsxSpreadAttribute(spread)) continue;
|
|
1126
|
+
// Props whose type has no `onChange` have none to pass on.
|
|
1127
|
+
const type = this.projectType(spread.expression)?.type;
|
|
1128
|
+
if (type && !(type.flags & ts.TypeFlags.Any) && !type.getProperty('onChange')) continue;
|
|
1129
|
+
const declaration = ts.isIdentifier(spread.expression)
|
|
1130
|
+
? this.declarationOf(spread.expression)
|
|
1131
|
+
: null;
|
|
1132
|
+
const rest =
|
|
1133
|
+
declaration &&
|
|
1134
|
+
ts.isBindingElement(declaration) &&
|
|
1135
|
+
declaration.dotDotDotToken &&
|
|
1136
|
+
ts.isObjectBindingPattern(declaration.parent)
|
|
1137
|
+
? declaration
|
|
1138
|
+
: null;
|
|
1139
|
+
// A pattern that names `onChange` leaves none in the rest.
|
|
1140
|
+
const pattern = /** @type {ts.ObjectBindingPattern | undefined} */ (rest?.parent);
|
|
1141
|
+
if (
|
|
1142
|
+
pattern?.elements.some((e) => (e.propertyName ?? e.name).getText(this.sf) === 'onChange')
|
|
1143
|
+
) {
|
|
1144
|
+
continue;
|
|
1145
|
+
}
|
|
1146
|
+
if (
|
|
1147
|
+
rest &&
|
|
1148
|
+
!this.checker.resolveName('onChange', spread, ts.SymbolFlags.Value, false) &&
|
|
1149
|
+
this.onlySpreadOntoFields(rest)
|
|
1150
|
+
) {
|
|
1151
|
+
this.restChange.add(rest);
|
|
1152
|
+
this.changeSpreads.add(spread);
|
|
1153
|
+
} else {
|
|
1154
|
+
this.note(
|
|
1155
|
+
RULE.nativeChange,
|
|
1156
|
+
'warning',
|
|
1157
|
+
spread,
|
|
1158
|
+
`An \`onChange\` in this spread fires on commit (blur) in Octane, not per edit; pass it to the \`<${field.tagName.getText(this.sf)}>\` as \`onInput\`.`,
|
|
1159
|
+
);
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
/**
|
|
1165
|
+
* Whether no read of a rest binding but its spreads onto text fields can
|
|
1166
|
+
* see an `onChange` in it: other reads name another member (`props.type`).
|
|
1167
|
+
*
|
|
1168
|
+
* @param {ts.BindingElement} rest
|
|
1169
|
+
*/
|
|
1170
|
+
onlySpreadOntoFields(rest) {
|
|
1171
|
+
let only = true;
|
|
1172
|
+
const visit = (/** @type {ts.Node} */ node) => {
|
|
1173
|
+
if (!only) return;
|
|
1174
|
+
if (ts.isIdentifier(node) && node !== rest.name && this.declarationOf(node) === rest) {
|
|
1175
|
+
const parent = node.parent;
|
|
1176
|
+
only = ts.isJsxSpreadAttribute(parent)
|
|
1177
|
+
? this.takesChange(parent.parent.parent)
|
|
1178
|
+
: ts.isPropertyAccessExpression(parent) &&
|
|
1179
|
+
parent.expression === node &&
|
|
1180
|
+
parent.name.text !== 'onChange';
|
|
1181
|
+
}
|
|
1182
|
+
ts.forEachChild(node, visit);
|
|
1183
|
+
};
|
|
1184
|
+
visit(ts.findAncestor(rest, ts.isFunctionLike) ?? this.sf);
|
|
1185
|
+
return only;
|
|
706
1186
|
}
|
|
707
1187
|
|
|
708
1188
|
/**
|
|
@@ -951,7 +1431,19 @@ class ModuleTransform {
|
|
|
951
1431
|
this.checkDefaultProps(node);
|
|
952
1432
|
}
|
|
953
1433
|
if (ts.isModuleDeclaration(node) && ts.isStringLiteral(node.name)) {
|
|
954
|
-
return
|
|
1434
|
+
return node.name.text in REACT_MODULES
|
|
1435
|
+
? this.rewriteAugmentation(node, node.name)
|
|
1436
|
+
: this.rewriteSpecifier(node, node.name);
|
|
1437
|
+
}
|
|
1438
|
+
if (ts.isBindingElement(node) && this.restChange.has(node)) {
|
|
1439
|
+
return 'onChange, ' + node.getText(this.sf);
|
|
1440
|
+
}
|
|
1441
|
+
if (ts.isVariableDeclaration(node) && this.descriptorComponents.has(node) && node.initializer) {
|
|
1442
|
+
this.count(RULE.descriptorChildren);
|
|
1443
|
+
return (
|
|
1444
|
+
this.span(node, node.getStart(this.sf), node.initializer.getStart(this.sf), PLAIN) +
|
|
1445
|
+
`${this.require('octane', 'descriptorChildren', false)}(${this.emit(node.initializer, PLAIN)})`
|
|
1446
|
+
);
|
|
955
1447
|
}
|
|
956
1448
|
if (ts.isJsxText(node) && this.target === 'tsrx' && /\/[/*]/.test(node.text)) {
|
|
957
1449
|
// TSRX reads `//` and `/*` in element text as comments, as JavaScript does.
|
|
@@ -971,6 +1463,11 @@ class ModuleTransform {
|
|
|
971
1463
|
return this.require('@octanejs/tanstack-router', DOCUMENT_TAGS[node.text], false);
|
|
972
1464
|
}
|
|
973
1465
|
if (ts.isJsxAttributes(node)) return this.rewriteAttributes(node);
|
|
1466
|
+
if ((ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node)) && this.islands.has(node)) {
|
|
1467
|
+
this.count(RULE.reactCompat);
|
|
1468
|
+
const island = this.require('octane/react', 'ReactCompat', false);
|
|
1469
|
+
return `<${island}>${this.span(node, node.getStart(this.sf), node.end, PLAIN)}</${island}>`;
|
|
1470
|
+
}
|
|
974
1471
|
if (ts.isJsxElement(node)) return this.rewriteElement(node, ctx);
|
|
975
1472
|
if (ts.isJsxExpression(node) && ctx.template) return this.rewriteHole(node);
|
|
976
1473
|
if (
|
|
@@ -979,8 +1476,20 @@ class ModuleTransform {
|
|
|
979
1476
|
ts.isArrowFunction(node)
|
|
980
1477
|
) {
|
|
981
1478
|
const component = this.target === 'tsrx' ? this.rewriteComponent(node) : null;
|
|
982
|
-
|
|
983
|
-
|
|
1479
|
+
const text =
|
|
1480
|
+
component ??
|
|
1481
|
+
(this.parameters.has(node) && node.body
|
|
1482
|
+
? this.head(node, node.body.getStart(this.sf)) + this.emit(node.body, PLAIN)
|
|
1483
|
+
: null);
|
|
1484
|
+
if (!ts.isFunctionDeclaration(node) || !this.descriptorComponents.has(node)) return text;
|
|
1485
|
+
// A marked binding is a `const`; the function keeps its name.
|
|
1486
|
+
this.count(RULE.descriptorChildren);
|
|
1487
|
+
const fn = (text ?? this.span(node, node.getStart(this.sf), node.end, ctx)).replace(
|
|
1488
|
+
/^export\s+/,
|
|
1489
|
+
'',
|
|
1490
|
+
);
|
|
1491
|
+
const exported = node.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
1492
|
+
return `${exported ? 'export ' : ''}const ${node.name?.text} = ${this.require('octane', 'descriptorChildren', false)}(${fn});`;
|
|
984
1493
|
}
|
|
985
1494
|
return null;
|
|
986
1495
|
}
|
|
@@ -1003,7 +1512,112 @@ class ModuleTransform {
|
|
|
1003
1512
|
this.importSlots.push(node);
|
|
1004
1513
|
return `\u0000IMPORT${this.importSlots.length - 1}\u0000`;
|
|
1005
1514
|
}
|
|
1006
|
-
|
|
1515
|
+
const text =
|
|
1516
|
+
this.rewritePackageNames(node, module) ?? this.rewriteSpecifier(node, node.moduleSpecifier);
|
|
1517
|
+
// Analysis recorded the packages this module imports values from.
|
|
1518
|
+
const port = node.importClause?.isTypeOnly === false ? (this.ports.get(module) ?? null) : null;
|
|
1519
|
+
if (
|
|
1520
|
+
!port ||
|
|
1521
|
+
ts
|
|
1522
|
+
.getLeadingCommentRanges(this.source, node.pos)
|
|
1523
|
+
?.some((range) => this.source.slice(range.pos, range.end).includes('No Octane port of'))
|
|
1524
|
+
)
|
|
1525
|
+
return text;
|
|
1526
|
+
return `// ${portComment(port)}\n${text ?? node.getText(this.sf)}`;
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
/**
|
|
1530
|
+
* An import or re-export of a ported package whose port spells some of its
|
|
1531
|
+
* names differently. A Radix package is a namespace of `@octanejs/radix`, so
|
|
1532
|
+
* its namespace import becomes that namespace and its named imports read
|
|
1533
|
+
* from it. Names the port lacks stay on the React package, for a person.
|
|
1534
|
+
*
|
|
1535
|
+
* @param {ts.ImportDeclaration | ts.ExportDeclaration} node
|
|
1536
|
+
* @param {string} module
|
|
1537
|
+
*/
|
|
1538
|
+
rewritePackageNames(node, module) {
|
|
1539
|
+
const clause = ts.isImportDeclaration(node) ? node.importClause : node;
|
|
1540
|
+
const namespace = radixNamespace(module);
|
|
1541
|
+
const next = this.resolve(module);
|
|
1542
|
+
// A default import; and a Radix package's re-export, which rewriteSpecifier reports.
|
|
1543
|
+
if (!clause || next === null || (ts.isImportClause(clause) ? clause.name : namespace)) {
|
|
1544
|
+
return null;
|
|
1545
|
+
}
|
|
1546
|
+
const keyword = ts.isImportClause(clause) ? 'import' : 'export';
|
|
1547
|
+
const quote =
|
|
1548
|
+
this.source[/** @type {ts.Expression} */ (node.moduleSpecifier).getStart(this.sf)];
|
|
1549
|
+
const from = (/** @type {string} */ specifier) => `from ${quote}${specifier}${quote};`;
|
|
1550
|
+
const type = clause.isTypeOnly ? 'type ' : '';
|
|
1551
|
+
const bindings = ts.isImportClause(clause) ? clause.namedBindings : clause.exportClause;
|
|
1552
|
+
if (bindings && ts.isNamespaceImport(bindings)) {
|
|
1553
|
+
if (namespace === null) return null;
|
|
1554
|
+
const local = bindings.name.text;
|
|
1555
|
+
this.count(RULE.import);
|
|
1556
|
+
return `import ${type}{ ${local === namespace ? local : `${namespace} as ${local}`} } ${from(next)}`;
|
|
1557
|
+
}
|
|
1558
|
+
if (!bindings || ts.isNamespaceExport(bindings)) return null;
|
|
1559
|
+
/** @type {string[]} */
|
|
1560
|
+
const kept = [];
|
|
1561
|
+
/** @type {string[]} */
|
|
1562
|
+
const values = [];
|
|
1563
|
+
/** @type {string[]} */
|
|
1564
|
+
const types = [];
|
|
1565
|
+
let renamed = false;
|
|
1566
|
+
for (const element of bindings.elements) {
|
|
1567
|
+
const imported = (element.propertyName ?? element.name).text;
|
|
1568
|
+
const local = element.name.text;
|
|
1569
|
+
const name = mapPackageExport(module, imported);
|
|
1570
|
+
if (name === null) {
|
|
1571
|
+
this.note(RULE.binding, 'error', element, missingExport(module, imported));
|
|
1572
|
+
kept.push(element.getText(this.sf));
|
|
1573
|
+
continue;
|
|
1574
|
+
}
|
|
1575
|
+
renamed ||= name !== imported;
|
|
1576
|
+
if (namespace === null) {
|
|
1577
|
+
values.push(
|
|
1578
|
+
`${element.isTypeOnly ? 'type ' : ''}${name === local ? name : `${name} as ${local}`}`,
|
|
1579
|
+
);
|
|
1580
|
+
} else if (RADIX_TYPES.has(name)) {
|
|
1581
|
+
types.push(`type ${local} = ${this.require(next, namespace, true)}.${name};`);
|
|
1582
|
+
} else {
|
|
1583
|
+
values.push(name === local ? name : `${name}: ${local}`);
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
if (namespace === null && kept.length === 0 && !renamed) return null;
|
|
1587
|
+
this.count(RULE.import);
|
|
1588
|
+
const lines =
|
|
1589
|
+
kept.length > 0 ? [`${keyword} ${type}{ ${kept.join(', ')} } ${from(module)}`] : [];
|
|
1590
|
+
if (values.length > 0) {
|
|
1591
|
+
lines.push(
|
|
1592
|
+
namespace === null
|
|
1593
|
+
? `${keyword} ${type}{ ${values.join(', ')} } ${from(next)}`
|
|
1594
|
+
: `const { ${values.join(', ')} } = ${this.require(next, namespace, false)};`,
|
|
1595
|
+
);
|
|
1596
|
+
}
|
|
1597
|
+
return [...lines, ...types].join('\n');
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
/**
|
|
1601
|
+
* `DialogPrimitive.DialogTitle`, a member of a ported package's namespace
|
|
1602
|
+
* import, as its port spells it.
|
|
1603
|
+
*
|
|
1604
|
+
* @param {ts.Node} namespace
|
|
1605
|
+
* @param {ts.MemberName} member
|
|
1606
|
+
* @param {Severity} severity of a member the port lacks
|
|
1607
|
+
*/
|
|
1608
|
+
rewritePackageMember(namespace, member, severity) {
|
|
1609
|
+
const entry = this.importOf(namespace);
|
|
1610
|
+
if (!entry?.namespace) return null;
|
|
1611
|
+
const name = mapPackageExport(entry.module, member.text);
|
|
1612
|
+
if (name === member.text || this.resolve(entry.module) === null) return null;
|
|
1613
|
+
if (name === null) {
|
|
1614
|
+
if (!ts.isJsxClosingElement(member.parent.parent)) {
|
|
1615
|
+
this.note(RULE.binding, severity, member, missingExport(entry.module, member.text));
|
|
1616
|
+
}
|
|
1617
|
+
return null;
|
|
1618
|
+
}
|
|
1619
|
+
this.count(RULE.import);
|
|
1620
|
+
return `${entry.local}.${name}`;
|
|
1007
1621
|
}
|
|
1008
1622
|
|
|
1009
1623
|
/** @param {ts.ExportDeclaration} node */
|
|
@@ -1018,7 +1632,10 @@ class ModuleTransform {
|
|
|
1018
1632
|
mapped,
|
|
1019
1633
|
);
|
|
1020
1634
|
}
|
|
1021
|
-
return
|
|
1635
|
+
return (
|
|
1636
|
+
this.rewritePackageNames(node, module) ??
|
|
1637
|
+
this.rewriteSpecifier(node, /** @type {ts.Expression} */ (node.moduleSpecifier))
|
|
1638
|
+
);
|
|
1022
1639
|
}
|
|
1023
1640
|
|
|
1024
1641
|
/**
|
|
@@ -1027,12 +1644,64 @@ class ModuleTransform {
|
|
|
1027
1644
|
*/
|
|
1028
1645
|
rewriteSpecifier(node, specifier) {
|
|
1029
1646
|
if (!ts.isStringLiteral(specifier)) return null;
|
|
1030
|
-
const next = this.
|
|
1647
|
+
const next = this.resolve(specifier.text);
|
|
1031
1648
|
if (next === null || next === specifier.text) return null;
|
|
1649
|
+
const namespace = radixNamespace(specifier.text);
|
|
1650
|
+
if (namespace !== null) {
|
|
1651
|
+
// A re-export, `import()`, or type of the package would be the whole binding.
|
|
1652
|
+
this.note(
|
|
1653
|
+
RULE.binding,
|
|
1654
|
+
'error',
|
|
1655
|
+
node,
|
|
1656
|
+
`\`@octanejs/radix\` serves \`${specifier.text}\` as its \`${namespace}\` namespace; read it from \`import { ${namespace} } from '@octanejs/radix'\`.`,
|
|
1657
|
+
);
|
|
1658
|
+
return null;
|
|
1659
|
+
}
|
|
1032
1660
|
this.count(RULE.import);
|
|
1033
1661
|
return this.replaceSpecifier(node, specifier, next);
|
|
1034
1662
|
}
|
|
1035
1663
|
|
|
1664
|
+
/**
|
|
1665
|
+
* The new spelling of an import specifier, or null to keep it.
|
|
1666
|
+
*
|
|
1667
|
+
* @param {string} specifier
|
|
1668
|
+
*/
|
|
1669
|
+
resolve(specifier) {
|
|
1670
|
+
let next = this.resolved.get(specifier);
|
|
1671
|
+
if (next === undefined) {
|
|
1672
|
+
next = this.options.resolveSpecifier?.(specifier) ?? null;
|
|
1673
|
+
this.resolved.set(specifier, next);
|
|
1674
|
+
}
|
|
1675
|
+
return next;
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
/**
|
|
1679
|
+
* `declare module 'react' { namespace JSX { … } }` types custom elements,
|
|
1680
|
+
* which Octane reads from `octane/jsx-runtime`'s JSX namespace. Anything
|
|
1681
|
+
* else it adds patches React's own types, which Octane does not use.
|
|
1682
|
+
*
|
|
1683
|
+
* @param {ts.ModuleDeclaration} node
|
|
1684
|
+
* @param {ts.StringLiteral} specifier
|
|
1685
|
+
*/
|
|
1686
|
+
rewriteAugmentation(node, specifier) {
|
|
1687
|
+
const statements = node.body && ts.isModuleBlock(node.body) ? node.body.statements : [];
|
|
1688
|
+
if (
|
|
1689
|
+
statements.length > 0 &&
|
|
1690
|
+
statements.every((s) => ts.isModuleDeclaration(s) && s.name.getText(this.sf) === 'JSX')
|
|
1691
|
+
) {
|
|
1692
|
+
this.count(RULE.import);
|
|
1693
|
+
this.augments = true;
|
|
1694
|
+
return this.replaceSpecifier(node, specifier, 'octane/jsx-runtime');
|
|
1695
|
+
}
|
|
1696
|
+
this.note(
|
|
1697
|
+
RULE.unsupported,
|
|
1698
|
+
'warning',
|
|
1699
|
+
node,
|
|
1700
|
+
`This augments React's own types, which Octane does not read; add what it declares to Octane's.`,
|
|
1701
|
+
);
|
|
1702
|
+
return null;
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1036
1705
|
/**
|
|
1037
1706
|
* @param {ts.Node} node
|
|
1038
1707
|
* @param {ts.Expression} specifier
|
|
@@ -1151,10 +1820,11 @@ class ModuleTransform {
|
|
|
1151
1820
|
|
|
1152
1821
|
/** @type {string[]} */
|
|
1153
1822
|
const lines = [];
|
|
1823
|
+
const quote = (/** @type {string} */ module) => this.quote + module + this.quote;
|
|
1154
1824
|
for (const [module, names] of imports) {
|
|
1155
1825
|
const specs = [...names.values()];
|
|
1156
1826
|
for (const { name, local } of specs) {
|
|
1157
|
-
if (name === '*') lines.push(`import * as ${local} from
|
|
1827
|
+
if (name === '*') lines.push(`import * as ${local} from ${quote(module)};`);
|
|
1158
1828
|
}
|
|
1159
1829
|
const named = specs.filter(({ name }) => name !== '*');
|
|
1160
1830
|
if (named.length === 0) continue;
|
|
@@ -1163,11 +1833,21 @@ class ModuleTransform {
|
|
|
1163
1833
|
({ name, local, typeOnly }) =>
|
|
1164
1834
|
(typeOnly && !allTypes ? 'type ' : '') + (name === local ? name : `${name} as ${local}`),
|
|
1165
1835
|
);
|
|
1166
|
-
lines.push(`import ${allTypes ? 'type ' : ''}{ ${list.join(', ')} } from
|
|
1836
|
+
lines.push(`import ${allTypes ? 'type ' : ''}{ ${list.join(', ')} } from ${quote(module)};`);
|
|
1167
1837
|
}
|
|
1168
1838
|
for (const [module, specs] of kept)
|
|
1169
|
-
lines.push(`import { ${specs.join(', ')} } from
|
|
1839
|
+
lines.push(`import { ${specs.join(', ')} } from ${quote(module)};`);
|
|
1170
1840
|
|
|
1841
|
+
// In a script, `declare module` declares the module instead of adding to
|
|
1842
|
+
// it: a module whose only import was `import 'react'` stays a module, and
|
|
1843
|
+
// a script with a retargeted augmentation becomes one.
|
|
1844
|
+
if (
|
|
1845
|
+
lines.length === 0 &&
|
|
1846
|
+
(this.importSlots.length > 0 || this.augments) &&
|
|
1847
|
+
!this.otherModuleSyntax()
|
|
1848
|
+
) {
|
|
1849
|
+
lines.push('export {};');
|
|
1850
|
+
}
|
|
1171
1851
|
if (this.importSlots.length === 0) {
|
|
1172
1852
|
if (lines.length === 0) return code;
|
|
1173
1853
|
const at = this.insertionPoint(code);
|
|
@@ -1179,6 +1859,19 @@ class ModuleTransform {
|
|
|
1179
1859
|
);
|
|
1180
1860
|
}
|
|
1181
1861
|
|
|
1862
|
+
/** Whether the module has an import or export besides its React imports. */
|
|
1863
|
+
otherModuleSyntax() {
|
|
1864
|
+
return this.sf.statements.some(
|
|
1865
|
+
(statement) =>
|
|
1866
|
+
(ts.isImportDeclaration(statement) && !this.importSlots.includes(statement)) ||
|
|
1867
|
+
ts.isImportEqualsDeclaration(statement) ||
|
|
1868
|
+
ts.isExportDeclaration(statement) ||
|
|
1869
|
+
ts.isExportAssignment(statement) ||
|
|
1870
|
+
(ts.canHaveModifiers(statement) &&
|
|
1871
|
+
ts.getModifiers(statement)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)),
|
|
1872
|
+
);
|
|
1873
|
+
}
|
|
1874
|
+
|
|
1182
1875
|
/**
|
|
1183
1876
|
* Where new imports go when the module imported nothing from React: after
|
|
1184
1877
|
* the directive prologue and any leading comments.
|
|
@@ -1188,9 +1881,10 @@ class ModuleTransform {
|
|
|
1188
1881
|
insertionPoint(code) {
|
|
1189
1882
|
// Whole lines only: a shebang, blank lines, comments that end their
|
|
1190
1883
|
// line, and directive lines. Anything inserted here starts a line, so
|
|
1191
|
-
// the Strong fallback can find its own directive line again.
|
|
1884
|
+
// the Strong fallback can find its own directive line again. The
|
|
1885
|
+
// comment on an unported package's import stays with it.
|
|
1192
1886
|
const prologue =
|
|
1193
|
-
/^(?:#![^\n]*\n)?(?:[ \t]*(?:\/\/[^\n]*|\/\*[\s\S]*?\*\/[ \t]*|(['"])use [a-z ]+\1;?[^\n]*)?\r?\n)*/.exec(
|
|
1887
|
+
/^(?:#![^\n]*\n)?(?:[ \t]*(?:\/\/(?! No Octane port of)[^\n]*|\/\*[\s\S]*?\*\/[ \t]*|(['"])use [a-z ]+\1;?[^\n]*)?\r?\n)*/.exec(
|
|
1194
1888
|
code,
|
|
1195
1889
|
);
|
|
1196
1890
|
return prologue ? prologue[0].length : 0;
|
|
@@ -1248,6 +1942,8 @@ class ModuleTransform {
|
|
|
1248
1942
|
this.count(RULE.import);
|
|
1249
1943
|
return this.require(mapped.module, mapped.name, false);
|
|
1250
1944
|
}
|
|
1945
|
+
const member = this.rewritePackageMember(node.expression, node.name, 'error');
|
|
1946
|
+
if (member !== null) return member;
|
|
1251
1947
|
// event.nativeEvent: Octane events are already native.
|
|
1252
1948
|
if (node.name.text === 'nativeEvent' && this.isEventParameter(node.expression)) {
|
|
1253
1949
|
this.count(RULE.eventObject);
|
|
@@ -1260,13 +1956,36 @@ class ModuleTransform {
|
|
|
1260
1956
|
return null;
|
|
1261
1957
|
}
|
|
1262
1958
|
|
|
1959
|
+
/**
|
|
1960
|
+
* Whether `node` names a global namespace React's types declare, such as
|
|
1961
|
+
* `React` in `React.ReactNode` written without an import.
|
|
1962
|
+
*
|
|
1963
|
+
* @param {ts.EntityName} node
|
|
1964
|
+
* @param {string} name
|
|
1965
|
+
*/
|
|
1966
|
+
isGlobalNamespace(node, name) {
|
|
1967
|
+
return (
|
|
1968
|
+
ts.isIdentifier(node) &&
|
|
1969
|
+
node.text === name &&
|
|
1970
|
+
!this.checker.resolveName(name, node, ts.SymbolFlags.Namespace, false)
|
|
1971
|
+
);
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1263
1974
|
/** @param {ts.QualifiedName} node */
|
|
1264
1975
|
rewriteQualifiedType(node) {
|
|
1976
|
+
const member = this.rewritePackageMember(node.left, node.right, 'warning');
|
|
1977
|
+
if (member !== null) return member;
|
|
1265
1978
|
const namespace = this.importOf(node.left);
|
|
1266
|
-
if (
|
|
1979
|
+
if (
|
|
1980
|
+
namespace
|
|
1981
|
+
? !namespace.namespace || !(namespace.module in REACT_MODULES)
|
|
1982
|
+
: !this.isGlobalNamespace(node.left, 'React')
|
|
1983
|
+
) {
|
|
1984
|
+
return null;
|
|
1985
|
+
}
|
|
1267
1986
|
const name = mapQualifiedType(node.right.text);
|
|
1268
1987
|
if (!name) {
|
|
1269
|
-
this.used.add(namespace.local);
|
|
1988
|
+
if (namespace) this.used.add(namespace.local);
|
|
1270
1989
|
this.note(
|
|
1271
1990
|
RULE.unsupported,
|
|
1272
1991
|
'warning',
|
|
@@ -1283,12 +2002,7 @@ class ModuleTransform {
|
|
|
1283
2002
|
rewriteTypeReference(node) {
|
|
1284
2003
|
// The global `JSX` namespace React's types declared is Octane's export.
|
|
1285
2004
|
const name = node.typeName;
|
|
1286
|
-
if (
|
|
1287
|
-
ts.isQualifiedName(name) &&
|
|
1288
|
-
ts.isIdentifier(name.left) &&
|
|
1289
|
-
name.left.text === 'JSX' &&
|
|
1290
|
-
!this.checker.resolveName('JSX', node, ts.SymbolFlags.Namespace, false)
|
|
1291
|
-
) {
|
|
2005
|
+
if (ts.isQualifiedName(name) && this.isGlobalNamespace(name.left, 'JSX')) {
|
|
1292
2006
|
this.require('octane', 'JSX', true);
|
|
1293
2007
|
}
|
|
1294
2008
|
return null;
|
|
@@ -1398,9 +2112,9 @@ class ModuleTransform {
|
|
|
1398
2112
|
this.count(RULE.forwardRef);
|
|
1399
2113
|
const [props, ref] = render.parameters;
|
|
1400
2114
|
const typeArgs = node.typeArguments;
|
|
1401
|
-
const refType = typeArgs?.[0]
|
|
1402
|
-
const
|
|
1403
|
-
|
|
2115
|
+
const refType = typeArgs?.[0] ? this.emit(typeArgs[0], PLAIN) : this.refTypeOf(ref);
|
|
2116
|
+
const propsNode = typeArgs?.[1] ?? props?.type;
|
|
2117
|
+
const propsType = propsNode ? this.emit(propsNode, PLAIN) : null;
|
|
1404
2118
|
const refName = ref && ts.isIdentifier(ref.name) ? ref.name.text : null;
|
|
1405
2119
|
const refBinding = refName === null ? null : refName === 'ref' ? 'ref' : `ref: ${refName}`;
|
|
1406
2120
|
|
|
@@ -1478,7 +2192,7 @@ class ModuleTransform {
|
|
|
1478
2192
|
refTypeOf(ref) {
|
|
1479
2193
|
const type = ref?.type;
|
|
1480
2194
|
if (type && ts.isTypeReferenceNode(type) && type.typeArguments?.length === 1) {
|
|
1481
|
-
return type.typeArguments[0]
|
|
2195
|
+
return this.emit(type.typeArguments[0], PLAIN);
|
|
1482
2196
|
}
|
|
1483
2197
|
return null;
|
|
1484
2198
|
}
|
|
@@ -1660,11 +2374,19 @@ class ModuleTransform {
|
|
|
1660
2374
|
targetsControl(node) {
|
|
1661
2375
|
const found = this.parameterOf(node);
|
|
1662
2376
|
if (!found) return false;
|
|
1663
|
-
|
|
1664
|
-
|
|
2377
|
+
// Unannotated, the project's types still name a handler a component
|
|
2378
|
+
// passes on to a control: `<Input onChange={(e) => …}>`.
|
|
2379
|
+
const inferred = found.param.type ? null : this.projectType(found.param);
|
|
2380
|
+
const type =
|
|
2381
|
+
found.param.type?.getText(this.sf) ??
|
|
2382
|
+
(inferred ? inferred.checker.typeToString(inferred.type) : '');
|
|
2383
|
+
if (
|
|
2384
|
+
/(ChangeEvent|FormEvent)<\s*(?:HTML(?:Input|TextArea|Select)Element\s*[|,]?\s*)+>$/.test(type)
|
|
2385
|
+
)
|
|
1665
2386
|
return true;
|
|
1666
2387
|
const attribute = this.handlerAttribute(found.fn);
|
|
1667
|
-
if (!attribute || !/^on(Change|Input)/.test(attribute.name.getText(this.sf)))
|
|
2388
|
+
if (!attribute || !/^on(Change|Input|Focus|Blur)/.test(attribute.name.getText(this.sf)))
|
|
2389
|
+
return false;
|
|
1668
2390
|
const tag = attribute.parent.parent.tagName.getText(this.sf);
|
|
1669
2391
|
return tag === 'input' || tag === 'textarea' || tag === 'select';
|
|
1670
2392
|
}
|
|
@@ -1716,8 +2438,6 @@ class ModuleTransform {
|
|
|
1716
2438
|
const intrinsic = /^[a-z]/.test(tag);
|
|
1717
2439
|
const spread = node.properties.some(ts.isJsxSpreadAttribute);
|
|
1718
2440
|
const attributes = node.properties;
|
|
1719
|
-
const named = (/** @type {string} */ name) =>
|
|
1720
|
-
attributes.find((a) => ts.isJsxAttribute(a) && a.name.getText(this.sf) === name);
|
|
1721
2441
|
|
|
1722
2442
|
let out = '';
|
|
1723
2443
|
let cursor = node.getStart(this.sf);
|
|
@@ -1739,17 +2459,28 @@ class ModuleTransform {
|
|
|
1739
2459
|
}
|
|
1740
2460
|
out += this.source.slice(cursor, start);
|
|
1741
2461
|
cursor = attribute.end;
|
|
2462
|
+
if (ts.isJsxSpreadAttribute(attribute) && this.changeSpreads.has(attribute)) {
|
|
2463
|
+
// On the spread's own line when it has one.
|
|
2464
|
+
this.count(RULE.nativeChange);
|
|
2465
|
+
const line = this.source.slice(this.source.lastIndexOf('\n', start - 1), start);
|
|
2466
|
+
out += 'onInput={onChange}' + (/^\n[ \t]*$/.test(line) ? line : ' ');
|
|
2467
|
+
}
|
|
1742
2468
|
if (!ts.isJsxAttribute(attribute) || !intrinsic) {
|
|
1743
2469
|
out += this.emit(attribute, PLAIN);
|
|
1744
2470
|
continue;
|
|
1745
2471
|
}
|
|
1746
2472
|
const name = attribute.name.getText(this.sf);
|
|
1747
|
-
if (
|
|
2473
|
+
if (
|
|
2474
|
+
name === 'className' &&
|
|
2475
|
+
this.target === 'tsrx' &&
|
|
2476
|
+
!attributeOf(element, 'class') &&
|
|
2477
|
+
!spread
|
|
2478
|
+
) {
|
|
1748
2479
|
this.count(RULE.className);
|
|
1749
2480
|
out += 'class' + this.emitInitializer(attribute);
|
|
1750
2481
|
} else if ((name === 'onChange' || name === 'onChangeCapture') && this.isTextEntry(element)) {
|
|
1751
2482
|
const input = name === 'onChange' ? 'onInput' : 'onInputCapture';
|
|
1752
|
-
if (
|
|
2483
|
+
if (attributeOf(element, input)) {
|
|
1753
2484
|
this.note(
|
|
1754
2485
|
RULE.nativeChange,
|
|
1755
2486
|
'warning',
|
|
@@ -1787,10 +2518,8 @@ class ModuleTransform {
|
|
|
1787
2518
|
const tag = element.tagName.getText(this.sf);
|
|
1788
2519
|
if (tag === 'textarea') return true;
|
|
1789
2520
|
if (tag !== 'input') return false;
|
|
1790
|
-
const type = element
|
|
1791
|
-
|
|
1792
|
-
);
|
|
1793
|
-
if (!type || !ts.isJsxAttribute(type)) return true;
|
|
2521
|
+
const type = attributeOf(element, 'type');
|
|
2522
|
+
if (!type) return true;
|
|
1794
2523
|
const value = type.initializer;
|
|
1795
2524
|
if (value && ts.isStringLiteral(value)) return TEXT_INPUT_TYPES.has(value.text);
|
|
1796
2525
|
this.note(
|
|
@@ -1815,6 +2544,15 @@ class ModuleTransform {
|
|
|
1815
2544
|
);
|
|
1816
2545
|
}
|
|
1817
2546
|
const tag = node.openingElement.tagName;
|
|
2547
|
+
// A `descriptorChildren` component receives its children as elements, as
|
|
2548
|
+
// in React; `@if` or `@for` there would hand it a fragment instead.
|
|
2549
|
+
const declaration = ts.isIdentifier(tag) ? this.declarationOf(tag) : null;
|
|
2550
|
+
if (
|
|
2551
|
+
declaration &&
|
|
2552
|
+
this.descriptorComponents.has(/** @type {ts.VariableDeclaration} */ (declaration))
|
|
2553
|
+
) {
|
|
2554
|
+
return this.span(node, node.getStart(this.sf), node.end, PLAIN);
|
|
2555
|
+
}
|
|
1818
2556
|
const name = this.calleeName(tag);
|
|
1819
2557
|
if (name === 'StrictMode') {
|
|
1820
2558
|
// Octane's StrictMode is a pass-through wrapper.
|
|
@@ -2098,6 +2836,21 @@ class ModuleTransform {
|
|
|
2098
2836
|
);
|
|
2099
2837
|
}
|
|
2100
2838
|
|
|
2839
|
+
/**
|
|
2840
|
+
* The type of `node` in the project's program, which resolves imports and
|
|
2841
|
+
* packages, or null when the program does not have this module.
|
|
2842
|
+
*
|
|
2843
|
+
* @param {ts.Node} node
|
|
2844
|
+
* @returns {{ type: ts.Type, checker: ts.TypeChecker } | null}
|
|
2845
|
+
*/
|
|
2846
|
+
projectType(node) {
|
|
2847
|
+
const types = this.options.types;
|
|
2848
|
+
if (types?.sourceFile.text !== this.source) return null;
|
|
2849
|
+
const same = nodeSpanning(types.sourceFile, node.getStart(this.sf), node.end);
|
|
2850
|
+
if (same?.kind !== node.kind) return null;
|
|
2851
|
+
return { type: types.checker.getTypeAtLocation(same), checker: types.checker };
|
|
2852
|
+
}
|
|
2853
|
+
|
|
2101
2854
|
/**
|
|
2102
2855
|
* The type of `expression`, from the project's program when it has this
|
|
2103
2856
|
* module, else from the module's own.
|
|
@@ -2105,13 +2858,7 @@ class ModuleTransform {
|
|
|
2105
2858
|
* @param {ts.Expression} expression
|
|
2106
2859
|
*/
|
|
2107
2860
|
typeOf(expression) {
|
|
2108
|
-
|
|
2109
|
-
if (types?.sourceFile.text === this.source) {
|
|
2110
|
-
const start = expression.getStart(this.sf);
|
|
2111
|
-
const same = nodeSpanning(types.sourceFile, start, expression.end);
|
|
2112
|
-
if (same?.kind === expression.kind) return types.checker.getTypeAtLocation(same);
|
|
2113
|
-
}
|
|
2114
|
-
return this.checker.getTypeAtLocation(expression);
|
|
2861
|
+
return this.projectType(expression)?.type ?? this.checker.getTypeAtLocation(expression);
|
|
2115
2862
|
}
|
|
2116
2863
|
|
|
2117
2864
|
/**
|
|
@@ -2260,11 +3007,7 @@ class ModuleTransform {
|
|
|
2260
3007
|
// to the header once: the same key on each arm of a conditional row.
|
|
2261
3008
|
const elements = (rowElements(root) ?? []).filter((element) => !ts.isJsxFragment(element));
|
|
2262
3009
|
const keys = elements.map((element) => {
|
|
2263
|
-
|
|
2264
|
-
const attribute = opening.attributes.properties.find(
|
|
2265
|
-
(a) => ts.isJsxAttribute(a) && a.name.getText(this.sf) === 'key',
|
|
2266
|
-
);
|
|
2267
|
-
return /** @type {ts.JsxAttribute | undefined} */ (attribute);
|
|
3010
|
+
return attributeOf(ts.isJsxElement(element) ? element.openingElement : element, 'key');
|
|
2268
3011
|
});
|
|
2269
3012
|
let key = null;
|
|
2270
3013
|
if (keys.some(Boolean)) {
|