@tsrx/core 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +249 -0
- package/package.json +3 -2
- package/src/index.js +60 -58
- package/src/parse/index.js +1 -1
- package/src/parse/parse-module.js +18 -0
- package/src/plugin.js +2248 -0
- package/src/scope.js +1 -1
- package/src/source-map-utils.js +19 -19
- package/src/transform/segments.js +101 -11
- package/types/index.d.ts +20 -20
- package/types/parse.d.ts +3 -3
package/src/scope.js
CHANGED
package/src/source-map-utils.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
@import { PostProcessingChanges, LineOffsets } from '../types/index';
|
|
3
|
-
@import * as AST from 'estree';
|
|
4
|
-
@import { CodeMapping } from '../types/index';
|
|
5
|
-
@import { CodeMapping as VolarCodeMapping } from '@volar/language-core';
|
|
6
|
-
@import { RawSourceMap } from 'source-map';
|
|
2
|
+
* @import { PostProcessingChanges, LineOffsets } from '../types/index.js';
|
|
3
|
+
* @import * as AST from 'estree';
|
|
4
|
+
* @import { CodeMapping } from '../types/index.js';
|
|
5
|
+
* @import { CodeMapping as VolarCodeMapping } from '@volar/language-core';
|
|
6
|
+
* @import { RawSourceMap } from 'source-map';
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
@typedef {{
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}} CodePosition
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
@typedef {Map<string,
|
|
23
|
-
*/
|
|
10
|
+
* @typedef {{
|
|
11
|
+
* line: number,
|
|
12
|
+
* column: number,
|
|
13
|
+
* end_line: number,
|
|
14
|
+
* end_column: number,
|
|
15
|
+
* code: string,
|
|
16
|
+
* metadata: {
|
|
17
|
+
* css?: AST.Element['metadata']['css']
|
|
18
|
+
* },
|
|
19
|
+
* }} CodePosition
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** @typedef {Map<string, CodePosition[]>} CodeToGeneratedMap */
|
|
23
|
+
/** @typedef {Map<string, {line: number, column: number}[]>} GeneratedToSourceMap */
|
|
24
24
|
|
|
25
25
|
import { decode } from '@jridgewell/sourcemap-codec';
|
|
26
26
|
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
CodeMapping,
|
|
10
10
|
VolarMappingsResult,
|
|
11
11
|
PostProcessingChanges,
|
|
12
|
+
LineOffsets,
|
|
12
13
|
} from '../../types/index';
|
|
13
14
|
@import { CodeMapping as VolarCodeMapping } from '@volar/language-core';
|
|
14
15
|
*/
|
|
@@ -982,9 +983,11 @@ export function convert_source_map_to_mappings(
|
|
|
982
983
|
visit(node.body);
|
|
983
984
|
}
|
|
984
985
|
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
986
|
+
if (node.loc) {
|
|
987
|
+
mappings.push(
|
|
988
|
+
get_mapping_from_node(node, src_to_gen_map, gen_line_offsets, mapping_data_verify_only),
|
|
989
|
+
);
|
|
990
|
+
}
|
|
988
991
|
|
|
989
992
|
return;
|
|
990
993
|
} else if (node.type === 'WhileStatement' || node.type === 'DoWhileStatement') {
|
|
@@ -1321,9 +1324,11 @@ export function convert_source_map_to_mappings(
|
|
|
1321
1324
|
}
|
|
1322
1325
|
}
|
|
1323
1326
|
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
+
if (node.loc) {
|
|
1328
|
+
mappings.push(
|
|
1329
|
+
get_mapping_from_node(node, src_to_gen_map, gen_line_offsets, mapping_data_verify_only),
|
|
1330
|
+
);
|
|
1331
|
+
}
|
|
1327
1332
|
|
|
1328
1333
|
return;
|
|
1329
1334
|
} else if (node.type === 'SwitchCase') {
|
|
@@ -2030,11 +2035,16 @@ export function convert_source_map_to_mappings(
|
|
|
2030
2035
|
);
|
|
2031
2036
|
const source_length = source_text.length;
|
|
2032
2037
|
const gen_length = gen_text.length;
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
+
let gen_line_col;
|
|
2039
|
+
try {
|
|
2040
|
+
gen_line_col = get_generated_position(
|
|
2041
|
+
token.loc.start.line,
|
|
2042
|
+
token.loc.start.column,
|
|
2043
|
+
src_to_gen_map,
|
|
2044
|
+
);
|
|
2045
|
+
} catch {
|
|
2046
|
+
continue;
|
|
2047
|
+
}
|
|
2038
2048
|
const gen_start = loc_to_offset(gen_line_col.line, gen_line_col.column, gen_line_offsets);
|
|
2039
2049
|
|
|
2040
2050
|
/** @type {CustomMappingData} */
|
|
@@ -2138,3 +2148,83 @@ export function convert_source_map_to_mappings(
|
|
|
2138
2148
|
cssMappings,
|
|
2139
2149
|
};
|
|
2140
2150
|
}
|
|
2151
|
+
|
|
2152
|
+
/**
|
|
2153
|
+
* Build a `VolarMappingsResult` from generated code plus source-map metadata.
|
|
2154
|
+
*
|
|
2155
|
+
* Framework packages are responsible for producing the generated AST/code/map.
|
|
2156
|
+
* Core owns the generic mapping conversion and result envelope so the editor
|
|
2157
|
+
* integration is not coupled to any specific framework package.
|
|
2158
|
+
*
|
|
2159
|
+
* @param {{
|
|
2160
|
+
* ast: AST.Program,
|
|
2161
|
+
* ast_from_source: AST.Program,
|
|
2162
|
+
* source: string,
|
|
2163
|
+
* generated_code: string,
|
|
2164
|
+
* source_map: RawSourceMap,
|
|
2165
|
+
* errors?: import('../../types/index').CompileError[],
|
|
2166
|
+
* post_processing_changes?: PostProcessingChanges,
|
|
2167
|
+
* line_offsets?: LineOffsets,
|
|
2168
|
+
* }} params
|
|
2169
|
+
* @returns {VolarMappingsResult}
|
|
2170
|
+
*/
|
|
2171
|
+
export function create_volar_mappings_result({
|
|
2172
|
+
ast,
|
|
2173
|
+
ast_from_source,
|
|
2174
|
+
source,
|
|
2175
|
+
generated_code,
|
|
2176
|
+
source_map,
|
|
2177
|
+
errors = [],
|
|
2178
|
+
post_processing_changes,
|
|
2179
|
+
line_offsets,
|
|
2180
|
+
}) {
|
|
2181
|
+
return {
|
|
2182
|
+
...convert_source_map_to_mappings(
|
|
2183
|
+
ast,
|
|
2184
|
+
ast_from_source,
|
|
2185
|
+
source,
|
|
2186
|
+
generated_code,
|
|
2187
|
+
source_map,
|
|
2188
|
+
/** @type {PostProcessingChanges} */ (post_processing_changes),
|
|
2189
|
+
line_offsets ?? build_line_offsets(generated_code),
|
|
2190
|
+
),
|
|
2191
|
+
errors,
|
|
2192
|
+
};
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
/**
|
|
2196
|
+
* Build a coarse `VolarMappingsResult` when a framework can emit valid TS/TSX
|
|
2197
|
+
* for checking but does not yet produce a location-rich generated AST for the
|
|
2198
|
+
* fine-grained source-map conversion path.
|
|
2199
|
+
*
|
|
2200
|
+
* @param {{
|
|
2201
|
+
* source: string,
|
|
2202
|
+
* generated_code: string,
|
|
2203
|
+
* errors?: import('../../types/index').CompileError[],
|
|
2204
|
+
* }} params
|
|
2205
|
+
* @returns {VolarMappingsResult}
|
|
2206
|
+
*/
|
|
2207
|
+
export function create_basic_volar_mappings_result({ source, generated_code, errors = [] }) {
|
|
2208
|
+
const shared_length = Math.min(source.length, generated_code.length);
|
|
2209
|
+
|
|
2210
|
+
return {
|
|
2211
|
+
code: generated_code,
|
|
2212
|
+
mappings:
|
|
2213
|
+
shared_length > 0
|
|
2214
|
+
? [
|
|
2215
|
+
{
|
|
2216
|
+
sourceOffsets: [0],
|
|
2217
|
+
generatedOffsets: [0],
|
|
2218
|
+
lengths: [shared_length],
|
|
2219
|
+
generatedLengths: [shared_length],
|
|
2220
|
+
data: {
|
|
2221
|
+
...mapping_data,
|
|
2222
|
+
customData: {},
|
|
2223
|
+
},
|
|
2224
|
+
},
|
|
2225
|
+
]
|
|
2226
|
+
: [],
|
|
2227
|
+
cssMappings: [],
|
|
2228
|
+
errors,
|
|
2229
|
+
};
|
|
2230
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type * as AST from 'estree';
|
|
2
2
|
import type * as ESTreeJSX from 'estree-jsx';
|
|
3
3
|
import type { TSESTree } from '@typescript-eslint/types';
|
|
4
|
-
import type { Parse } from './parse';
|
|
4
|
+
import type { Parse } from './parse.js';
|
|
5
5
|
import type * as ESRap from 'esrap';
|
|
6
6
|
import type { Position } from 'acorn';
|
|
7
|
-
import type { RequireAllOrNone } from '../src/helpers';
|
|
7
|
+
import type { RequireAllOrNone } from '../src/helpers.js';
|
|
8
8
|
|
|
9
9
|
export type { Parse };
|
|
10
10
|
|
|
@@ -163,7 +163,7 @@ declare module 'estree' {
|
|
|
163
163
|
Component: Component;
|
|
164
164
|
Tsx: Tsx;
|
|
165
165
|
TsxCompat: TsxCompat;
|
|
166
|
-
|
|
166
|
+
TSRXExpression: TSRXExpression;
|
|
167
167
|
Html: Html;
|
|
168
168
|
Element: Element;
|
|
169
169
|
Text: TextNode;
|
|
@@ -329,8 +329,8 @@ declare module 'estree' {
|
|
|
329
329
|
expression: AST.Expression;
|
|
330
330
|
}
|
|
331
331
|
|
|
332
|
-
export interface
|
|
333
|
-
type: '
|
|
332
|
+
export interface TSRXExpression extends AST.BaseExpression {
|
|
333
|
+
type: 'TSRXExpression';
|
|
334
334
|
expression: AST.Expression;
|
|
335
335
|
loc?: AST.SourceLocation;
|
|
336
336
|
}
|
|
@@ -339,7 +339,7 @@ declare module 'estree' {
|
|
|
339
339
|
type: 'Element';
|
|
340
340
|
// MemberExpression for namespaced or dynamic elements
|
|
341
341
|
id: AST.Identifier | AST.MemberExpression;
|
|
342
|
-
attributes:
|
|
342
|
+
attributes: TSRXAttribute[];
|
|
343
343
|
children: AST.Node[];
|
|
344
344
|
selfClosing?: boolean;
|
|
345
345
|
unclosed?: boolean;
|
|
@@ -421,33 +421,33 @@ declare module 'estree' {
|
|
|
421
421
|
* Ripple's extended Declaration type that includes Component
|
|
422
422
|
* Use this instead of Declaration when you need Component support
|
|
423
423
|
*/
|
|
424
|
-
export type
|
|
424
|
+
export type TSRXDeclaration = AST.Declaration | Component | AST.TSDeclareFunction;
|
|
425
425
|
|
|
426
426
|
/**
|
|
427
427
|
* Ripple's extended ExportNamedDeclaration with Component support
|
|
428
428
|
*/
|
|
429
|
-
interface
|
|
430
|
-
declaration?:
|
|
429
|
+
interface TSRXExportNamedDeclaration extends Omit<AST.ExportNamedDeclaration, 'declaration'> {
|
|
430
|
+
declaration?: TSRXDeclaration | null | undefined;
|
|
431
431
|
}
|
|
432
432
|
|
|
433
433
|
/**
|
|
434
434
|
* Ripple's extended Program with Component support
|
|
435
435
|
*/
|
|
436
|
-
interface
|
|
436
|
+
interface TSRXProgram extends Omit<Program, 'body'> {
|
|
437
437
|
body: (Program['body'][number] | Component | FunctionExpression)[];
|
|
438
438
|
}
|
|
439
439
|
|
|
440
|
-
interface
|
|
440
|
+
interface TSRXMethodDefinition extends Omit<AST.MethodDefinition, 'value'> {
|
|
441
441
|
value: AST.MethodDefinition['value'] | Component;
|
|
442
442
|
}
|
|
443
443
|
|
|
444
|
-
interface
|
|
444
|
+
interface TSRXProperty extends Omit<AST.Property, 'value'> {
|
|
445
445
|
value: AST.Property['value'] | Component;
|
|
446
446
|
}
|
|
447
447
|
|
|
448
|
-
export type
|
|
448
|
+
export type TSRXAttribute = AST.Attribute | AST.SpreadAttribute | AST.RefAttribute;
|
|
449
449
|
|
|
450
|
-
export type
|
|
450
|
+
export type TSRXStatement = AST.Statement | TSESTree.Statement;
|
|
451
451
|
|
|
452
452
|
export type NodeWithChildren = AST.Element | AST.Tsx | AST.TsxCompat;
|
|
453
453
|
|
|
@@ -658,11 +658,11 @@ declare module 'estree-jsx' {
|
|
|
658
658
|
computed?: boolean;
|
|
659
659
|
}
|
|
660
660
|
|
|
661
|
-
interface
|
|
661
|
+
interface TSRXJSXOpeningElement extends Omit<JSXOpeningElement, 'name'> {
|
|
662
662
|
name: AST.MemberExpression | JSXIdentifier | JSXNamespacedName;
|
|
663
663
|
}
|
|
664
664
|
|
|
665
|
-
interface
|
|
665
|
+
interface TSRXJSXClosingElement extends Omit<JSXClosingElement, 'name'> {
|
|
666
666
|
name: AST.MemberExpression | JSXIdentifier | JSXNamespacedName;
|
|
667
667
|
}
|
|
668
668
|
|
|
@@ -1144,7 +1144,7 @@ export interface AnalysisResult {
|
|
|
1144
1144
|
/**
|
|
1145
1145
|
* Configuration for Ripple parser plugin
|
|
1146
1146
|
*/
|
|
1147
|
-
export interface
|
|
1147
|
+
export interface TSRXPluginConfig {
|
|
1148
1148
|
allowSatisfies?: boolean;
|
|
1149
1149
|
}
|
|
1150
1150
|
|
|
@@ -1224,7 +1224,7 @@ export interface Binding {
|
|
|
1224
1224
|
/**
|
|
1225
1225
|
* Root scope manager
|
|
1226
1226
|
*/
|
|
1227
|
-
export interface
|
|
1227
|
+
export interface ScopeRootInterface {
|
|
1228
1228
|
/** Set of conflicting/reserved names */
|
|
1229
1229
|
conflicts: Set<string>;
|
|
1230
1230
|
/** Generate unique identifier name */
|
|
@@ -1232,7 +1232,7 @@ export interface ScopeRoot {
|
|
|
1232
1232
|
}
|
|
1233
1233
|
|
|
1234
1234
|
export interface ScopeConstructorInterface {
|
|
1235
|
-
root:
|
|
1235
|
+
root: ScopeRootInterface;
|
|
1236
1236
|
parent: ScopeInterface | null;
|
|
1237
1237
|
porous: boolean;
|
|
1238
1238
|
error_options: {
|
|
@@ -1255,7 +1255,7 @@ export type ScopeConstructorParameters = [
|
|
|
1255
1255
|
*/
|
|
1256
1256
|
export interface ScopeInterface {
|
|
1257
1257
|
/** Root scope manager */
|
|
1258
|
-
root:
|
|
1258
|
+
root: ScopeRootInterface;
|
|
1259
1259
|
/** Parent scope */
|
|
1260
1260
|
parent: ScopeInterface | null;
|
|
1261
1261
|
/** Map of declared bindings */
|
package/types/parse.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ import type * as acorn from 'acorn';
|
|
|
14
14
|
import type * as AST from 'estree';
|
|
15
15
|
import type * as ESTreeJSX from 'estree-jsx';
|
|
16
16
|
import type * as ESRap from 'esrap';
|
|
17
|
-
import type * as CoreCompiler from './index';
|
|
17
|
+
import type * as CoreCompiler from './index.js';
|
|
18
18
|
import type { RawSourceMap } from 'source-map';
|
|
19
19
|
|
|
20
20
|
type ForInit = boolean | 'await';
|
|
@@ -1196,7 +1196,7 @@ export namespace Parse {
|
|
|
1196
1196
|
topLevel?: boolean,
|
|
1197
1197
|
exports?: AST.ExportSpecifier,
|
|
1198
1198
|
):
|
|
1199
|
-
| AST.
|
|
1199
|
+
| AST.TSRXExpression
|
|
1200
1200
|
| AST.Html
|
|
1201
1201
|
| AST.TextNode
|
|
1202
1202
|
| ESTreeJSX.JSXEmptyExpression
|
|
@@ -1634,7 +1634,7 @@ export namespace Parse {
|
|
|
1634
1634
|
* Parse JSX attribute (name="value" or {spread})
|
|
1635
1635
|
* @returns JSXAttribute or JSXSpreadAttribute
|
|
1636
1636
|
*/
|
|
1637
|
-
jsx_parseAttribute(): AST.
|
|
1637
|
+
jsx_parseAttribute(): AST.TSRXAttribute | ESTreeJSX.JSXAttribute;
|
|
1638
1638
|
|
|
1639
1639
|
/**
|
|
1640
1640
|
* Parse JSX opening element at position
|