@openrewrite/recipes-react 0.27.0 → 0.29.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/dist/change-react-type.d.ts +14 -0
- package/dist/change-react-type.d.ts.map +1 -0
- package/dist/change-react-type.js +41 -0
- package/dist/change-react-type.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -2
- package/dist/index.js.map +1 -1
- package/dist/react18/add-children-to-props.d.ts.map +1 -1
- package/dist/react18/add-children-to-props.js +24 -6
- package/dist/react18/add-children-to-props.js.map +1 -1
- package/dist/react19/deprecated-prop-types-types.d.ts +23 -0
- package/dist/react19/deprecated-prop-types-types.d.ts.map +1 -0
- package/dist/react19/deprecated-prop-types-types.js +191 -0
- package/dist/react19/deprecated-prop-types-types.js.map +1 -0
- package/dist/react19/deprecated-react-child.d.ts +8 -0
- package/dist/react19/deprecated-react-child.d.ts.map +1 -0
- package/dist/react19/deprecated-react-child.js +91 -0
- package/dist/react19/deprecated-react-child.js.map +1 -0
- package/dist/react19/deprecated-react-fragment.d.ts +8 -0
- package/dist/react19/deprecated-react-fragment.d.ts.map +1 -0
- package/dist/react19/deprecated-react-fragment.js +92 -0
- package/dist/react19/deprecated-react-fragment.js.map +1 -0
- package/dist/react19/deprecated-react-node-array.d.ts +8 -0
- package/dist/react19/deprecated-react-node-array.d.ts.map +1 -0
- package/dist/react19/deprecated-react-node-array.js +92 -0
- package/dist/react19/deprecated-react-node-array.js.map +1 -0
- package/dist/react19/deprecated-react-text.d.ts +8 -0
- package/dist/react19/deprecated-react-text.d.ts.map +1 -0
- package/dist/react19/deprecated-react-text.js +91 -0
- package/dist/react19/deprecated-react-text.js.map +1 -0
- package/dist/react19/deprecated-react-types.d.ts.map +1 -1
- package/dist/react19/deprecated-react-types.js +3 -104
- package/dist/react19/deprecated-react-types.js.map +1 -1
- package/dist/react19/refobject-defaults.d.ts +13 -0
- package/dist/react19/refobject-defaults.d.ts.map +1 -0
- package/dist/react19/refobject-defaults.js +152 -0
- package/dist/react19/refobject-defaults.js.map +1 -0
- package/dist/react19/remove-react-fc.js +3 -2
- package/dist/react19/remove-react-fc.js.map +1 -1
- package/dist/types/imports.d.ts +31 -0
- package/dist/types/imports.d.ts.map +1 -0
- package/dist/types/imports.js +385 -0
- package/dist/types/imports.js.map +1 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +33 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/module.d.ts +6 -0
- package/dist/types/module.d.ts.map +1 -0
- package/dist/types/module.js +5 -0
- package/dist/types/module.js.map +1 -0
- package/dist/types/re-export.d.ts +5 -0
- package/dist/types/re-export.d.ts.map +1 -0
- package/dist/types/re-export.js +12 -0
- package/dist/types/re-export.js.map +1 -0
- package/dist/types/reference.d.ts +20 -0
- package/dist/types/reference.d.ts.map +1 -0
- package/dist/types/reference.js +161 -0
- package/dist/types/reference.js.map +1 -0
- package/dist/types/rename.d.ts +10 -0
- package/dist/types/rename.d.ts.map +1 -0
- package/dist/types/rename.js +136 -0
- package/dist/types/rename.js.map +1 -0
- package/dist/types/replace.d.ts +8 -0
- package/dist/types/replace.d.ts.map +1 -0
- package/dist/types/replace.js +122 -0
- package/dist/types/replace.js.map +1 -0
- package/dist/util/java-recipe-proxy.d.ts +1 -0
- package/dist/util/java-recipe-proxy.d.ts.map +1 -1
- package/dist/util/java-recipe-proxy.js +3 -1
- package/dist/util/java-recipe-proxy.js.map +1 -1
- package/package.json +2 -2
- package/src/change-react-type.ts +32 -0
- package/src/index.ts +21 -0
- package/src/react18/add-children-to-props.ts +22 -7
- package/src/react19/deprecated-prop-types-types.ts +170 -0
- package/src/react19/deprecated-react-child.ts +67 -0
- package/src/react19/deprecated-react-fragment.ts +74 -0
- package/src/react19/deprecated-react-node-array.ts +74 -0
- package/src/react19/deprecated-react-text.ts +66 -0
- package/src/react19/deprecated-react-types.ts +4 -95
- package/src/react19/refobject-defaults.ts +120 -0
- package/src/react19/remove-react-fc.ts +3 -3
- package/src/types/imports.ts +396 -0
- package/src/types/index.ts +21 -0
- package/src/types/module.ts +10 -0
- package/src/types/re-export.ts +14 -0
- package/src/types/reference.ts +186 -0
- package/src/types/rename.ts +112 -0
- package/src/types/replace.ts +123 -0
- package/src/util/java-recipe-proxy.ts +3 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.RefObjectDefaults = void 0;
|
|
13
|
+
const rewrite_1 = require("@openrewrite/rewrite");
|
|
14
|
+
const java_1 = require("@openrewrite/rewrite/java");
|
|
15
|
+
const javascript_1 = require("@openrewrite/rewrite/javascript");
|
|
16
|
+
const json_1 = require("@openrewrite/rewrite/json");
|
|
17
|
+
const semver_1 = require("semver");
|
|
18
|
+
const types_1 = require("../types");
|
|
19
|
+
const java_recipe_proxy_1 = require("../util/java-recipe-proxy");
|
|
20
|
+
class RefObjectDefaults extends rewrite_1.ScanningRecipe {
|
|
21
|
+
constructor() {
|
|
22
|
+
super(...arguments);
|
|
23
|
+
this.name = "org.openrewrite.react.19.refobject-defaults";
|
|
24
|
+
this.displayName = "Add `null` to the type argument of `RefObject`";
|
|
25
|
+
this.description = "Replaces React's `RefObject<T>` with `RefObject<T | null>`, since `@types/react` 19 no longer makes `current` nullable. Only edits a package still on `@types/react` 18 or earlier, as its lockfile resolves it or else as its range allows, since code written for 19 means `current` to be non-null. Leaves `any`, a type argument that already includes `null` or `any`, and `MutableRefObject` alone.";
|
|
26
|
+
}
|
|
27
|
+
initialValue() {
|
|
28
|
+
return new Map();
|
|
29
|
+
}
|
|
30
|
+
scanner(majors) {
|
|
31
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
return new class extends json_1.JsonVisitor {
|
|
33
|
+
visitDocument(doc) {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
if (!/(^|\/)package\.json$/.test(doc.sourcePath))
|
|
36
|
+
return doc;
|
|
37
|
+
const root = doc.sourcePath.slice(0, -"package.json".length);
|
|
38
|
+
const found = typesReactMajor(doc);
|
|
39
|
+
if (found !== undefined && !majors.has(root))
|
|
40
|
+
majors.set(root, found);
|
|
41
|
+
return doc;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}();
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
editorWithData(majors) {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
const before19 = new class extends javascript_1.JavaScriptVisitor {
|
|
50
|
+
visitJsCompilationUnit(cu) {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
const found = majorAt(cu.sourcePath, majors);
|
|
53
|
+
return found !== undefined && found < 19 ? (0, rewrite_1.foundSearchResult)(cu) : cu;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}();
|
|
57
|
+
return (0, rewrite_1.check)(before19, (0, types_1.onTypeScriptSources)(new class extends javascript_1.JavaScriptVisitor {
|
|
58
|
+
visitParameterizedType(type, ctx) {
|
|
59
|
+
const _super = Object.create(null, {
|
|
60
|
+
visitParameterizedType: { get: () => super.visitParameterizedType }
|
|
61
|
+
});
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
const pt = yield _super.visitParameterizedType.call(this, type, ctx);
|
|
64
|
+
if (!pt.typeParameters || !isRefObject(new rewrite_1.Cursor(pt.class, this.cursor), pt.class))
|
|
65
|
+
return pt;
|
|
66
|
+
const typeParameters = yield withNullableFirst(pt.typeParameters);
|
|
67
|
+
return typeParameters === pt.typeParameters ? pt : Object.assign(Object.assign({}, pt), { typeParameters });
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
visitExpressionWithTypeArguments(expression, ctx) {
|
|
71
|
+
const _super = Object.create(null, {
|
|
72
|
+
visitExpressionWithTypeArguments: { get: () => super.visitExpressionWithTypeArguments }
|
|
73
|
+
});
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
const e = yield _super.visitExpressionWithTypeArguments.call(this, expression, ctx);
|
|
76
|
+
if (!e.typeArguments || !isRefObject(new rewrite_1.Cursor(e.clazz, this.cursor), e.clazz))
|
|
77
|
+
return e;
|
|
78
|
+
const typeArguments = yield withNullableFirst(e.typeArguments);
|
|
79
|
+
return typeArguments === e.typeArguments ? e : Object.assign(Object.assign({}, e), { typeArguments });
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
visitImportType(importType, ctx) {
|
|
83
|
+
const _super = Object.create(null, {
|
|
84
|
+
visitImportType: { get: () => super.visitImportType }
|
|
85
|
+
});
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
var _a;
|
|
88
|
+
const it = yield _super.visitImportType.call(this, importType, ctx);
|
|
89
|
+
if (!it.typeArguments || ((_a = (0, types_1.importTypeReferenceOf)(it, types_1.react)) === null || _a === void 0 ? void 0 : _a.member) !== 'RefObject')
|
|
90
|
+
return it;
|
|
91
|
+
const typeArguments = yield withNullableFirst(it.typeArguments);
|
|
92
|
+
return typeArguments === it.typeArguments ? it : Object.assign(Object.assign({}, it), { typeArguments });
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}()));
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
editor() {
|
|
99
|
+
const _super = Object.create(null, {
|
|
100
|
+
editor: { get: () => super.editor }
|
|
101
|
+
});
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
return (0, rewrite_1.check)((0, java_recipe_proxy_1.treesOfType)("org.openrewrite.javascript.tree.JS"), _super.editor.call(this));
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
exports.RefObjectDefaults = RefObjectDefaults;
|
|
108
|
+
function typesReactMajor(doc) {
|
|
109
|
+
var _a, _b, _c, _d;
|
|
110
|
+
const resolution = (0, javascript_1.findNodeResolutionResult)(doc);
|
|
111
|
+
const declared = [...(_a = resolution === null || resolution === void 0 ? void 0 : resolution.dependencies) !== null && _a !== void 0 ? _a : [], ...(_b = resolution === null || resolution === void 0 ? void 0 : resolution.devDependencies) !== null && _b !== void 0 ? _b : [],
|
|
112
|
+
...(_c = resolution === null || resolution === void 0 ? void 0 : resolution.optionalDependencies) !== null && _c !== void 0 ? _c : [], ...(_d = resolution === null || resolution === void 0 ? void 0 : resolution.peerDependencies) !== null && _d !== void 0 ? _d : []].filter(dep => dep.name === '@types/react');
|
|
113
|
+
const resolved = declared.map(dep => { var _a; return (_a = dep.resolved) === null || _a === void 0 ? void 0 : _a.version; }).find((version, i) => version !== undefined && (0, semver_1.valid)(version) !== null &&
|
|
114
|
+
(!(0, semver_1.validRange)(declared[i].versionConstraint) || (0, semver_1.satisfies)(version, declared[i].versionConstraint)));
|
|
115
|
+
if (resolved)
|
|
116
|
+
return (0, semver_1.major)(resolved);
|
|
117
|
+
const lowest = declared.map(dep => (0, semver_1.validRange)(dep.versionConstraint) ? (0, semver_1.minVersion)(dep.versionConstraint) : null).find(version => version);
|
|
118
|
+
return lowest ? lowest.major : undefined;
|
|
119
|
+
}
|
|
120
|
+
function majorAt(path, majors) {
|
|
121
|
+
let nearest;
|
|
122
|
+
for (const root of majors.keys())
|
|
123
|
+
if (path.startsWith(root) && (nearest === undefined || root.length > nearest.length))
|
|
124
|
+
nearest = root;
|
|
125
|
+
return nearest === undefined ? undefined : majors.get(nearest);
|
|
126
|
+
}
|
|
127
|
+
function isRefObject(cursor, name) {
|
|
128
|
+
var _a;
|
|
129
|
+
return ((_a = (0, types_1.typeReferenceOf)(cursor, name, types_1.react)) === null || _a === void 0 ? void 0 : _a.member) === 'RefObject';
|
|
130
|
+
}
|
|
131
|
+
function withNullableFirst(typeArguments) {
|
|
132
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
133
|
+
const [first, ...rest] = typeArguments.elements;
|
|
134
|
+
if (!first || membersOf(first.element).some(t => isNull(t) || isAny(t)))
|
|
135
|
+
return typeArguments;
|
|
136
|
+
return Object.assign(Object.assign({}, typeArguments), { elements: [Object.assign(Object.assign({}, first), { element: yield (0, types_1.unionWith)(first.element, 'null') }), ...rest] });
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
function membersOf(type) {
|
|
140
|
+
if (type.kind === javascript_1.JS.Kind.Union)
|
|
141
|
+
return type.types.flatMap(t => membersOf(t.element));
|
|
142
|
+
if (type.kind === java_1.J.Kind.ParenthesizedTypeTree)
|
|
143
|
+
return membersOf(type.parenthesizedType.tree.element);
|
|
144
|
+
return [type];
|
|
145
|
+
}
|
|
146
|
+
function isNull(type) {
|
|
147
|
+
return type.kind === javascript_1.JS.Kind.LiteralType && type.literal.valueSource === 'null';
|
|
148
|
+
}
|
|
149
|
+
function isAny(type) {
|
|
150
|
+
return (0, java_1.isIdentifier)(type) && type.simpleName === 'any';
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=refobject-defaults.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refobject-defaults.js","sourceRoot":"","sources":["../../src/react19/refobject-defaults.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kDAAqH;AACrH,oDAA0D;AAC1D,gEAAgG;AAChG,oDAA4D;AAC5D,m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EAAC,IAAI,CAAC,IAAI,IAAI,CAAC,UAAU,KAAK,KAAK,CAAC;AAC3D,CAAC"}
|
|
@@ -14,6 +14,7 @@ const rewrite_1 = require("@openrewrite/rewrite");
|
|
|
14
14
|
const javascript_1 = require("@openrewrite/rewrite/javascript");
|
|
15
15
|
const java_1 = require("@openrewrite/rewrite/java");
|
|
16
16
|
const javascript_2 = require("@openrewrite/rewrite/javascript");
|
|
17
|
+
const types_1 = require("../types");
|
|
17
18
|
class RemoveReactFC extends rewrite_1.Recipe {
|
|
18
19
|
constructor() {
|
|
19
20
|
super(...arguments);
|
|
@@ -75,8 +76,8 @@ class RemoveReactFC extends rewrite_1.Recipe {
|
|
|
75
76
|
const modifiedArrowFn = propsTypeArg
|
|
76
77
|
? this.addPropsTypeToArrowFunction(arrowFn, propsTypeArg)
|
|
77
78
|
: arrowFn;
|
|
78
|
-
(0,
|
|
79
|
-
(0,
|
|
79
|
+
(0, types_1.dropImport)(this, types_1.react, "FC");
|
|
80
|
+
(0, types_1.dropImport)(this, types_1.react, "FunctionComponent");
|
|
80
81
|
const updatedVarDecl = Object.assign(Object.assign({}, v), { typeExpression: undefined, variables: [Object.assign(Object.assign({}, v.variables[0]), { element: Object.assign(Object.assign({}, namedVar), { initializer: Object.assign(Object.assign({}, namedVar.initializer), { element: modifiedArrowFn }) }) })] });
|
|
81
82
|
return updatedVarDecl;
|
|
82
83
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remove-react-fc.js","sourceRoot":"","sources":["../../src/react19/remove-react-fc.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kDAAuF;AACvF,
|
|
1
|
+
{"version":3,"file":"remove-react-fc.js","sourceRoot":"","sources":["../../src/react19/remove-react-fc.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,kDAAuF;AACvF,gEAOyC;AACzC,oDAAiF;AACjF,gEAAmD;AACnD,oCAA2C;AAgB3C,MAAa,aAAc,SAAQ,gBAAM;IAAzC;;Q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1B,IAAI,EAAE,eAAE,CAAC,IAAI,CAAC,QAAQ;4BACtB,EAAE,EAAE,SAAS,CAAC,EAAE;4BAChB,MAAM,EAAE,iBAAU;4BAClB,OAAO,EAAE,SAAS,CAAC,OAAO;4BAC1B,cAAc,kCACP,SAAS,KACZ,MAAM,EAAE,kBAAW,GACtB;yBACJ,CAAC;wBAEF,MAAM,cAAc,mCACb,OAAO,KACV,SAAS,EAAE,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,iCACtC,QAAQ,KACX,KAAK,EAAE,MAAM,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,iBAAU,IACtE,CAAC,EACH,cAAc,EAAE,QAAQ,GAC3B,CAAC;wBAEF,uCACO,OAAO,KACV,MAAM,kCACC,MAAM,KACT,KAAK,EAAE,MAAM,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,EAClF,UAAU,kCACH,MAAM,CAAC,UAAU,KACpB,aAAa,EAAE,IAAI,EACnB,UAAU,EAAE;wEAED,MAAM,CAAC,CAAC,CAAC,KACZ,OAAO,EAAE,cAAc;wCAE3B,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;qCACrB,UAGX;oBACN,CAAC;oBAED,OAAO,OAAO,CAAC;gBACnB,CAAC;aACJ,EAAE,CAAC,CAAC;QACT,CAAC;KAAA;CACJ;AAlOD,sCAkOC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Cursor } from "@openrewrite/rewrite";
|
|
2
|
+
import { J } from "@openrewrite/rewrite/java";
|
|
3
|
+
import { JavaScriptVisitor, JS } from "@openrewrite/rewrite/javascript";
|
|
4
|
+
import { Module } from "./module";
|
|
5
|
+
export type ImportScope = JS.CompilationUnit | J.Block;
|
|
6
|
+
export interface ModuleImports {
|
|
7
|
+
readonly named: Map<string, {
|
|
8
|
+
member: string;
|
|
9
|
+
typeOnly: boolean;
|
|
10
|
+
}>;
|
|
11
|
+
readonly qualifiers: Set<string>;
|
|
12
|
+
readonly bindsGlobal: boolean;
|
|
13
|
+
readonly typeOnlyDefault?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare function importsOf(scope: ImportScope, from: Module): ModuleImports;
|
|
16
|
+
export declare function importsMember(scope: ImportScope, from: Module, member: string): boolean;
|
|
17
|
+
export declare function localNameOf(imports: ModuleImports, member: string): string | undefined;
|
|
18
|
+
export declare function importScopesOf(cursor: Cursor): ImportScope[];
|
|
19
|
+
export declare function boundIn(cursor: Cursor, id: J.Identifier, scope: ImportScope): boolean;
|
|
20
|
+
export declare function declaresTopLevelValue(scope: ImportScope, name: string): boolean;
|
|
21
|
+
export declare function moduleOf(i: JS.Import): string | undefined;
|
|
22
|
+
export declare function exportName(name: J): string | undefined;
|
|
23
|
+
export declare function memberOf(specifier: JS.ImportSpecifier): string | undefined;
|
|
24
|
+
export declare function dropImport(visitor: JavaScriptVisitor<any>, from: Module, member: string): void;
|
|
25
|
+
export declare function isModule(cu: JS.CompilationUnit): boolean;
|
|
26
|
+
export declare function editBlockImports(visitor: JavaScriptVisitor<any>, block: J.Block, from: Module, edit: {
|
|
27
|
+
add?: string;
|
|
28
|
+
drop?: string;
|
|
29
|
+
}): void;
|
|
30
|
+
export declare function keepMovedTypesTypeOnly(visitor: JavaScriptVisitor<any>, cu: JS.CompilationUnit, to: Module, members: readonly string[]): void;
|
|
31
|
+
//# sourceMappingURL=imports.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imports.d.ts","sourceRoot":"","sources":["../../src/types/imports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAW,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAA2B,CAAC,EAAc,MAAM,2BAA2B,CAAC;AACnF,OAAO,EAG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GAAG,SAAS,CAGtD;AAGD,wBAAgB,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC,eAAe,GAAG,MAAM,GAAG,SAAS,CAG1E;AAaD,wBAAgB,UAAU,CAAC,OAAO,EAAE,iBAAiB,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAI9F;AAsED,wBAAgB,QAAQ,CAAC,EAAE,EAAE,EAAE,CAAC,eAAe,GAAG,OAAO,CAIxD;AAgDD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAM3I;AAiED,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,iBAAiB,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,CAAC,eAAe,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAO5I"}
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.importsOf = importsOf;
|
|
13
|
+
exports.importsMember = importsMember;
|
|
14
|
+
exports.localNameOf = localNameOf;
|
|
15
|
+
exports.importScopesOf = importScopesOf;
|
|
16
|
+
exports.boundIn = boundIn;
|
|
17
|
+
exports.declaresTopLevelValue = declaresTopLevelValue;
|
|
18
|
+
exports.moduleOf = moduleOf;
|
|
19
|
+
exports.exportName = exportName;
|
|
20
|
+
exports.memberOf = memberOf;
|
|
21
|
+
exports.dropImport = dropImport;
|
|
22
|
+
exports.isModule = isModule;
|
|
23
|
+
exports.editBlockImports = editBlockImports;
|
|
24
|
+
exports.keepMovedTypesTypeOnly = keepMovedTypesTypeOnly;
|
|
25
|
+
const rewrite_1 = require("@openrewrite/rewrite");
|
|
26
|
+
const java_1 = require("@openrewrite/rewrite/java");
|
|
27
|
+
const javascript_1 = require("@openrewrite/rewrite/javascript");
|
|
28
|
+
const reference_1 = require("./reference");
|
|
29
|
+
const importsCache = new WeakMap();
|
|
30
|
+
function importsOf(scope, from) {
|
|
31
|
+
var _a;
|
|
32
|
+
let byModule = importsCache.get(scope);
|
|
33
|
+
if (!byModule)
|
|
34
|
+
importsCache.set(scope, byModule = new Map());
|
|
35
|
+
let imports = byModule.get(from.module);
|
|
36
|
+
if (imports)
|
|
37
|
+
return imports;
|
|
38
|
+
const named = new Map();
|
|
39
|
+
const qualifiers = new Set();
|
|
40
|
+
let bindsGlobal = false;
|
|
41
|
+
let typeOnlyDefault;
|
|
42
|
+
const binds = (local) => {
|
|
43
|
+
if (local === from.global)
|
|
44
|
+
bindsGlobal = true;
|
|
45
|
+
};
|
|
46
|
+
for (const { element: statement } of scope.statements) {
|
|
47
|
+
if (statement.kind !== javascript_1.JS.Kind.Import)
|
|
48
|
+
continue;
|
|
49
|
+
const clause = statement.importClause;
|
|
50
|
+
if (!clause)
|
|
51
|
+
continue;
|
|
52
|
+
const fromModule = moduleOf(statement) === from.module;
|
|
53
|
+
const local = (_a = clause.name) === null || _a === void 0 ? void 0 : _a.element.simpleName;
|
|
54
|
+
if (local)
|
|
55
|
+
binds(local);
|
|
56
|
+
if (local && fromModule)
|
|
57
|
+
qualifiers.add(local);
|
|
58
|
+
if (local && fromModule && clause.typeOnly && !clause.namedBindings)
|
|
59
|
+
typeOnlyDefault !== null && typeOnlyDefault !== void 0 ? typeOnlyDefault : (typeOnlyDefault = local);
|
|
60
|
+
const bindings = clause.namedBindings;
|
|
61
|
+
if ((bindings === null || bindings === void 0 ? void 0 : bindings.kind) === javascript_1.JS.Kind.Alias) {
|
|
62
|
+
const alias = bindings.alias;
|
|
63
|
+
if ((0, java_1.isIdentifier)(alias)) {
|
|
64
|
+
binds(alias.simpleName);
|
|
65
|
+
if (fromModule)
|
|
66
|
+
qualifiers.add(alias.simpleName);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else if ((bindings === null || bindings === void 0 ? void 0 : bindings.kind) === javascript_1.JS.Kind.NamedImports) {
|
|
70
|
+
for (const { element: specifier } of bindings.elements.elements) {
|
|
71
|
+
if (specifier.kind !== javascript_1.JS.Kind.ImportSpecifier)
|
|
72
|
+
continue;
|
|
73
|
+
const member = memberOf(specifier);
|
|
74
|
+
const localName = localOf(specifier);
|
|
75
|
+
if (!localName)
|
|
76
|
+
continue;
|
|
77
|
+
binds(localName);
|
|
78
|
+
if (fromModule && member) {
|
|
79
|
+
named.set(localName, { member, typeOnly: clause.typeOnly || specifier.importType.element });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
imports = { named, qualifiers, bindsGlobal, typeOnlyDefault };
|
|
85
|
+
byModule.set(from.module, imports);
|
|
86
|
+
return imports;
|
|
87
|
+
}
|
|
88
|
+
function importsMember(scope, from, member) {
|
|
89
|
+
return localNameOf(importsOf(scope, from), member) !== undefined;
|
|
90
|
+
}
|
|
91
|
+
function localNameOf(imports, member) {
|
|
92
|
+
var _a;
|
|
93
|
+
return (_a = [...imports.named].find(([, imported]) => imported.member === member)) === null || _a === void 0 ? void 0 : _a[0];
|
|
94
|
+
}
|
|
95
|
+
function importScopesOf(cursor) {
|
|
96
|
+
const scopes = [];
|
|
97
|
+
for (let c = cursor; c; c = c.parent) {
|
|
98
|
+
const node = c.value;
|
|
99
|
+
if ((node === null || node === void 0 ? void 0 : node.kind) === javascript_1.JS.Kind.CompilationUnit)
|
|
100
|
+
scopes.push(node);
|
|
101
|
+
const body = (node === null || node === void 0 ? void 0 : node.kind) === javascript_1.JS.Kind.NamespaceDeclaration && node.name.element.kind === java_1.J.Kind.Literal ?
|
|
102
|
+
node.body : undefined;
|
|
103
|
+
if (body)
|
|
104
|
+
scopes.push(body);
|
|
105
|
+
}
|
|
106
|
+
return scopes;
|
|
107
|
+
}
|
|
108
|
+
function boundIn(cursor, id, scope) {
|
|
109
|
+
var _a;
|
|
110
|
+
const owner = (0, javascript_1.resolve)(cursor, id);
|
|
111
|
+
return owner !== undefined && (owner.id === scope.id ||
|
|
112
|
+
owner.kind === javascript_1.JS.Kind.NamespaceDeclaration && ((_a = owner.body) === null || _a === void 0 ? void 0 : _a.id) === scope.id);
|
|
113
|
+
}
|
|
114
|
+
function declaresTopLevelValue(scope, name) {
|
|
115
|
+
return scope.statements.some(({ element: s }) => {
|
|
116
|
+
const declares = (v) => v.variables.some(n => (0, javascript_1.bindingNames)(n.element.name).some(b => b.name === name));
|
|
117
|
+
if (s.kind === java_1.J.Kind.VariableDeclarations)
|
|
118
|
+
return declares(s);
|
|
119
|
+
if (s.kind === javascript_1.JS.Kind.ScopedVariableDeclarations) {
|
|
120
|
+
return s.variables.some(({ element: v }) => v.kind === java_1.J.Kind.VariableDeclarations && declares(v));
|
|
121
|
+
}
|
|
122
|
+
return s.kind === java_1.J.Kind.MethodDeclaration && s.name.simpleName === name;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
function moduleOf(i) {
|
|
126
|
+
var _a, _b, _c, _d;
|
|
127
|
+
const required = (_a = i.initializer) === null || _a === void 0 ? void 0 : _a.element;
|
|
128
|
+
const specifier = (_c = (_b = i.moduleSpecifier) === null || _b === void 0 ? void 0 : _b.element) !== null && _c !== void 0 ? _c : ((required === null || required === void 0 ? void 0 : required.kind) === java_1.J.Kind.MethodInvocation && required.name.simpleName === 'require' && !required.select ?
|
|
129
|
+
(_d = required.arguments.elements[0]) === null || _d === void 0 ? void 0 : _d.element : undefined);
|
|
130
|
+
return (specifier === null || specifier === void 0 ? void 0 : specifier.kind) === java_1.J.Kind.Literal ? specifier.value : undefined;
|
|
131
|
+
}
|
|
132
|
+
function exportName(name) {
|
|
133
|
+
if ((0, java_1.isIdentifier)(name))
|
|
134
|
+
return name.simpleName;
|
|
135
|
+
return name.kind === java_1.J.Kind.Literal && typeof name.value === 'string' ? name.value : undefined;
|
|
136
|
+
}
|
|
137
|
+
function memberOf(specifier) {
|
|
138
|
+
const s = specifier.specifier;
|
|
139
|
+
return s.kind === javascript_1.JS.Kind.Alias ? exportName(s.propertyName.element) : exportName(s);
|
|
140
|
+
}
|
|
141
|
+
function localOf(specifier) {
|
|
142
|
+
const s = specifier.specifier;
|
|
143
|
+
const local = s.kind === javascript_1.JS.Kind.Alias ? s.alias : s;
|
|
144
|
+
return (0, java_1.isIdentifier)(local) ? local.simpleName : undefined;
|
|
145
|
+
}
|
|
146
|
+
function dropImport(visitor, from, member) {
|
|
147
|
+
if (!visitor.afterVisit.some(v => v instanceof DropImport && v.module === from.module && v.member === member)) {
|
|
148
|
+
visitor.afterVisit.push(new DropImport(from.module, member));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
class DropImport extends javascript_1.JavaScriptVisitor {
|
|
152
|
+
constructor(module, member) {
|
|
153
|
+
super();
|
|
154
|
+
this.module = module;
|
|
155
|
+
this.member = member;
|
|
156
|
+
}
|
|
157
|
+
visitJsCompilationUnit(cu, p) {
|
|
158
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
+
const hidden = yield this.withSameSpelledValuesHidden(cu);
|
|
160
|
+
const removedHidden = yield this.removeKeepingNamespaces(hidden, p);
|
|
161
|
+
if (!removedHidden || removedHidden === hidden)
|
|
162
|
+
return cu;
|
|
163
|
+
const removed = hidden === cu ? removedHidden : withStatementsRestored(cu, removedHidden);
|
|
164
|
+
const kept = withClosingLayoutKept(cu, removed);
|
|
165
|
+
return isModule(cu) && !isModule(kept) ? yield withModuleMarker(kept) : kept;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
withSameSpelledValuesHidden(cu) {
|
|
169
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
170
|
+
var _a;
|
|
171
|
+
const locals = [...importsOf(cu, { module: this.module }).named]
|
|
172
|
+
.filter(([local, i]) => i.member === this.member && i.typeOnly && declaresTopLevelValue(cu, local))
|
|
173
|
+
.map(([local]) => local);
|
|
174
|
+
if (locals.length === 0)
|
|
175
|
+
return cu;
|
|
176
|
+
const from = { module: this.module };
|
|
177
|
+
return (_a = yield new class extends javascript_1.JavaScriptVisitor {
|
|
178
|
+
visitImportDeclaration(i) {
|
|
179
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
180
|
+
return i;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
visitIdentifier(identifier, p) {
|
|
184
|
+
const _super = Object.create(null, {
|
|
185
|
+
visitIdentifier: { get: () => super.visitIdentifier }
|
|
186
|
+
});
|
|
187
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
188
|
+
const id = yield _super.visitIdentifier.call(this, identifier, p);
|
|
189
|
+
return locals.includes(id.simpleName) && !(0, reference_1.typeReferenceOf)(this.cursor, id, from) ?
|
|
190
|
+
Object.assign(Object.assign({}, id), { simpleName: `__value_${id.simpleName}` }) : id;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}().visit(cu, undefined)) !== null && _a !== void 0 ? _a : cu;
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
removeKeepingNamespaces(cu, p) {
|
|
197
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
198
|
+
const namespaces = new Map(cu.statements.filter(s => this.isNamespaceImport(s.element)).map(s => [s.element.id, s]));
|
|
199
|
+
const remove = new javascript_1.RemoveImport(this.module, this.member);
|
|
200
|
+
if (namespaces.size === 0)
|
|
201
|
+
return remove.visit(cu, p);
|
|
202
|
+
const standIns = cu.statements.map(s => namespaces.has(s.element.id) ? Object.assign(Object.assign({}, s), { element: { kind: java_1.J.Kind.Empty, id: s.element.id, prefix: s.element.prefix, markers: s.element.markers } }) : s);
|
|
203
|
+
const standing = Object.assign(Object.assign({}, cu), { statements: standIns });
|
|
204
|
+
const removed = yield remove.visit(standing, p);
|
|
205
|
+
if (!removed || removed === standing)
|
|
206
|
+
return cu;
|
|
207
|
+
const statements = removed.statements.map(s => {
|
|
208
|
+
const namespace = namespaces.get(s.element.id);
|
|
209
|
+
if (!namespace)
|
|
210
|
+
return s;
|
|
211
|
+
return s.element.prefix === namespace.element.prefix ? namespace : Object.assign(Object.assign({}, namespace), { element: Object.assign(Object.assign({}, namespace.element), { prefix: s.element.prefix }) });
|
|
212
|
+
});
|
|
213
|
+
return Object.assign(Object.assign({}, removed), { statements });
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
isNamespaceImport(statement) {
|
|
217
|
+
var _a, _b;
|
|
218
|
+
return statement.kind === javascript_1.JS.Kind.Import && moduleOf(statement) === this.module &&
|
|
219
|
+
((_b = (_a = statement.importClause) === null || _a === void 0 ? void 0 : _a.namedBindings) === null || _b === void 0 ? void 0 : _b.kind) === javascript_1.JS.Kind.Alias;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function withStatementsRestored(original, edited) {
|
|
223
|
+
const originals = new Map(original.statements.map(s => [s.element.id, s.element]));
|
|
224
|
+
return Object.assign(Object.assign({}, edited), { statements: edited.statements.map(s => {
|
|
225
|
+
const was = originals.get(s.element.id);
|
|
226
|
+
return !was || s.element.kind === javascript_1.JS.Kind.Import ? s : Object.assign(Object.assign({}, s), { element: Object.assign(Object.assign({}, was), { prefix: s.element.prefix }) });
|
|
227
|
+
}) });
|
|
228
|
+
}
|
|
229
|
+
function isModule(cu) {
|
|
230
|
+
return cu.statements.some(({ element: s }) => {
|
|
231
|
+
var _a;
|
|
232
|
+
return s.kind === javascript_1.JS.Kind.Import || s.kind === javascript_1.JS.Kind.ExportDeclaration || s.kind === javascript_1.JS.Kind.ExportAssignment ||
|
|
233
|
+
((_a = s.modifiers) !== null && _a !== void 0 ? _a : []).some(m => m.keyword === 'export');
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
const parser = new javascript_1.JavaScriptParser();
|
|
237
|
+
function withModuleMarker(cu) {
|
|
238
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
239
|
+
const parsed = yield parser.parseOnly({ text: 'export {};', sourcePath: 'module-marker.ts' });
|
|
240
|
+
const marker = parsed.statements[0];
|
|
241
|
+
const placed = Object.assign(Object.assign({}, marker), { element: Object.assign(Object.assign({}, marker.element), { id: (0, rewrite_1.randomId)(), prefix: Object.assign(Object.assign({}, marker.element.prefix), { whitespace: cu.statements.length ? '\n\n' : '' }) }) });
|
|
242
|
+
return Object.assign(Object.assign({}, cu), { statements: [...cu.statements, placed] });
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
function withClosingLayoutKept(before, after) {
|
|
246
|
+
const originals = new Map(before.statements.map(s => [s.element.id, s.element]));
|
|
247
|
+
const statements = after.statements.map(s => {
|
|
248
|
+
const original = originals.get(s.element.id);
|
|
249
|
+
if (s.element.kind !== javascript_1.JS.Kind.Import || !original || original === s.element)
|
|
250
|
+
return s;
|
|
251
|
+
const kept = closingLayoutKept(original, s.element);
|
|
252
|
+
return kept === s.element ? s : Object.assign(Object.assign({}, s), { element: kept });
|
|
253
|
+
});
|
|
254
|
+
return statements.every((s, i) => s === after.statements[i]) ? after : Object.assign(Object.assign({}, after), { statements });
|
|
255
|
+
}
|
|
256
|
+
function closingLayoutKept(original, imported) {
|
|
257
|
+
var _a;
|
|
258
|
+
const was = (_a = namedImportsOf(original)) === null || _a === void 0 ? void 0 : _a.elements.elements;
|
|
259
|
+
const named = namedImportsOf(imported);
|
|
260
|
+
const now = named === null || named === void 0 ? void 0 : named.elements.elements;
|
|
261
|
+
if (!was || !named || !now || now.length === 0 || now.length >= was.length)
|
|
262
|
+
return imported;
|
|
263
|
+
const lastWas = was[was.length - 1], lastNow = now[now.length - 1];
|
|
264
|
+
if (lastNow.element.id === lastWas.element.id)
|
|
265
|
+
return imported;
|
|
266
|
+
const last = Object.assign(Object.assign({}, lastNow), { after: lastWas.after, markers: lastWas.markers });
|
|
267
|
+
return Object.assign(Object.assign({}, imported), { importClause: Object.assign(Object.assign({}, imported.importClause), { namedBindings: Object.assign(Object.assign({}, named), { elements: Object.assign(Object.assign({}, named.elements), { elements: [...now.slice(0, -1), last] }) }) }) });
|
|
268
|
+
}
|
|
269
|
+
function namedImportsOf(i) {
|
|
270
|
+
var _a;
|
|
271
|
+
const bindings = (_a = i.importClause) === null || _a === void 0 ? void 0 : _a.namedBindings;
|
|
272
|
+
return (bindings === null || bindings === void 0 ? void 0 : bindings.kind) === javascript_1.JS.Kind.NamedImports ? bindings : undefined;
|
|
273
|
+
}
|
|
274
|
+
function editBlockImports(visitor, block, from, edit) {
|
|
275
|
+
let edits = visitor.afterVisit.find(v => v instanceof EditBlockImports && v.blockId === block.id && v.module === from.module);
|
|
276
|
+
if (!edits)
|
|
277
|
+
visitor.afterVisit.push(edits = new EditBlockImports(block.id, from.module));
|
|
278
|
+
if (edit.add)
|
|
279
|
+
edits.add.add(edit.add);
|
|
280
|
+
if (edit.drop)
|
|
281
|
+
edits.drop.add(edit.drop);
|
|
282
|
+
}
|
|
283
|
+
class EditBlockImports extends javascript_1.JavaScriptVisitor {
|
|
284
|
+
constructor(blockId, module) {
|
|
285
|
+
super();
|
|
286
|
+
this.blockId = blockId;
|
|
287
|
+
this.module = module;
|
|
288
|
+
this.add = new Set();
|
|
289
|
+
this.drop = new Set();
|
|
290
|
+
}
|
|
291
|
+
visitBlock(block, p) {
|
|
292
|
+
const _super = Object.create(null, {
|
|
293
|
+
visitBlock: { get: () => super.visitBlock }
|
|
294
|
+
});
|
|
295
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
296
|
+
const visited = yield _super.visitBlock.call(this, block, p);
|
|
297
|
+
return visited.id === this.blockId ? this.edited(visited) : visited;
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
edited(block) {
|
|
301
|
+
const from = { module: this.module };
|
|
302
|
+
const imports = importsOf(block, from);
|
|
303
|
+
const used = (0, javascript_1.namesUsedWithin)(block.statements.filter(s => s.element.kind !== javascript_1.JS.Kind.Import));
|
|
304
|
+
const drop = new Set([...this.drop].filter(member => {
|
|
305
|
+
const local = localNameOf(imports, member);
|
|
306
|
+
return local !== undefined && !used.has(local);
|
|
307
|
+
}));
|
|
308
|
+
const pending = [...this.add].filter(member => !localNameOf(imports, member));
|
|
309
|
+
let appended = false;
|
|
310
|
+
const statements = block.statements.flatMap(statement => {
|
|
311
|
+
var _a;
|
|
312
|
+
const i = statement.element;
|
|
313
|
+
const named = i.kind === javascript_1.JS.Kind.Import && moduleOf(i) === this.module ? namedImportsOf(i) : undefined;
|
|
314
|
+
if (!named)
|
|
315
|
+
return [statement];
|
|
316
|
+
let elements = named.elements.elements.flatMap(e => {
|
|
317
|
+
const member = memberOf(e.element);
|
|
318
|
+
if (member === undefined || !drop.has(member))
|
|
319
|
+
return [e];
|
|
320
|
+
const next = pending.shift();
|
|
321
|
+
return next === undefined ? [] : [Object.assign(Object.assign({}, e), { element: specifierFor(e.element, next) })];
|
|
322
|
+
});
|
|
323
|
+
if (!appended && pending.length && elements.length) {
|
|
324
|
+
appended = true;
|
|
325
|
+
const last = elements[elements.length - 1];
|
|
326
|
+
elements = [...elements.slice(0, -1), Object.assign(Object.assign({}, last), { after: java_1.emptySpace }), ...pending.splice(0).map((member, k, added) => (Object.assign(Object.assign({}, last), { element: specifierFor(last.element, member, java_1.singleSpace), after: k === added.length - 1 ? last.after : java_1.emptySpace })))];
|
|
327
|
+
}
|
|
328
|
+
if (elements.length === 0 && !((_a = i.importClause) === null || _a === void 0 ? void 0 : _a.name))
|
|
329
|
+
return [];
|
|
330
|
+
return [Object.assign(Object.assign({}, statement), { element: Object.assign(Object.assign({}, i), { importClause: Object.assign(Object.assign({}, i.importClause), { namedBindings: Object.assign(Object.assign({}, named), { elements: Object.assign(Object.assign({}, named.elements), { elements }) }) }) }) })];
|
|
331
|
+
});
|
|
332
|
+
return Object.assign(Object.assign({}, block), { statements });
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function specifierFor(like, member, prefix = like.prefix) {
|
|
336
|
+
const s = like.specifier;
|
|
337
|
+
const name = s.kind === javascript_1.JS.Kind.Alias ? Object.assign(Object.assign({}, s.propertyName.element), { prefix: s.prefix }) : s;
|
|
338
|
+
const identifier = (0, java_1.isIdentifier)(name) ? name :
|
|
339
|
+
{ kind: java_1.J.Kind.Identifier, id: (0, rewrite_1.randomId)(), prefix: name.prefix, markers: name.markers, annotations: [], simpleName: member };
|
|
340
|
+
return Object.assign(Object.assign({}, like), { id: (0, rewrite_1.randomId)(), prefix, specifier: Object.assign(Object.assign({}, identifier), { id: (0, rewrite_1.randomId)(), simpleName: member, type: undefined }) });
|
|
341
|
+
}
|
|
342
|
+
function keepMovedTypesTypeOnly(visitor, cu, to, members) {
|
|
343
|
+
const existing = new Set();
|
|
344
|
+
for (const { element: statement } of cu.statements) {
|
|
345
|
+
const named = statement.kind === javascript_1.JS.Kind.Import && moduleOf(statement) === to.module ? namedImportsOf(statement) : undefined;
|
|
346
|
+
named === null || named === void 0 ? void 0 : named.elements.elements.forEach(e => existing.add(e.element.id));
|
|
347
|
+
}
|
|
348
|
+
visitor.afterVisit.push(new TypeOnlyImports(to.module, new Set(members), existing));
|
|
349
|
+
}
|
|
350
|
+
class TypeOnlyImports extends javascript_1.JavaScriptVisitor {
|
|
351
|
+
constructor(module, members, existing) {
|
|
352
|
+
super();
|
|
353
|
+
this.module = module;
|
|
354
|
+
this.members = members;
|
|
355
|
+
this.existing = existing;
|
|
356
|
+
}
|
|
357
|
+
visitJsCompilationUnit(cu) {
|
|
358
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
359
|
+
const statements = cu.statements.map(s => s.element.kind === javascript_1.JS.Kind.Import && moduleOf(s.element) === this.module ? Object.assign(Object.assign({}, s), { element: this.typeOnly(s.element) }) : s);
|
|
360
|
+
return statements.every((s, i) => s.element === cu.statements[i].element) ? cu : Object.assign(Object.assign({}, cu), { statements });
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
typeOnly(i) {
|
|
364
|
+
const clause = i.importClause;
|
|
365
|
+
const named = namedImportsOf(i);
|
|
366
|
+
if (!clause || clause.typeOnly || !named)
|
|
367
|
+
return i;
|
|
368
|
+
const moved = (s) => {
|
|
369
|
+
var _a;
|
|
370
|
+
return s.kind === javascript_1.JS.Kind.ImportSpecifier && !this.existing.has(s.id) &&
|
|
371
|
+
!s.importType.element && this.members.has((_a = memberOf(s)) !== null && _a !== void 0 ? _a : '');
|
|
372
|
+
};
|
|
373
|
+
const elements = named.elements.elements;
|
|
374
|
+
if (!elements.some(e => moved(e.element)))
|
|
375
|
+
return i;
|
|
376
|
+
if (!clause.name && elements.every(e => moved(e.element))) {
|
|
377
|
+
const bindings = named.prefix.whitespace ? named : Object.assign(Object.assign({}, named), { prefix: java_1.singleSpace });
|
|
378
|
+
return Object.assign(Object.assign({}, i), { importClause: Object.assign(Object.assign({}, clause), { typeOnly: true, namedBindings: bindings }) });
|
|
379
|
+
}
|
|
380
|
+
const inline = (s) => (Object.assign(Object.assign({}, s), { importType: Object.assign(Object.assign({}, s.importType), { element: true }), specifier: Object.assign(Object.assign({}, s.specifier), { prefix: java_1.singleSpace }) }));
|
|
381
|
+
const marked = elements.map(e => moved(e.element) ? Object.assign(Object.assign({}, e), { element: inline(e.element) }) : e);
|
|
382
|
+
return Object.assign(Object.assign({}, i), { importClause: Object.assign(Object.assign({}, clause), { namedBindings: Object.assign(Object.assign({}, named), { elements: Object.assign(Object.assign({}, named.elements), { elements: marked }) }) }) });
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
//# sourceMappingURL=imports.js.map
|