@nstudio/nativescript-compiler 9.2.0-alpha.10 → 9.2.0-alpha.12
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/api/ir.js +7 -1
- package/dist/cli/runtime.cjs +7 -1
- package/dist/css/css.js +30 -6
- package/dist/driver/cli.js +2 -0
- package/dist/driver/compile.js +23 -10
- package/dist/driver/paths.js +7 -0
- package/dist/driver/unimplemented.js +15 -0
- package/dist/kotlin/access.js +23 -5
- package/dist/kotlin/arguments.js +5 -0
- package/dist/kotlin/builtins.js +0 -4
- package/dist/kotlin/calls.js +6 -2
- package/dist/kotlin/class-methods.js +9 -3
- package/dist/kotlin/components.js +13 -8
- package/dist/kotlin/conversion.js +17 -5
- package/dist/kotlin/core-api.js +2 -13
- package/dist/kotlin/declarations.js +2 -0
- package/dist/kotlin/expression-statements.js +5 -4
- package/dist/kotlin/expressions.js +8 -3
- package/dist/kotlin/functions.js +13 -4
- package/dist/kotlin/global-calls.js +11 -0
- package/dist/kotlin/literals.js +58 -14
- package/dist/kotlin/names.js +2 -2
- package/dist/kotlin/native/api.js +9 -1
- package/dist/kotlin/native/calls.js +84 -3
- package/dist/kotlin/native/subclasses.js +4 -3
- package/dist/kotlin/native/types.js +8 -5
- package/dist/kotlin/operators.js +4 -1
- package/dist/kotlin/overrides.js +7 -5
- package/dist/kotlin/placement.js +3 -1
- package/dist/kotlin/prototype-calls.js +5 -0
- package/dist/kotlin/render.js +62 -0
- package/dist/kotlin/statements.js +9 -0
- package/dist/kotlin/static-calls.js +10 -7
- package/dist/kotlin/translator.js +31 -11
- package/dist/kotlin/type-facts.js +10 -2
- package/dist/kotlin/types.js +6 -6
- package/dist/plugins/native-android.js +37 -5
- package/dist/plugins/native-ios.js +2 -0
- package/dist/plugins/source.js +73 -23
- package/dist/program/app-modules.js +91 -16
- package/dist/program/app-paths.js +80 -12
- package/dist/program/class-mixins.js +463 -0
- package/dist/program/core-facts.js +7 -0
- package/dist/program/glob.js +274 -0
- package/dist/program/platform.js +32 -4
- package/dist/program/program.js +239 -38
- package/dist/program/source-lines.js +4 -1
- package/dist/program/vite-plugins.js +144 -0
- package/dist/semantics/apis/globals.js +2 -2
- package/dist/semantics/apis/object.js +1 -0
- package/dist/semantics/apis/statics.js +6 -0
- package/dist/semantics/ast.js +20 -2
- package/dist/semantics/classes.js +36 -1
- package/dist/semantics/facts.js +28 -1
- package/dist/semantics/globals.js +3 -0
- package/dist/semantics/lang.js +13 -1
- package/dist/semantics/nullish.js +29 -11
- package/dist/semantics/refuse-receivers.js +3 -3
- package/dist/semantics/refuse-scope.js +2 -7
- package/dist/semantics/runtime.js +1 -0
- package/dist/semantics/shapes.js +21 -1
- package/dist/semantics/states/value-states.js +1 -1
- package/dist/semantics/throws.js +1 -0
- package/dist/semantics/translator-methods.js +1 -1
- package/dist/semantics/types.js +21 -1
- package/dist/swift/access.js +25 -10
- package/dist/swift/arguments.js +10 -2
- package/dist/swift/assignment.js +12 -1
- package/dist/swift/builtins.js +0 -4
- package/dist/swift/calls.js +6 -2
- package/dist/swift/class-methods.js +3 -3
- package/dist/swift/components.js +28 -8
- package/dist/swift/construction.js +4 -0
- package/dist/swift/conversion.js +1 -1
- package/dist/swift/core-api.js +9 -12
- package/dist/swift/from-any.js +2 -2
- package/dist/swift/functions.js +13 -2
- package/dist/swift/generics.js +5 -1
- package/dist/swift/global-calls.js +2 -0
- package/dist/swift/literals.js +57 -10
- package/dist/swift/modules.js +1 -1
- package/dist/swift/names.js +1 -1
- package/dist/swift/native/sdk-tables.js +52 -4
- package/dist/swift/native/sdk.js +9 -1
- package/dist/swift/operators.js +4 -1
- package/dist/swift/parameters.js +5 -2
- package/dist/swift/placement.js +44 -4
- package/dist/swift/prototype-calls.js +17 -2
- package/dist/swift/render.js +64 -0
- package/dist/swift/results.js +1 -1
- package/dist/swift/statements.js +9 -0
- package/dist/swift/static-calls.js +13 -6
- package/dist/swift/types.js +9 -9
- package/dist/targets/android/build.js +18 -12
- package/dist/targets/ios/build.js +11 -10
- package/dist/targets/ios/dependencies.js +12 -0
- package/dist/targets/resources.js +15 -1
- package/dist/template/plan.js +105 -8
- package/dist/types/adapter-api.d.ts +13 -0
- package/dist/types/ir.d.ts +47 -0
- package/kit-android/generated/kotlin/org/nativescript/kit/__Objects.kt +102 -7485
- package/kit-android/generated/kotlin/org/nativescript/kit/application_application.kt +4 -4
- package/kit-android/generated/kotlin/org/nativescript/kit/image_source_index.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_animation_animation_common.kt +7 -3
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_animation_index.kt +3 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_view_base_index.kt +2 -4
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_view_index.kt +5 -6
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_view_view_common.kt +1 -3
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_dialogs_index.kt +11 -6
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_frame_fragment_transitions.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_frame_frame_common.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_frame_frame_helper_for_android.kt +7 -5
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_frame_index.kt +6 -3
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_gestures_touch_manager.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_layouts_flexbox_layout_index.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_search_bar_index.kt +4 -3
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_segmented_bar_index.kt +2 -2
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_text_base_formatted_string.kt +1 -3
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_text_base_index.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_transition_fade_transition.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_transition_flip_transition.kt +8 -8
- package/kit-android/generated/kotlin/org/nativescript/kit/utils_index.kt +1 -1
- package/kit-android/generated/source-lines.json +1 -1
- package/kit-android/gradlew.bat +94 -0
- package/kit-android/src/fromcore/kotlin/org/nativescript/kit/CoreBridge.kt +67 -1
- package/kit-android/src/main/kotlin/org/nativescript/kit/Signals.kt +15 -2
- package/kit-android/src/main/kotlin/org/nativescript/kit/runtime/Prototypes.kt +26 -0
- package/kit-apple/Overlays/xml-free/Core/ui.core.bindable.index.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity1.swift +26 -161
- package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity2.swift +9 -284
- package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity3Plus.swift +11 -7
- package/kit-apple/Sources/NativeScriptKit/Core/Object_callback_thisArg_once.swift +1 -1
- package/kit-apple/Sources/NativeScriptKit/Core/Object_eventName_object_index_section_view_ios_android.swift +7 -5
- package/kit-apple/Sources/NativeScriptKit/Core/Object_eventName_object_oldIndex_newIndex.swift +4 -2
- package/kit-apple/Sources/NativeScriptKit/Core/{Object_iterator_prototype_for_keyFor_hasInstance_isConcatSpreadable_and_19_more_91b5f3.swift → Object_iterator_prototype_for_keyFor_hasInstance_isConcatSpreadable_and_19_more_fa18e1.swift} +28 -103
- package/kit-apple/Sources/NativeScriptKit/Core/__Namespace.Utils.swift +17 -15
- package/kit-apple/Sources/NativeScriptKit/Core/application-settings.index.ios.swift +8 -8
- package/kit-apple/Sources/NativeScriptKit/Core/application.application-common.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/color.color-common.swift +7 -7
- package/kit-apple/Sources/NativeScriptKit/Core/data.observable.index.swift +3 -3
- package/kit-apple/Sources/NativeScriptKit/Core/http.http-request-internal.index.ios.swift +2 -1
- package/kit-apple/Sources/NativeScriptKit/Core/image-source.index.ios.swift +1 -1
- package/kit-apple/Sources/NativeScriptKit/Core/kit-index.json +40 -246
- package/kit-apple/Sources/NativeScriptKit/Core/manifest.json +1 -2
- package/kit-apple/Sources/NativeScriptKit/Core/ui.action-bar.index.ios.swift +3 -3
- package/kit-apple/Sources/NativeScriptKit/Core/ui.button.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.content-view.index.swift +8 -8
- package/kit-apple/Sources/NativeScriptKit/Core/ui.core.bindable.index.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.core.view.index.ios.swift +8 -8
- package/kit-apple/Sources/NativeScriptKit/Core/ui.core.view.view-common.swift +1 -3
- package/kit-apple/Sources/NativeScriptKit/Core/ui.core.view.view-helper.view-helper-common.swift +7 -7
- package/kit-apple/Sources/NativeScriptKit/Core/ui.core.weak-event-listener.index.swift +3 -3
- package/kit-apple/Sources/NativeScriptKit/Core/ui.frame.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.gestures.index.ios.swift +2 -2
- package/kit-apple/Sources/NativeScriptKit/Core/ui.html-view.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.image.image-common.swift +2 -2
- package/kit-apple/Sources/NativeScriptKit/Core/ui.image.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.label.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.absolute-layout.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.dock-layout.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.flexbox-layout.index.ios.swift +3 -3
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.grid-layout.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.stack-layout.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.wrap-layout.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.list-view.index.ios.swift +3 -3
- package/kit-apple/Sources/NativeScriptKit/Core/ui.page.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.repeater.index.swift +10 -6
- package/kit-apple/Sources/NativeScriptKit/Core/ui.scroll-view.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.split-view.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.styling.background.ios.swift +1 -1
- package/kit-apple/Sources/NativeScriptKit/Core/ui.tab-view.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.text-base.formatted-string.swift +2 -1
- package/kit-apple/Sources/NativeScriptKit/Core/utils.common.swift +9 -9
- package/kit-apple/Sources/NativeScriptKit/Core/utils.layout-helper.index.ios.swift +10 -10
- package/kit-apple/Sources/NativeScriptKit/CoreBridge.swift +67 -1
- package/kit-apple/Sources/NativeScriptKit/Runtime/Lowering.swift +4 -0
- package/kit-apple/Sources/NativeScriptKit/Runtime/Prototypes.swift +25 -0
- package/kit-apple/Sources/NativeScriptKit/Runtime/RegExp.swift +6 -0
- package/kit-apple/Sources/NativeScriptKit/Signals.swift +20 -2
- package/package.json +2 -1
- package/kit-apple/Sources/NativeScriptKit/Core/Object_toString_charAt_charCodeAt_concat_indexOf_lastIndexOf_and_44_more_fc25df.swift +0 -481
|
@@ -1,12 +1,61 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
2
3
|
import { existsSync, statSync } from 'node:fs';
|
|
3
|
-
import { dirname, resolve, sep } from 'node:path';
|
|
4
|
+
import { dirname, isAbsolute, resolve, sep } from 'node:path';
|
|
4
5
|
/** Packages whose resolution always belongs to the framework/core/kit rather than a monorepo tsconfig path alias. */
|
|
5
|
-
const PACKAGE_ONLY = /^(@nativescript\/(core|types|types-ios|types-android|types-minimal|angular|vue|svelte|react|solid|webpack|vite|tailwind|android|ios)|@angular\/|vue|svelte|react|react-dom|solid-js|tslib|typescript)(\/|$)/;
|
|
6
|
+
const PACKAGE_ONLY = /^(@nativescript\/(core|types|types-ios|types-android|types-minimal|angular|vue|svelte|react|react-dom|solid|webpack|vite|tailwind|android|ios)|@angular\/|vue|svelte|react|react-dom|solid-js|tslib|typescript)(\/|$)/;
|
|
7
|
+
const viteAliasCache = new Map();
|
|
8
|
+
/** App-level `resolve.alias` entries from a Vite config (`vite.config.*`) that point to workspace TypeScript sources. */
|
|
9
|
+
function viteAliases(root) {
|
|
10
|
+
const cached = viteAliasCache.get(root);
|
|
11
|
+
if (cached)
|
|
12
|
+
return cached;
|
|
13
|
+
const configFile = ['vite.config.mts', 'vite.config.ts', 'vite.config.mjs', 'vite.config.js'].map((f) => resolve(root, f)).find(existsSync);
|
|
14
|
+
if (!configFile) {
|
|
15
|
+
viteAliasCache.set(root, []);
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
const out = [];
|
|
19
|
+
try {
|
|
20
|
+
const script = `
|
|
21
|
+
import { createRequire } from 'node:module';
|
|
22
|
+
import { pathToFileURL } from 'node:url';
|
|
23
|
+
const req = createRequire(${JSON.stringify(resolve(root, 'package.json'))});
|
|
24
|
+
const vite = await import(pathToFileURL(req.resolve('vite')).href);
|
|
25
|
+
const cfg = await vite.resolveConfig({ configFile: ${JSON.stringify(configFile)}, mode: 'production' }, 'build', 'production', 'production');
|
|
26
|
+
const list = (cfg.resolve?.alias ?? []).map((a) => ({
|
|
27
|
+
find: a.find instanceof RegExp ? { re: a.find.source, flags: a.find.flags } : a.find,
|
|
28
|
+
replacement: a.replacement,
|
|
29
|
+
}));
|
|
30
|
+
process.stdout.write('\\n__NS_VITE_ALIASES__' + JSON.stringify(list) + '__END__\\n');
|
|
31
|
+
`;
|
|
32
|
+
const stdout = execFileSync(process.execPath, ['--input-type=module', '-e', script], {
|
|
33
|
+
cwd: root,
|
|
34
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
35
|
+
env: { ...process.env, VITE_CONFIG_NATIVE_IGNORE_WARNING: 'true' },
|
|
36
|
+
timeout: 15_000,
|
|
37
|
+
}).toString('utf8');
|
|
38
|
+
const m = /__NS_VITE_ALIASES__([\s\S]*?)__END__/.exec(stdout);
|
|
39
|
+
if (m) {
|
|
40
|
+
for (const entry of JSON.parse(m[1])) {
|
|
41
|
+
const rep = entry.replacement;
|
|
42
|
+
if (!rep || !isAbsolute(rep) || rep.startsWith('/@fs/') || /[\\/]node_modules[\\/]/.test(rep))
|
|
43
|
+
continue;
|
|
44
|
+
const find = typeof entry.find === 'string' ? entry.find : new RegExp(entry.find.re, entry.find.flags);
|
|
45
|
+
out.push({ find, replacement: rep });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
// Fall back cleanly to tsconfig paths if Vite cannot load in this environment.
|
|
51
|
+
}
|
|
52
|
+
viteAliasCache.set(root, out);
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
6
55
|
/**
|
|
7
|
-
* The `compilerOptions.paths` of the tsconfig.json nearest `from` (its `extends` followed),
|
|
8
|
-
*
|
|
9
|
-
*
|
|
56
|
+
* The `compilerOptions.paths` of the tsconfig.json nearest `from` (its `extends` followed), plus app-level Vite
|
|
57
|
+
* `resolve.alias` entries when a `vite.config.*` is present: `~/app/x` is `<app>/src/app/x` for `"~/*": ["./src/*"]`,
|
|
58
|
+
* and monorepo shared TypeScript aliases resolve to their workspace TypeScript sources.
|
|
10
59
|
* A specifier targeting `node_modules`, a built JS package directory, or core/framework packages is undefined.
|
|
11
60
|
*/
|
|
12
61
|
export function pathAliases(from) {
|
|
@@ -15,22 +64,41 @@ export function pathAliases(from) {
|
|
|
15
64
|
if (existsSync(resolve(dir, 'tsconfig.json')))
|
|
16
65
|
config = resolve(dir, 'tsconfig.json');
|
|
17
66
|
else if (dirname(dir) === dir)
|
|
18
|
-
|
|
67
|
+
break;
|
|
19
68
|
}
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const patterns = Object.entries(options.paths).map(([pattern, targets]) => {
|
|
69
|
+
const root = config ? dirname(config) : resolve(from);
|
|
70
|
+
const vAliases = viteAliases(root);
|
|
71
|
+
const options = config ? ts.getParsedCommandLineOfConfigFile(config, {}, { ...ts.sys, onUnRecoverableConfigFileDiagnostic: () => { } })?.options : undefined;
|
|
72
|
+
const base = options?.baseUrl ?? options?.pathsBasePath ?? root;
|
|
73
|
+
const patterns = Object.entries(options?.paths ?? {}).map(([pattern, targets]) => {
|
|
26
74
|
const star = pattern.indexOf('*');
|
|
27
75
|
return { prefix: star < 0 ? pattern : pattern.slice(0, star), suffix: star < 0 ? null : pattern.slice(star + 1), targets };
|
|
28
76
|
});
|
|
29
77
|
// As TypeScript matches them: an exact pattern first, then the longest prefix.
|
|
30
78
|
patterns.sort((a, b) => Number(a.suffix !== null) - Number(b.suffix !== null) || b.prefix.length - a.prefix.length);
|
|
79
|
+
if (!patterns.length && !vAliases.length)
|
|
80
|
+
return () => undefined;
|
|
31
81
|
const isFile = (p) => existsSync(p) && statSync(p).isFile();
|
|
32
82
|
const names = (path) => ['', '.ts', '.tsx', '.d.ts', '/index.ts', '/index.tsx'].some((x) => isFile(path + x)) || ['ios', 'android'].some((p) => isFile(`${path}.${p}.ts`) || isFile(`${path}/index.${p}.ts`));
|
|
33
83
|
return (spec) => {
|
|
84
|
+
if (!PACKAGE_ONLY.test(spec)) {
|
|
85
|
+
for (const a of vAliases) {
|
|
86
|
+
if (typeof a.find === 'string') {
|
|
87
|
+
if (spec === a.find && names(a.replacement))
|
|
88
|
+
return a.replacement;
|
|
89
|
+
if (spec.startsWith(a.find + '/')) {
|
|
90
|
+
const sub = resolve(a.replacement, spec.slice(a.find.length + 1));
|
|
91
|
+
if (!/[\\/]node_modules[\\/]/.test(sub) && names(sub))
|
|
92
|
+
return sub;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else if (a.find.test(spec)) {
|
|
96
|
+
const rep = spec.replace(a.find, a.replacement);
|
|
97
|
+
if (isAbsolute(rep) && !/[\\/]node_modules[\\/]/.test(rep) && names(rep))
|
|
98
|
+
return rep;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
34
102
|
for (const p of patterns) {
|
|
35
103
|
let star = '';
|
|
36
104
|
if (p.suffix === null) {
|
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { dirname, relative, resolve } from 'node:path';
|
|
4
|
+
import { NATIVE_CONTROLLERS, NATIVE_VIEWS } from "./core-facts.js";
|
|
5
|
+
/** `@nativescript/core` view classes that `getViewClass(tag)` resolves to a concrete class export. */
|
|
6
|
+
const CORE_VIEW_NAMES = new Set([
|
|
7
|
+
...Object.keys(NATIVE_VIEWS),
|
|
8
|
+
...Object.keys(NATIVE_CONTROLLERS),
|
|
9
|
+
'Repeater',
|
|
10
|
+
'StackLayout',
|
|
11
|
+
'GridLayout',
|
|
12
|
+
'FlexboxLayout',
|
|
13
|
+
'WrapLayout',
|
|
14
|
+
'DockLayout',
|
|
15
|
+
'AbsoluteLayout',
|
|
16
|
+
'ContentView',
|
|
17
|
+
'Placeholder',
|
|
18
|
+
'Frame',
|
|
19
|
+
]);
|
|
20
|
+
/**
|
|
21
|
+
* Desugars compile-time class patterns that native backends cannot express dynamically:
|
|
22
|
+
*
|
|
23
|
+
* 1. TypeScript class-factory mixins (`class Sub extends mixin(Base) { … }` where `mixin` returns
|
|
24
|
+
* `class Mixin extends Base { … }`): specializes `class __mixin_Mixin_Base extends Base { … }`
|
|
25
|
+
* in the subclass's file and stubs the generic mixin factory body when declared in a module.
|
|
26
|
+
* 2. Local `@CSSType(tag) class Local extends Base { … }` declarations inside a `for (const tag of CONST_LIST)`
|
|
27
|
+
* loop (including `const Base = getViewClass(tag)` for known `@nativescript/core` view classes):
|
|
28
|
+
* unrolls the loop and hoists one concrete top-level class per tag.
|
|
29
|
+
*
|
|
30
|
+
* All identifier and constructor rewrites are driven by the TypeScript AST so strings, comments,
|
|
31
|
+
* property keys, and shadowed locals are never altered.
|
|
32
|
+
*/
|
|
33
|
+
export function desugarClassMixins(text, fileName) {
|
|
34
|
+
if (!/\bclass\b/.test(text))
|
|
35
|
+
return text;
|
|
36
|
+
const sf = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, fileName.endsWith('x') ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
|
|
37
|
+
const edits = [];
|
|
38
|
+
const keepLines = (start, end, replacement) => {
|
|
39
|
+
const origLines = (text.slice(start, end).match(/\n/g) ?? []).length;
|
|
40
|
+
const newLines = (replacement.match(/\n/g) ?? []).length;
|
|
41
|
+
edits.push({ start, end, text: replacement + (origLines > newLines ? '\n'.repeat(origLines - newLines) : '') });
|
|
42
|
+
};
|
|
43
|
+
const existingImports = collectImportedNames(sf);
|
|
44
|
+
const extraImports = [];
|
|
45
|
+
const hoistedClasses = [];
|
|
46
|
+
const usedMixinImports = new Map();
|
|
47
|
+
// 1. Specialize `class Sub extends mixinFn(ActualBase) { ... }`
|
|
48
|
+
for (const st of sf.statements) {
|
|
49
|
+
if (!ts.isClassDeclaration(st))
|
|
50
|
+
continue;
|
|
51
|
+
const ext = st.heritageClauses?.find((h) => h.token === ts.SyntaxKind.ExtendsKeyword)?.types[0];
|
|
52
|
+
if (!ext || !ts.isCallExpression(ext.expression) || !ts.isIdentifier(ext.expression.expression) || ext.expression.arguments.length !== 1)
|
|
53
|
+
continue;
|
|
54
|
+
const arg = ext.expression.arguments[0];
|
|
55
|
+
if (!ts.isIdentifier(arg))
|
|
56
|
+
continue;
|
|
57
|
+
const mixinName = ext.expression.expression.text;
|
|
58
|
+
const found = findMixinFactory(sf, mixinName, fileName);
|
|
59
|
+
if (!found)
|
|
60
|
+
continue;
|
|
61
|
+
if (found.specifier)
|
|
62
|
+
usedMixinImports.set(found.specifier, (usedMixinImports.get(found.specifier) ?? 0) + 1);
|
|
63
|
+
const specialized = `__mixin_${found.className}_${arg.text}`;
|
|
64
|
+
const bodyText = specializeMixinBody(found.cls, found.sourceFile, found.paramName, arg.text);
|
|
65
|
+
for (const imp of found.imports) {
|
|
66
|
+
if (imp.names.every((n) => existingImports.has(n)))
|
|
67
|
+
continue;
|
|
68
|
+
for (const n of imp.names)
|
|
69
|
+
existingImports.add(n);
|
|
70
|
+
extraImports.push(imp.text);
|
|
71
|
+
}
|
|
72
|
+
edits.push({ start: st.getStart(sf), end: st.getStart(sf), text: `class ${specialized} extends ${arg.text} ${bodyText}\n` });
|
|
73
|
+
keepLines(ext.expression.getStart(sf), ext.expression.end, specialized);
|
|
74
|
+
}
|
|
75
|
+
// 1b. Stub exported generic mixin factory definitions (`export function f<T extends new (...args: any[]) => ...>(Base: T) { return class X extends Base { ... }; }`)
|
|
76
|
+
for (const st of sf.statements) {
|
|
77
|
+
if (!ts.isFunctionDeclaration(st) || !st.name || !st.body || !st.typeParameters?.length)
|
|
78
|
+
continue;
|
|
79
|
+
const shape = mixinFactoryShape(st);
|
|
80
|
+
if (!shape)
|
|
81
|
+
continue;
|
|
82
|
+
const retType = st.type ? '' : `: ${st.parameters[0].type ? st.parameters[0].type.getText(sf) : 'any'}`;
|
|
83
|
+
keepLines(st.body.getStart(sf), st.body.end, `${retType} { return ${shape.paramName}; }`);
|
|
84
|
+
}
|
|
85
|
+
// 2. Unroll `for (const tag of CONST_ARRAY)` containing a local `@CSSType(tag) class ...`
|
|
86
|
+
const constArrays = collectConstStringArrays(sf);
|
|
87
|
+
const coreViewImports = new Set();
|
|
88
|
+
const visitFn = (node) => {
|
|
89
|
+
if (ts.isForOfStatement(node) && ts.isVariableDeclarationList(node.initializer) && node.initializer.declarations.length === 1 && ts.isIdentifier(node.initializer.declarations[0].name) && ts.isBlock(node.statement)) {
|
|
90
|
+
const loopVar = node.initializer.declarations[0].name.text;
|
|
91
|
+
const tags = resolveStringArray(node.expression, constArrays);
|
|
92
|
+
if (tags?.length) {
|
|
93
|
+
const localCls = node.statement.statements.find(ts.isClassDeclaration);
|
|
94
|
+
if (localCls && localCls.name) {
|
|
95
|
+
const unrolled = unrollLocalClassLoop(sf, node, loopVar, tags, localCls, coreViewImports, hoistedClasses);
|
|
96
|
+
if (unrolled !== null) {
|
|
97
|
+
keepLines(node.getStart(sf), node.end, unrolled);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
ts.forEachChild(node, visitFn);
|
|
104
|
+
};
|
|
105
|
+
visitFn(sf);
|
|
106
|
+
if (!edits.length && !hoistedClasses.length)
|
|
107
|
+
return text;
|
|
108
|
+
for (const [spec, calls] of usedMixinImports) {
|
|
109
|
+
const decl = spec.parent.parent.parent;
|
|
110
|
+
let named = 0;
|
|
111
|
+
const count = (n) => {
|
|
112
|
+
if (ts.isIdentifier(n) && n.text === spec.name.text)
|
|
113
|
+
named++;
|
|
114
|
+
ts.forEachChild(n, count);
|
|
115
|
+
};
|
|
116
|
+
count(sf);
|
|
117
|
+
if (spec.parent.elements.length === 1 && !decl.importClause?.name && named === calls + 1)
|
|
118
|
+
keepLines(decl.getStart(sf), decl.end, '');
|
|
119
|
+
}
|
|
120
|
+
text = applyEdits(text, edits);
|
|
121
|
+
const coreImportLine = coreViewImports.size ? `import { ${[...coreViewImports].map((v) => `${v} as __nsCore_${v}`).join(', ')} } from '@nativescript/core';` : '';
|
|
122
|
+
const prefix = [...extraImports, ...(coreImportLine ? [coreImportLine] : [])].join(' ');
|
|
123
|
+
if (prefix)
|
|
124
|
+
text = `${prefix} ${text}`;
|
|
125
|
+
if (hoistedClasses.length)
|
|
126
|
+
text = `${text}\n${hoistedClasses.join('\n')}\n`;
|
|
127
|
+
return text;
|
|
128
|
+
}
|
|
129
|
+
function findMixinFactory(sf, name, fileName) {
|
|
130
|
+
for (const st of sf.statements) {
|
|
131
|
+
if (ts.isFunctionDeclaration(st) && st.name?.text === name) {
|
|
132
|
+
const shape = mixinFactoryShape(st);
|
|
133
|
+
return shape ? { ...shape, sourceFile: sf, imports: [] } : null;
|
|
134
|
+
}
|
|
135
|
+
if (!ts.isImportDeclaration(st) || !ts.isStringLiteral(st.moduleSpecifier) || !st.moduleSpecifier.text.startsWith('.') || st.importClause?.isTypeOnly)
|
|
136
|
+
continue;
|
|
137
|
+
const bindings = st.importClause?.namedBindings;
|
|
138
|
+
const spec = bindings && ts.isNamedImports(bindings) ? bindings.elements.find((e) => e.name.text === name && !e.isTypeOnly) : undefined;
|
|
139
|
+
if (!spec)
|
|
140
|
+
continue;
|
|
141
|
+
const base = resolve(dirname(fileName), st.moduleSpecifier.text);
|
|
142
|
+
const file = [base + '.ts', base + '/index.ts'].find((f) => existsSync(f));
|
|
143
|
+
if (!file)
|
|
144
|
+
return null;
|
|
145
|
+
const raw = readFileSync(file, 'utf8');
|
|
146
|
+
const other = ts.createSourceFile(file, raw, ts.ScriptTarget.Latest, true);
|
|
147
|
+
const exported = (spec.propertyName ?? spec.name).text;
|
|
148
|
+
const decl = other.statements.find((s) => ts.isFunctionDeclaration(s) && s.name?.text === exported && !!s.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword));
|
|
149
|
+
const shape = decl ? mixinFactoryShape(decl) : null;
|
|
150
|
+
if (!shape)
|
|
151
|
+
return null;
|
|
152
|
+
const imports = collectNeededImports(other, shape.cls, file, fileName);
|
|
153
|
+
return { ...shape, sourceFile: other, imports, specifier: spec };
|
|
154
|
+
}
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
function mixinFactoryShape(fn) {
|
|
158
|
+
if (!fn.body || fn.parameters.length !== 1 || !ts.isIdentifier(fn.parameters[0].name))
|
|
159
|
+
return null;
|
|
160
|
+
if (fn.body.statements.length !== 1)
|
|
161
|
+
return null;
|
|
162
|
+
const ret = fn.body.statements[0];
|
|
163
|
+
if (!ts.isReturnStatement(ret) || !ret.expression)
|
|
164
|
+
return null;
|
|
165
|
+
let expr = ret.expression;
|
|
166
|
+
while (ts.isParenthesizedExpression(expr))
|
|
167
|
+
expr = expr.expression;
|
|
168
|
+
if (!ts.isClassExpression(expr))
|
|
169
|
+
return null;
|
|
170
|
+
const paramName = fn.parameters[0].name.text;
|
|
171
|
+
const ext = expr.heritageClauses?.find((h) => h.token === ts.SyntaxKind.ExtendsKeyword)?.types[0];
|
|
172
|
+
if (!ext || !ts.isIdentifier(ext.expression) || ext.expression.text !== paramName)
|
|
173
|
+
return null;
|
|
174
|
+
return { className: expr.name?.text ?? fn.name?.text ?? 'Mixin', paramName, cls: expr };
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Extracts `{ ... }` from a mixin `ClassExpression`, replacing references to `paramName` with `baseName`
|
|
178
|
+
* and simplifying a forwarding `constructor(...args: any[]) { super(...args); ... }` to `constructor() { super(); ... }`.
|
|
179
|
+
*/
|
|
180
|
+
function specializeMixinBody(cls, sf, paramName, baseName) {
|
|
181
|
+
const full = sf.text;
|
|
182
|
+
const openBrace = full.indexOf('{', cls.getStart(sf));
|
|
183
|
+
if (openBrace < 0)
|
|
184
|
+
return '{}';
|
|
185
|
+
const bodyText = full.slice(openBrace, cls.end);
|
|
186
|
+
const edits = [];
|
|
187
|
+
// Simplify `constructor(...args: any[]) { super(...args); ... }` for concrete subclasses whose base takes 0 args.
|
|
188
|
+
for (const m of cls.members) {
|
|
189
|
+
if (!ts.isConstructorDeclaration(m) || !m.body || m.parameters.length !== 1)
|
|
190
|
+
continue;
|
|
191
|
+
const p = m.parameters[0];
|
|
192
|
+
if (!p.dotDotDotToken || !ts.isIdentifier(p.name))
|
|
193
|
+
continue;
|
|
194
|
+
const restName = p.name.text;
|
|
195
|
+
const first = m.body.statements[0];
|
|
196
|
+
if (!first || !ts.isExpressionStatement(first) || !ts.isCallExpression(first.expression) || first.expression.expression.kind !== ts.SyntaxKind.SuperKeyword)
|
|
197
|
+
continue;
|
|
198
|
+
const superArgs = first.expression.arguments;
|
|
199
|
+
if (superArgs.length !== 1 || !ts.isSpreadElement(superArgs[0]) || !ts.isIdentifier(superArgs[0].expression) || superArgs[0].expression.text !== restName)
|
|
200
|
+
continue;
|
|
201
|
+
edits.push({ start: p.getStart(sf) - openBrace, end: p.end - openBrace, text: '' });
|
|
202
|
+
edits.push({ start: superArgs[0].getStart(sf) - openBrace, end: superArgs[0].end - openBrace, text: '' });
|
|
203
|
+
}
|
|
204
|
+
const replacements = new Map([[paramName, baseName]]);
|
|
205
|
+
for (const m of cls.members)
|
|
206
|
+
collectIdentifierEdits(m, sf, openBrace, replacements, new Set(), edits);
|
|
207
|
+
return applyEdits(bodyText, edits);
|
|
208
|
+
}
|
|
209
|
+
function collectNeededImports(other, cls, otherFile, targetFile) {
|
|
210
|
+
const referenced = new Set();
|
|
211
|
+
const walk = (n) => {
|
|
212
|
+
if (ts.isIdentifier(n) && !isMemberNamePosition(n))
|
|
213
|
+
referenced.add(n.text);
|
|
214
|
+
ts.forEachChild(n, walk);
|
|
215
|
+
};
|
|
216
|
+
for (const m of cls.members)
|
|
217
|
+
walk(m);
|
|
218
|
+
const out = [];
|
|
219
|
+
for (const st of other.statements) {
|
|
220
|
+
if (!ts.isImportDeclaration(st) || !ts.isStringLiteral(st.moduleSpecifier) || !st.importClause)
|
|
221
|
+
continue;
|
|
222
|
+
const mod = st.moduleSpecifier.text;
|
|
223
|
+
const resolvedMod = mod.startsWith('.') ? resolve(dirname(otherFile), mod) : mod;
|
|
224
|
+
const specText = mod.startsWith('.') ? relativeSpecifier(dirname(targetFile), resolvedMod) : mod;
|
|
225
|
+
const bindings = st.importClause.namedBindings;
|
|
226
|
+
if (bindings && ts.isNamedImports(bindings)) {
|
|
227
|
+
const needed = bindings.elements.filter((el) => referenced.has(el.name.text));
|
|
228
|
+
if (needed.length) {
|
|
229
|
+
const typePrefix = st.importClause.isTypeOnly ? 'type ' : '';
|
|
230
|
+
out.push({
|
|
231
|
+
names: needed.map((el) => el.name.text),
|
|
232
|
+
text: `import ${typePrefix}{ ${needed.map((el) => el.getText(other)).join(', ')} } from ${JSON.stringify(specText)};`,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return out;
|
|
238
|
+
}
|
|
239
|
+
function relativeSpecifier(fromDir, absTarget) {
|
|
240
|
+
const rel = relative(fromDir, absTarget).replace(/\\/g, '/');
|
|
241
|
+
return rel.startsWith('.') ? rel : `./${rel}`;
|
|
242
|
+
}
|
|
243
|
+
function collectImportedNames(sf) {
|
|
244
|
+
const existing = new Set();
|
|
245
|
+
for (const st of sf.statements) {
|
|
246
|
+
if (!ts.isImportDeclaration(st))
|
|
247
|
+
continue;
|
|
248
|
+
if (st.importClause?.name)
|
|
249
|
+
existing.add(st.importClause.name.text);
|
|
250
|
+
const b = st.importClause?.namedBindings;
|
|
251
|
+
if (b && ts.isNamedImports(b))
|
|
252
|
+
for (const el of b.elements)
|
|
253
|
+
existing.add(el.name.text);
|
|
254
|
+
}
|
|
255
|
+
return existing;
|
|
256
|
+
}
|
|
257
|
+
function collectConstStringArrays(sf) {
|
|
258
|
+
const map = new Map();
|
|
259
|
+
for (const st of sf.statements) {
|
|
260
|
+
if (!ts.isVariableStatement(st) || !(st.declarationList.flags & ts.NodeFlags.Const))
|
|
261
|
+
continue;
|
|
262
|
+
for (const d of st.declarationList.declarations) {
|
|
263
|
+
if (!ts.isIdentifier(d.name) || !d.initializer)
|
|
264
|
+
continue;
|
|
265
|
+
const items = resolveStringArray(d.initializer, map);
|
|
266
|
+
if (items)
|
|
267
|
+
map.set(d.name.text, items);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return map;
|
|
271
|
+
}
|
|
272
|
+
function resolveStringArray(expr, known) {
|
|
273
|
+
const x = ts.skipOuterExpressions(expr);
|
|
274
|
+
if (ts.isIdentifier(x))
|
|
275
|
+
return known.get(x.text) ?? null;
|
|
276
|
+
if (!ts.isArrayLiteralExpression(x))
|
|
277
|
+
return null;
|
|
278
|
+
const out = [];
|
|
279
|
+
for (const el of x.elements) {
|
|
280
|
+
if (ts.isStringLiteralLike(el))
|
|
281
|
+
out.push(el.text);
|
|
282
|
+
else if (ts.isSpreadElement(el)) {
|
|
283
|
+
const nested = resolveStringArray(el.expression, known);
|
|
284
|
+
if (!nested)
|
|
285
|
+
return null;
|
|
286
|
+
out.push(...nested);
|
|
287
|
+
}
|
|
288
|
+
else
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
return out;
|
|
292
|
+
}
|
|
293
|
+
function isGetViewClassCall(stmt, baseIdent, loopVar) {
|
|
294
|
+
const decl = stmt.declarationList.declarations.find((d) => ts.isIdentifier(d.name) && d.name.text === baseIdent);
|
|
295
|
+
if (!decl?.initializer)
|
|
296
|
+
return false;
|
|
297
|
+
const init = ts.skipOuterExpressions(decl.initializer);
|
|
298
|
+
return ts.isCallExpression(init) && ts.isIdentifier(init.expression) && init.expression.text === 'getViewClass' && init.arguments.length === 1 && ts.isIdentifier(init.arguments[0]) && init.arguments[0].text === loopVar;
|
|
299
|
+
}
|
|
300
|
+
function unrollLocalClassLoop(sf, loop, loopVar, tags, localCls, coreViewImports, hoistedClasses) {
|
|
301
|
+
const block = loop.statement;
|
|
302
|
+
const clsName = localCls.name.text;
|
|
303
|
+
const ext = localCls.heritageClauses?.find((h) => h.token === ts.SyntaxKind.ExtendsKeyword)?.types[0];
|
|
304
|
+
if (!ext || !ts.isIdentifier(ext.expression))
|
|
305
|
+
return null;
|
|
306
|
+
const baseIdent = ext.expression.text;
|
|
307
|
+
const baseVarStmt = block.statements.find((s) => ts.isVariableStatement(s) &&
|
|
308
|
+
s.declarationList.declarations.some((d) => ts.isIdentifier(d.name) && d.name.text === baseIdent));
|
|
309
|
+
const isCoreViewLookup = !!baseVarStmt && isGetViewClassCall(baseVarStmt, baseIdent, loopVar) && tags.every((t) => CORE_VIEW_NAMES.has(t));
|
|
310
|
+
if (baseVarStmt && !isCoreViewLookup)
|
|
311
|
+
return null;
|
|
312
|
+
// Collect any other local `const x = ...` before `localCls` that `localCls`'s body references (e.g. `const items = itemsDescriptor(Base.prototype)`).
|
|
313
|
+
const localVars = [];
|
|
314
|
+
for (const s of block.statements) {
|
|
315
|
+
if (s === localCls)
|
|
316
|
+
break;
|
|
317
|
+
if (s === baseVarStmt)
|
|
318
|
+
continue;
|
|
319
|
+
if (!ts.isVariableStatement(s) || s.declarationList.declarations.length !== 1)
|
|
320
|
+
return null;
|
|
321
|
+
const d = s.declarationList.declarations[0];
|
|
322
|
+
if (!ts.isIdentifier(d.name) || !d.initializer)
|
|
323
|
+
return null;
|
|
324
|
+
localVars.push({ name: d.name.text, initializer: d.initializer });
|
|
325
|
+
}
|
|
326
|
+
const afterCls = block.statements.slice(block.statements.indexOf(localCls) + 1);
|
|
327
|
+
const bodyStmts = [];
|
|
328
|
+
for (const tag of tags) {
|
|
329
|
+
const safeTag = tag.replace(/[^A-Za-z0-9_]/g, '_');
|
|
330
|
+
const hoistedName = `__local_${clsName}_${safeTag}`;
|
|
331
|
+
const baseName = isCoreViewLookup ? `__nsCore_${tag}` : baseIdent;
|
|
332
|
+
if (isCoreViewLookup)
|
|
333
|
+
coreViewImports.add(tag);
|
|
334
|
+
const initMap = new Map([
|
|
335
|
+
[loopVar, JSON.stringify(tag)],
|
|
336
|
+
...(isCoreViewLookup ? [[baseIdent, baseName]] : []),
|
|
337
|
+
]);
|
|
338
|
+
const varRenames = new Map();
|
|
339
|
+
for (const lv of localVars) {
|
|
340
|
+
const modVar = `__local_${lv.name}_${safeTag}`;
|
|
341
|
+
varRenames.set(lv.name, modVar);
|
|
342
|
+
hoistedClasses.push(`let ${modVar}: any;`);
|
|
343
|
+
bodyStmts.push(`${modVar} = ${rewriteNode(lv.initializer, sf, initMap)};`);
|
|
344
|
+
}
|
|
345
|
+
const clsMap = new Map([
|
|
346
|
+
[loopVar, JSON.stringify(tag)],
|
|
347
|
+
...(isCoreViewLookup ? [[baseIdent, baseName]] : []),
|
|
348
|
+
...varRenames,
|
|
349
|
+
]);
|
|
350
|
+
hoistedClasses.push(rewriteNode(localCls, sf, clsMap, { renameClass: [clsName, hoistedName], ignoreAccessors: isCoreViewLookup }));
|
|
351
|
+
const afterMap = new Map([
|
|
352
|
+
[clsName, hoistedName],
|
|
353
|
+
[loopVar, JSON.stringify(tag)],
|
|
354
|
+
...(isCoreViewLookup ? [[baseIdent, baseName]] : []),
|
|
355
|
+
...varRenames,
|
|
356
|
+
]);
|
|
357
|
+
for (const s of afterCls)
|
|
358
|
+
bodyStmts.push(rewriteNode(s, sf, afterMap));
|
|
359
|
+
}
|
|
360
|
+
return `{ ${bodyStmts.join(' ')} }`;
|
|
361
|
+
}
|
|
362
|
+
/** Returns true when `id` is a property/member name declaration or access (`obj.id`, `{ id: v }`, `get id()`), not a variable reference. */
|
|
363
|
+
function isMemberNamePosition(id) {
|
|
364
|
+
const p = id.parent;
|
|
365
|
+
if (ts.isPropertyAccessExpression(p) && p.name === id)
|
|
366
|
+
return true;
|
|
367
|
+
if ((ts.isPropertyAssignment(p) || ts.isPropertyDeclaration(p) || ts.isPropertySignature(p) || ts.isMethodDeclaration(p) || ts.isMethodSignature(p) || ts.isGetAccessorDeclaration(p) || ts.isSetAccessorDeclaration(p)) && p.name === id)
|
|
368
|
+
return true;
|
|
369
|
+
if (ts.isQualifiedName(p) && p.right === id)
|
|
370
|
+
return true;
|
|
371
|
+
if (ts.isLabeledStatement(p) && p.label === id)
|
|
372
|
+
return true;
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
/** Collects AST-accurate identifier replacement edits inside `node`, respecting shadowing and member names. */
|
|
376
|
+
function collectIdentifierEdits(node, sf, baseOffset, replacements, shadowed, edits, opts) {
|
|
377
|
+
if (ts.isClassDeclaration(node) && node.name && opts?.renameClass && node.name.text === opts.renameClass[0]) {
|
|
378
|
+
edits.push({ start: node.name.getStart(sf) - baseOffset, end: node.name.end - baseOffset, text: opts.renameClass[1] });
|
|
379
|
+
}
|
|
380
|
+
if (ts.isFunctionLike(node)) {
|
|
381
|
+
const nextShadow = new Set(shadowed);
|
|
382
|
+
for (const param of node.parameters)
|
|
383
|
+
collectBindingNames(param.name, nextShadow);
|
|
384
|
+
if ('body' in node && node.body && ts.isBlock(node.body)) {
|
|
385
|
+
for (const st of node.body.statements) {
|
|
386
|
+
if (ts.isVariableStatement(st)) {
|
|
387
|
+
for (const d of st.declarationList.declarations)
|
|
388
|
+
collectBindingNames(d.name, nextShadow);
|
|
389
|
+
}
|
|
390
|
+
else if (ts.isFunctionDeclaration(st) && st.name)
|
|
391
|
+
nextShadow.add(st.name.text);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
ts.forEachChild(node, (child) => {
|
|
395
|
+
if (child === node.name)
|
|
396
|
+
return;
|
|
397
|
+
if (node.parameters.includes(child)) {
|
|
398
|
+
const param = child;
|
|
399
|
+
if (param.type)
|
|
400
|
+
collectIdentifierEdits(param.type, sf, baseOffset, replacements, shadowed, edits);
|
|
401
|
+
if (param.initializer)
|
|
402
|
+
collectIdentifierEdits(param.initializer, sf, baseOffset, replacements, shadowed, edits);
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
collectIdentifierEdits(child, sf, baseOffset, replacements, nextShadow, edits);
|
|
406
|
+
});
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (ts.isShorthandPropertyAssignment(node)) {
|
|
410
|
+
const rep = !shadowed.has(node.name.text) ? replacements.get(node.name.text) : undefined;
|
|
411
|
+
if (rep !== undefined) {
|
|
412
|
+
edits.push({ start: node.name.getStart(sf) - baseOffset, end: node.name.end - baseOffset, text: `${node.name.text}: ${rep}` });
|
|
413
|
+
}
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (ts.isIdentifier(node)) {
|
|
417
|
+
if (isMemberNamePosition(node))
|
|
418
|
+
return;
|
|
419
|
+
if (ts.isClassDeclaration(node.parent) && node.parent.name === node)
|
|
420
|
+
return;
|
|
421
|
+
if (ts.isVariableDeclaration(node.parent) && node.parent.name === node)
|
|
422
|
+
return;
|
|
423
|
+
if (shadowed.has(node.text))
|
|
424
|
+
return;
|
|
425
|
+
const rep = replacements.get(node.text);
|
|
426
|
+
if (rep !== undefined) {
|
|
427
|
+
edits.push({ start: node.getStart(sf) - baseOffset, end: node.end - baseOffset, text: rep });
|
|
428
|
+
}
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
ts.forEachChild(node, (child) => collectIdentifierEdits(child, sf, baseOffset, replacements, shadowed, edits));
|
|
432
|
+
}
|
|
433
|
+
function collectBindingNames(name, out) {
|
|
434
|
+
if (ts.isIdentifier(name))
|
|
435
|
+
out.add(name.text);
|
|
436
|
+
else
|
|
437
|
+
for (const el of name.elements)
|
|
438
|
+
if (!ts.isOmittedExpression(el))
|
|
439
|
+
collectBindingNames(el.name, out);
|
|
440
|
+
}
|
|
441
|
+
function rewriteNode(node, sf, replacements, opts) {
|
|
442
|
+
const baseOffset = node.getStart(sf);
|
|
443
|
+
const edits = [];
|
|
444
|
+
if (opts?.ignoreAccessors && ts.isClassDeclaration(node)) {
|
|
445
|
+
for (const m of node.members) {
|
|
446
|
+
if (ts.isGetAccessorDeclaration(m) || ts.isSetAccessorDeclaration(m)) {
|
|
447
|
+
const at = m.getStart(sf) - baseOffset;
|
|
448
|
+
edits.push({ start: at, end: at, text: '// @ts-ignore\n' });
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
collectIdentifierEdits(node, sf, baseOffset, replacements, new Set(), edits, opts);
|
|
453
|
+
return applyEdits(sf.text.slice(baseOffset, node.end), edits);
|
|
454
|
+
}
|
|
455
|
+
function applyEdits(source, edits) {
|
|
456
|
+
if (!edits.length)
|
|
457
|
+
return source;
|
|
458
|
+
const sorted = [...edits].sort((a, b) => b.start - a.start || b.end - a.end);
|
|
459
|
+
let out = source;
|
|
460
|
+
for (const e of sorted)
|
|
461
|
+
out = out.slice(0, e.start) + e.text + out.slice(e.end);
|
|
462
|
+
return out;
|
|
463
|
+
}
|
|
@@ -54,6 +54,13 @@ export function coreBarrels(root) {
|
|
|
54
54
|
export function addedDeclarations(decls) {
|
|
55
55
|
return decls.length > 0 && decls.every((d) => !isCoreDeclaration(d) && (!d.getSourceFile().isDeclarationFile || ts.isExternalModule(d.getSourceFile())));
|
|
56
56
|
}
|
|
57
|
+
/** Whether `callee` is an instance method declared on a kit type (`target.method`). */
|
|
58
|
+
export function isKitInstanceMethod(core, callee) {
|
|
59
|
+
if (!core || !ts.isPropertyAccessExpression(callee))
|
|
60
|
+
return false;
|
|
61
|
+
const owner = core.owner(callee.expression);
|
|
62
|
+
return !!owner && !owner.isStatic && core.kitMember(owner.name, callee.name.text)?.kind === 'func';
|
|
63
|
+
}
|
|
57
64
|
/** A property core's typings declare that the kit's class lacks and leaves to each object's own properties (see `CoreAPI.ownPropertyOnly`). */
|
|
58
65
|
export function ownPropertyOnly(index, owner, name, sym) {
|
|
59
66
|
if (kitMember(index, owner, name.text) || !kitMember(index, owner, 'jsExpando'))
|