@panomc/sdk 1.0.0-dev.9 → 2.0.0-dev.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/api/routes.core.json +3702 -0
- package/bin/api/__tests__/api-compat.test.mjs +97 -0
- package/bin/api/__tests__/check-paths.test.mjs +113 -0
- package/bin/api/__tests__/cli.test.mjs +44 -0
- package/bin/api/__tests__/extract-routes.test.mjs +132 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/api/paths.old-new.json +82 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
- package/bin/api/__tests__/fixtures/plugin-new/api/paths.old-new.json +66 -0
- package/bin/api/__tests__/fixtures/plugin-new/build.gradle.kts +42 -0
- package/bin/api/__tests__/fixtures/plugin-new/gradle.properties +4 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/panel/Things.js +4 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/theme/Logo.svelte +9 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/theme/api.js +29 -0
- package/bin/api/__tests__/fixtures/plugin-old/build.gradle.kts +22 -0
- package/bin/api/__tests__/fixtures/plugin-old/gradle.properties +4 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/panel/Things.js +4 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/theme/Logo.svelte +9 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/theme/api.js +29 -0
- package/bin/api/__tests__/fixtures/refusals/gradle.properties +1 -0
- package/bin/api/__tests__/fixtures/refusals/src/main/kotlin/x/Bad.kt +39 -0
- package/bin/api/__tests__/fixtures/unresolved/src/main/kotlin/x/Orphan.kt +19 -0
- package/bin/api/__tests__/helpers.mjs +48 -0
- package/bin/api/__tests__/migrate-v1.test.mjs +377 -0
- package/bin/api/__tests__/openapi-snapshot.test.mjs +120 -0
- package/bin/api/api-compat.mjs +204 -0
- package/bin/api/check-paths.mjs +135 -0
- package/bin/api/cli.mjs +83 -0
- package/bin/api/extract-routes.mjs +129 -0
- package/bin/api/js-scan.mjs +337 -0
- package/bin/api/kotlin.mjs +366 -0
- package/bin/api/migrate-v1.mjs +330 -0
- package/bin/api/openapi-snapshot.mjs +112 -0
- package/bin/api/project.mjs +172 -0
- package/bin/api/rename-table.mjs +66 -0
- package/bin/api/util.mjs +194 -0
- package/bin/pano-api.js +5 -0
- package/build/plugin-api-rollup.js +46 -0
- package/core/css/pano-tokens.css +225 -0
- package/core/favicon/apple-touch-icon.png +0 -0
- package/core/favicon/favicon-96x96.png +0 -0
- package/core/favicon/favicon.ico +0 -0
- package/core/favicon/favicon.svg +1 -0
- package/core/favicon/site.webmanifest +21 -0
- package/core/favicon/web-app-manifest-192x192.png +0 -0
- package/core/favicon/web-app-manifest-512x512.png +0 -0
- package/core/favicon_old/android-icon-144x144.png +0 -0
- package/core/favicon_old/android-icon-192x192.png +0 -0
- package/core/favicon_old/android-icon-36x36.png +0 -0
- package/core/favicon_old/android-icon-48x48.png +0 -0
- package/core/favicon_old/android-icon-72x72.png +0 -0
- package/core/favicon_old/android-icon-96x96.png +0 -0
- package/core/favicon_old/apple-icon-114x114.png +0 -0
- package/core/favicon_old/apple-icon-120x120.png +0 -0
- package/core/favicon_old/apple-icon-144x144.png +0 -0
- package/core/favicon_old/apple-icon-152x152.png +0 -0
- package/core/favicon_old/apple-icon-180x180.png +0 -0
- package/core/favicon_old/apple-icon-57x57.png +0 -0
- package/core/favicon_old/apple-icon-60x60.png +0 -0
- package/core/favicon_old/apple-icon-72x72.png +0 -0
- package/core/favicon_old/apple-icon-76x76.png +0 -0
- package/core/favicon_old/apple-icon-precomposed.png +0 -0
- package/core/favicon_old/apple-icon.png +0 -0
- package/core/favicon_old/browserconfig.xml +11 -0
- package/core/favicon_old/favicon-16x16.png +0 -0
- package/core/favicon_old/favicon-32x32.png +0 -0
- package/core/favicon_old/favicon-96x96.png +0 -0
- package/core/favicon_old/favicon.ico +0 -0
- package/core/favicon_old/manifest.json +41 -0
- package/core/favicon_old/ms-icon-144x144.png +0 -0
- package/core/favicon_old/ms-icon-150x150.png +0 -0
- package/core/favicon_old/ms-icon-310x310.png +0 -0
- package/core/favicon_old/ms-icon-70x70.png +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.svg +483 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff2 +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.svg +486 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff2 +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.svg +484 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff2 +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.svg +486 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff2 +0 -0
- package/core/js/ControllerRegistry.js +421 -0
- package/core/js/NotificationManager.js +62 -0
- package/core/js/PluginAPI.js +39 -0
- package/core/js/PluginManager.js +851 -0
- package/core/js/RouteMatcher.js +142 -0
- package/core/js/__tests__/ControllerRegistry.test.js +378 -0
- package/core/js/__tests__/NotificationManager.test.js +57 -0
- package/core/js/__tests__/PluginAPI.test.js +25 -0
- package/core/js/__tests__/RouteMatcher.test.js +33 -0
- package/core/js/__tests__/accessClient.test.js +582 -0
- package/core/js/__tests__/api.util.v1.test.js +336 -0
- package/core/js/__tests__/appStores.test.js +47 -0
- package/core/js/__tests__/devBounce.test.js +28 -0
- package/core/js/__tests__/html.util.test.js +18 -0
- package/core/js/__tests__/pluginFolder.util.test.js +106 -0
- package/core/js/__tests__/pluginPackage.test.js +134 -0
- package/core/js/__tests__/pluginUiZip.test.js +47 -0
- package/core/js/__tests__/sdkLevel.test.js +34 -0
- package/core/js/api.util.js +433 -0
- package/core/js/appStores.js +93 -0
- package/core/js/basePath.js +10 -0
- package/core/js/html.util.js +26 -0
- package/core/js/pluginFolder.util.js +95 -0
- package/core/js/tooltip.util.js +59 -0
- package/core/js/variables.js +95 -0
- package/core/js/version.util.js +100 -0
- package/core/scss/_pano-tokens.scss +51 -0
- package/core/scss/custom-bootstrap.scss +210 -0
- package/core/scss/fonts.scss +85 -0
- package/core/scss/main.scss +389 -0
- package/core/scss/themes.scss +122 -0
- package/package.json +63 -11
- package/src/__tests__/controllers.test.js +220 -0
- package/src/__tests__/views.spike.test.js +472 -0
- package/src/components/{index.js → panel.js} +9 -7
- package/src/components/theme.js +18 -0
- package/src/controllers.js +127 -0
- package/src/index.js +1 -1
- package/src/runtime-specifiers.js +88 -0
- package/src/svelte.js +3 -2
- package/src/types.js +5 -3
- package/src/utils/api.js +42 -1
- package/src/utils/auth.js +8 -0
- package/src/utils/component.js +4 -2
- package/src/utils/route.js +16 -0
- package/src/utils/text.js +8 -0
- package/src/views.js +29 -0
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
// A small Kotlin reader for the pano-api CLI: it masks comments and strings, finds classes with their supertypes,
|
|
2
|
+
// `const val` string constants and `Path(...)` calls. It is not a Kotlin parser; it understands exactly what
|
|
3
|
+
// the endpoint scan and the codemod need and fails loudly on the rest.
|
|
4
|
+
import { lineIndex } from './util.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Skips a Kotlin string literal starting at the opening quote; returns the index after the closing quote.
|
|
8
|
+
* @param {string} s @param {number} i
|
|
9
|
+
*/
|
|
10
|
+
function skipString(s, i) {
|
|
11
|
+
if (s.startsWith('"""', i)) {
|
|
12
|
+
const end = s.indexOf('"""', i + 3);
|
|
13
|
+
if (end === -1) return s.length;
|
|
14
|
+
let e = end + 3;
|
|
15
|
+
while (s[e] === '"') e++;
|
|
16
|
+
return e;
|
|
17
|
+
}
|
|
18
|
+
let j = i + 1;
|
|
19
|
+
while (j < s.length) {
|
|
20
|
+
const c = s[j];
|
|
21
|
+
if (c === '\\') j += 2;
|
|
22
|
+
else if (c === '"') return j + 1;
|
|
23
|
+
else if (c === '$' && s[j + 1] === '{') {
|
|
24
|
+
let depth = 0;
|
|
25
|
+
let k = j + 1;
|
|
26
|
+
for (; k < s.length; k++) {
|
|
27
|
+
if (s[k] === '"') k = skipString(s, k) - 1;
|
|
28
|
+
else if (s[k] === '{') depth++;
|
|
29
|
+
else if (s[k] === '}' && --depth === 0) break;
|
|
30
|
+
}
|
|
31
|
+
j = k + 1;
|
|
32
|
+
} else j++;
|
|
33
|
+
}
|
|
34
|
+
return s.length;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Masks a Kotlin source. `text` has comments blanked (strings intact); `code` additionally has string contents
|
|
39
|
+
* replaced by `x` so brace and paren matching never trips over a quoted bracket. Offsets are identical to `src`.
|
|
40
|
+
* @param {string} src
|
|
41
|
+
* @returns {{ text: string, code: string }}
|
|
42
|
+
*/
|
|
43
|
+
export function maskKotlin(src) {
|
|
44
|
+
const text = src.split('');
|
|
45
|
+
const code = src.split('');
|
|
46
|
+
const blank = (arr, from, to) => {
|
|
47
|
+
for (let k = from; k < to; k++) if (arr[k] !== '\n' && arr[k] !== '\r') arr[k] = ' ';
|
|
48
|
+
};
|
|
49
|
+
let i = 0;
|
|
50
|
+
const n = src.length;
|
|
51
|
+
while (i < n) {
|
|
52
|
+
const c = src[i];
|
|
53
|
+
if (c === '/' && src[i + 1] === '/') {
|
|
54
|
+
let e = src.indexOf('\n', i);
|
|
55
|
+
if (e === -1) e = n;
|
|
56
|
+
blank(text, i, e);
|
|
57
|
+
blank(code, i, e);
|
|
58
|
+
i = e;
|
|
59
|
+
} else if (c === '/' && src[i + 1] === '*') {
|
|
60
|
+
// KDoc and block comments nest in Kotlin.
|
|
61
|
+
let depth = 1;
|
|
62
|
+
let j = i + 2;
|
|
63
|
+
while (j < n && depth > 0) {
|
|
64
|
+
if (src[j] === '/' && src[j + 1] === '*') {
|
|
65
|
+
depth++;
|
|
66
|
+
j += 2;
|
|
67
|
+
} else if (src[j] === '*' && src[j + 1] === '/') {
|
|
68
|
+
depth--;
|
|
69
|
+
j += 2;
|
|
70
|
+
} else j++;
|
|
71
|
+
}
|
|
72
|
+
blank(text, i, j);
|
|
73
|
+
blank(code, i, j);
|
|
74
|
+
i = j;
|
|
75
|
+
} else if (c === '"') {
|
|
76
|
+
const e = skipString(src, i);
|
|
77
|
+
const q = src.startsWith('"""', i) ? 3 : 1;
|
|
78
|
+
for (let k = i + q; k < e - q; k++) if (code[k] !== '\n' && code[k] !== '\r') code[k] = 'x';
|
|
79
|
+
i = e;
|
|
80
|
+
} else if (c === "'") {
|
|
81
|
+
const m = /^'(?:\\.[^']{0,6}|[^\\'])'/.exec(src.slice(i, i + 10));
|
|
82
|
+
if (m) {
|
|
83
|
+
for (let k = i + 1; k < i + m[0].length - 1; k++) code[k] = 'x';
|
|
84
|
+
i += m[0].length;
|
|
85
|
+
} else i++;
|
|
86
|
+
} else i++;
|
|
87
|
+
}
|
|
88
|
+
return { text: text.join(''), code: code.join('') };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Index of the bracket closing the one at `open`, or -1. Works on masked code.
|
|
93
|
+
* @param {string} code @param {number} open
|
|
94
|
+
*/
|
|
95
|
+
export function matchBracket(code, open) {
|
|
96
|
+
const pairs = { '(': ')', '{': '}', '[': ']', '<': '>' };
|
|
97
|
+
const o = code[open];
|
|
98
|
+
const c = pairs[o];
|
|
99
|
+
let depth = 0;
|
|
100
|
+
for (let i = open; i < code.length; i++) {
|
|
101
|
+
if (code[i] === o) depth++;
|
|
102
|
+
else if (code[i] === c) {
|
|
103
|
+
if (o === '<' && code[i - 1] === '-') continue;
|
|
104
|
+
if (--depth === 0) return i;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return -1;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Splits a masked range on top-level commas.
|
|
112
|
+
* @param {string} code @param {number} from @param {number} to
|
|
113
|
+
* @returns {{ start: number, end: number }[]}
|
|
114
|
+
*/
|
|
115
|
+
export function splitArgs(code, from, to) {
|
|
116
|
+
const out = [];
|
|
117
|
+
let depth = 0;
|
|
118
|
+
let start = from;
|
|
119
|
+
for (let i = from; i < to; i++) {
|
|
120
|
+
const c = code[i];
|
|
121
|
+
if (c === '(' || c === '[' || c === '{') depth++;
|
|
122
|
+
else if (c === ')' || c === ']' || c === '}') depth--;
|
|
123
|
+
else if (c === ',' && depth === 0) {
|
|
124
|
+
out.push({ start, end: i });
|
|
125
|
+
start = i + 1;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (code.slice(start, to).trim()) out.push({ start, end: to });
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const MODIFIERS =
|
|
133
|
+
'(?:abstract|open|data|inner|internal|private|public|protected|sealed|final|enum|annotation|value|fun|expect|actual)';
|
|
134
|
+
const ANNOTATIONS_BEFORE = new RegExp(`((?:@[\\w.]+(?::\\w+)?(?:\\([^)]*\\))?\\s*|\\b${MODIFIERS}\\s+)*)$`);
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @typedef {{ name: string, call: boolean, open: number, close: number }} SuperRef
|
|
138
|
+
* @typedef {{
|
|
139
|
+
* name: string, kind: string, start: number, line: number, supers: SuperRef[], body: [number, number] | null,
|
|
140
|
+
* annotations: string[], abstract: boolean, file: KotlinFile, owner: ClassInfo | null
|
|
141
|
+
* }} ClassInfo
|
|
142
|
+
* @typedef {{ name: string, owner: string | null, raw: string, start: number, end: number, file: KotlinFile }} ConstInfo
|
|
143
|
+
* @typedef {{
|
|
144
|
+
* path: string, rel: string, src: string, text: string, code: string, pkg: string, imports: string[],
|
|
145
|
+
* classes: ClassInfo[], consts: ConstInfo[], lineOf: (i: number) => number
|
|
146
|
+
* }} KotlinFile
|
|
147
|
+
*/
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Parses one Kotlin file.
|
|
151
|
+
* @param {string} src @param {string} filePath @param {string} relPath
|
|
152
|
+
* @returns {KotlinFile}
|
|
153
|
+
*/
|
|
154
|
+
export function parseKotlin(src, filePath, relPath) {
|
|
155
|
+
const { text, code } = maskKotlin(src);
|
|
156
|
+
const lineOf = lineIndex(src);
|
|
157
|
+
const pkg = /^\s*package\s+([\w.]+)/m.exec(code)?.[1] ?? '';
|
|
158
|
+
const imports = [...code.matchAll(/^\s*import\s+([\w.*]+)(?:\s+as\s+\w+)?/gm)].map((m) => m[1]);
|
|
159
|
+
/** @type {KotlinFile} */
|
|
160
|
+
const file = { path: filePath, rel: relPath, src, text, code, pkg, imports, classes: [], consts: [], lineOf };
|
|
161
|
+
|
|
162
|
+
const re = /(?<![:\w.])(?:(class|interface)|(object))\s+([A-Za-z_]\w*)/g;
|
|
163
|
+
let m;
|
|
164
|
+
while ((m = re.exec(code))) {
|
|
165
|
+
const kind = m[1] || m[2];
|
|
166
|
+
let pos = m.index + m[0].length;
|
|
167
|
+
const skipWs = () => {
|
|
168
|
+
while (pos < code.length && /\s/.test(code[pos])) pos++;
|
|
169
|
+
};
|
|
170
|
+
skipWs();
|
|
171
|
+
if (code[pos] === '<') {
|
|
172
|
+
const e = matchBracket(code, pos);
|
|
173
|
+
if (e === -1) continue;
|
|
174
|
+
pos = e + 1;
|
|
175
|
+
skipWs();
|
|
176
|
+
}
|
|
177
|
+
const ctor = /^(?:(?:private|internal|public|protected)\s+)?(?:constructor\s*)?\(/.exec(code.slice(pos, pos + 40));
|
|
178
|
+
if (ctor) {
|
|
179
|
+
const open = pos + ctor[0].length - 1;
|
|
180
|
+
const close = matchBracket(code, open);
|
|
181
|
+
if (close === -1) continue;
|
|
182
|
+
pos = close + 1;
|
|
183
|
+
skipWs();
|
|
184
|
+
}
|
|
185
|
+
/** @type {SuperRef[]} */
|
|
186
|
+
const supers = [];
|
|
187
|
+
if (code[pos] === ':') {
|
|
188
|
+
pos++;
|
|
189
|
+
for (;;) {
|
|
190
|
+
skipWs();
|
|
191
|
+
const t = /^([A-Za-z_][\w.]*)/.exec(code.slice(pos));
|
|
192
|
+
if (!t) break;
|
|
193
|
+
pos += t[0].length;
|
|
194
|
+
if (code[pos] === '<') {
|
|
195
|
+
const e = matchBracket(code, pos);
|
|
196
|
+
if (e === -1) break;
|
|
197
|
+
pos = e + 1;
|
|
198
|
+
}
|
|
199
|
+
if (code[pos] === '?') pos++;
|
|
200
|
+
let wsPos = pos;
|
|
201
|
+
while (/\s/.test(code[wsPos] || '')) wsPos++;
|
|
202
|
+
let call = false;
|
|
203
|
+
let open = -1;
|
|
204
|
+
let close = -1;
|
|
205
|
+
if (code[wsPos] === '(') {
|
|
206
|
+
const e = matchBracket(code, wsPos);
|
|
207
|
+
if (e === -1) break;
|
|
208
|
+
call = true;
|
|
209
|
+
open = wsPos;
|
|
210
|
+
close = e;
|
|
211
|
+
pos = e + 1;
|
|
212
|
+
}
|
|
213
|
+
supers.push({ name: t[1].split('.').pop() || t[1], call, open, close });
|
|
214
|
+
wsPos = pos;
|
|
215
|
+
while (/\s/.test(code[wsPos] || '')) wsPos++;
|
|
216
|
+
if (code.startsWith('by', wsPos) && /\s/.test(code[wsPos + 2] || '')) {
|
|
217
|
+
// delegation: skip the expression up to the next top-level comma, brace or line end
|
|
218
|
+
let depth = 0;
|
|
219
|
+
let k = wsPos + 2;
|
|
220
|
+
for (; k < code.length; k++) {
|
|
221
|
+
const c = code[k];
|
|
222
|
+
if (c === '(' || c === '[') depth++;
|
|
223
|
+
else if (c === ')' || c === ']') depth--;
|
|
224
|
+
else if (depth === 0 && (c === ',' || c === '{' || c === '\n')) break;
|
|
225
|
+
}
|
|
226
|
+
pos = k;
|
|
227
|
+
wsPos = pos;
|
|
228
|
+
while (/\s/.test(code[wsPos] || '')) wsPos++;
|
|
229
|
+
}
|
|
230
|
+
if (code[wsPos] === ',') {
|
|
231
|
+
pos = wsPos + 1;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
skipWs();
|
|
237
|
+
}
|
|
238
|
+
const where = /^where\b/.exec(code.slice(pos, pos + 8));
|
|
239
|
+
if (where) {
|
|
240
|
+
const b = code.indexOf('{', pos);
|
|
241
|
+
if (b !== -1) pos = b;
|
|
242
|
+
}
|
|
243
|
+
/** @type {[number, number] | null} */
|
|
244
|
+
let body = null;
|
|
245
|
+
if (code[pos] === '{') {
|
|
246
|
+
const e = matchBracket(code, pos);
|
|
247
|
+
if (e !== -1) body = [pos, e];
|
|
248
|
+
}
|
|
249
|
+
const before = code.slice(Math.max(0, m.index - 600), m.index);
|
|
250
|
+
const tail = ANNOTATIONS_BEFORE.exec(before)?.[1] ?? '';
|
|
251
|
+
const annotations = [...tail.matchAll(/@([\w.]+)/g)].map((a) => a[1].split('.').pop() || a[1]);
|
|
252
|
+
file.classes.push({
|
|
253
|
+
name: m[3], kind, start: m.index, line: lineOf(m.index), supers, body, annotations,
|
|
254
|
+
abstract: /\babstract\b/.test(tail), file, owner: null,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
// nesting: the innermost class whose body contains the declaration is its owner
|
|
258
|
+
for (const c of file.classes) {
|
|
259
|
+
let best = null;
|
|
260
|
+
for (const o of file.classes) {
|
|
261
|
+
if (o === c || !o.body) continue;
|
|
262
|
+
if (c.start > o.body[0] && c.start < o.body[1] && (!best || o.body[0] > best.body[0])) best = o;
|
|
263
|
+
}
|
|
264
|
+
c.owner = best;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const cre = /\bconst\s+val\s+([A-Za-z_]\w*)\s*(?::\s*String)?\s*=\s*"/g;
|
|
268
|
+
while ((m = cre.exec(code))) {
|
|
269
|
+
const q = m.index + m[0].length - 1;
|
|
270
|
+
const end = skipString(src, q);
|
|
271
|
+
if (src.startsWith('"""', q)) continue;
|
|
272
|
+
let owner = null;
|
|
273
|
+
for (const o of file.classes) {
|
|
274
|
+
if (o.body && q > o.body[0] && q < o.body[1] && (!owner || o.body[0] > owner.body[0])) owner = o;
|
|
275
|
+
}
|
|
276
|
+
file.consts.push({ name: m[1], owner: owner ? owner.name : null, raw: text.slice(q + 1, end - 1), start: q + 1, end: end - 1, file });
|
|
277
|
+
}
|
|
278
|
+
return file;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Finds `Path(...)` calls inside a range of a parsed file.
|
|
283
|
+
* @param {KotlinFile} file @param {number} from @param {number} to
|
|
284
|
+
* @returns {{ start: number, line: number, expr: { start: number, end: number }, method: string }[]}
|
|
285
|
+
*/
|
|
286
|
+
export function findPathCalls(file, from, to) {
|
|
287
|
+
const out = [];
|
|
288
|
+
const re = /(?<![\w.])Path\s*\(/g;
|
|
289
|
+
const slice = file.code.slice(from, to);
|
|
290
|
+
let m;
|
|
291
|
+
while ((m = re.exec(slice))) {
|
|
292
|
+
const open = from + m.index + m[0].length - 1;
|
|
293
|
+
const close = matchBracket(file.code, open);
|
|
294
|
+
if (close === -1) continue;
|
|
295
|
+
const args = splitArgs(file.code, open + 1, close);
|
|
296
|
+
if (args.length < 2) continue;
|
|
297
|
+
const mt = /(?:RouteType\.)?([A-Z]+)\s*$/.exec(file.text.slice(args[1].start, args[1].end).trim());
|
|
298
|
+
if (!mt) continue;
|
|
299
|
+
out.push({ start: from + m.index, line: file.lineOf(from + m.index), expr: args[0], method: mt[1] === 'ROUTE' ? 'ANY' : mt[1] });
|
|
300
|
+
}
|
|
301
|
+
return out;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** @typedef {{ value: string, file: KotlinFile, start: number, end: number }} Piece */
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Evaluates a path expression (string literals with `$NAME` / `${NAME}` templates, constant references and `+`)
|
|
308
|
+
* into its string value plus the literal pieces it is made of (with file ranges, so the codemod can edit them).
|
|
309
|
+
* @param {KotlinFile} file
|
|
310
|
+
* @param {{ start: number, end: number }} expr
|
|
311
|
+
* @param {(file: KotlinFile, ref: string) => ConstInfo | null} resolveConst
|
|
312
|
+
* @returns {{ value: string, pieces: Piece[] }}
|
|
313
|
+
*/
|
|
314
|
+
export function evalPathExpr(file, expr, resolveConst) {
|
|
315
|
+
/** @type {Piece[]} */
|
|
316
|
+
const pieces = [];
|
|
317
|
+
const evalRaw = (f, raw, rawStart, depth) => {
|
|
318
|
+
if (depth > 8) throw new Error('constant reference chain is too deep');
|
|
319
|
+
let at = 0;
|
|
320
|
+
const re = /\$\{\s*([\w.]+)\s*\}|\$([A-Za-z_]\w*)/g;
|
|
321
|
+
let m;
|
|
322
|
+
while ((m = re.exec(raw))) {
|
|
323
|
+
if (m.index > at) pieces.push({ value: raw.slice(at, m.index), file: f, start: rawStart + at, end: rawStart + m.index });
|
|
324
|
+
const ref = m[1] || m[2];
|
|
325
|
+
const def = resolveConst(f, ref);
|
|
326
|
+
if (!def) throw new Error(`cannot resolve constant "${ref}"`);
|
|
327
|
+
evalRaw(def.file, def.raw, def.start, depth + 1);
|
|
328
|
+
at = m.index + m[0].length;
|
|
329
|
+
}
|
|
330
|
+
if (at < raw.length) pieces.push({ value: raw.slice(at), file: f, start: rawStart + at, end: rawStart + raw.length });
|
|
331
|
+
};
|
|
332
|
+
const src = file.text;
|
|
333
|
+
const code = file.code;
|
|
334
|
+
// split on top-level +
|
|
335
|
+
const terms = [];
|
|
336
|
+
let depth = 0;
|
|
337
|
+
let s = expr.start;
|
|
338
|
+
for (let i = expr.start; i < expr.end; i++) {
|
|
339
|
+
const c = code[i];
|
|
340
|
+
if (c === '(' || c === '[' || c === '{') depth++;
|
|
341
|
+
else if (c === ')' || c === ']' || c === '}') depth--;
|
|
342
|
+
else if (c === '+' && depth === 0) {
|
|
343
|
+
terms.push([s, i]);
|
|
344
|
+
s = i + 1;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
terms.push([s, expr.end]);
|
|
348
|
+
for (const [a, b] of terms) {
|
|
349
|
+
const t = src.slice(a, b);
|
|
350
|
+
const lead = t.length - t.trimStart().length;
|
|
351
|
+
const body = t.trim();
|
|
352
|
+
if (body.startsWith('"') && !body.startsWith('"""') && body.endsWith('"') && body.length >= 2) {
|
|
353
|
+
const rawStart = a + lead + 1;
|
|
354
|
+
const raw = body.slice(1, -1);
|
|
355
|
+
if (/\\/.test(raw)) throw new Error('escape sequences in a path literal are not supported');
|
|
356
|
+
evalRaw(file, raw, rawStart, 0);
|
|
357
|
+
} else if (/^[A-Za-z_][\w.]*$/.test(body)) {
|
|
358
|
+
const def = resolveConst(file, body);
|
|
359
|
+
if (!def) throw new Error(`cannot resolve constant "${body}"`);
|
|
360
|
+
evalRaw(def.file, def.raw, def.start, 1);
|
|
361
|
+
} else {
|
|
362
|
+
throw new Error(`unsupported path expression "${body.length > 60 ? body.slice(0, 57) + '...' : body}"`);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return { value: pieces.map((p) => p.value).join(''), pieces };
|
|
366
|
+
}
|