espolar 0.1.5 → 0.2.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/README.md +2 -2
- package/dist/index.d.ts +13 -8
- package/dist/index.js +23 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -75,8 +75,8 @@ Main entry point. Returns `PrintResult<Data>`.
|
|
|
75
75
|
| --------------------- | ---------------------------------- | ------------------------------------------------------------------------------------------ |
|
|
76
76
|
| `source` | `string` | The original source code (required) |
|
|
77
77
|
| `isUntouched` | `(node) => boolean \| SourceRange` | Determine if a node should be preserved from source. Default: checks `range`/`start`/`end` |
|
|
78
|
-
| `getMappingData` | `(node?) => Data` | Extract data for each mapping entry. Default: `() =>
|
|
79
|
-
| `combineMappingData` | `(left, right) => Data` | Merge data when adjacent mappings are combined. Default:
|
|
78
|
+
| `getMappingData` | `(node?) => Data` | Extract data for each mapping entry. Default: `() => undefined` |
|
|
79
|
+
| `combineMappingData` | `(left, right) => Data` | Merge data when adjacent mappings are combined. Default: throws if `left !== right` |
|
|
80
80
|
| `printers` | `Printers<Data>` | Override printers for specific `AST_NODE_TYPES` |
|
|
81
81
|
| `getLeadingComments` | `(node) => Comment[] \| undefined` | Return comments to print before a touched node |
|
|
82
82
|
| `getTrailingComments` | `(node) => Comment[] \| undefined` | Return comments to print after a touched node |
|
package/dist/index.d.ts
CHANGED
|
@@ -62,10 +62,9 @@ interface PrintResult<Data> {
|
|
|
62
62
|
code: string;
|
|
63
63
|
mappings: Mapping<Data>[];
|
|
64
64
|
}
|
|
65
|
-
interface
|
|
65
|
+
interface PrintOptionsBase<Data> {
|
|
66
66
|
source: string;
|
|
67
67
|
isUntouched?: (node: AST.Node) => boolean | SourceRange;
|
|
68
|
-
getMappingData?: (node?: AST.Node | null) => Data;
|
|
69
68
|
combineMappingData?: (left: Data, right: Data) => Data;
|
|
70
69
|
printers?: Printers<Data>;
|
|
71
70
|
getLeadingComments?: (node: AST.Node) => Comment[] | undefined;
|
|
@@ -81,6 +80,11 @@ interface PrintOptions<Data> {
|
|
|
81
80
|
*/
|
|
82
81
|
experimentalGetLeftParenSourceRange?: (node: AST.CallExpression | AST.NewExpression) => SourceRange | undefined;
|
|
83
82
|
}
|
|
83
|
+
interface MappingDataOptions<Data> {
|
|
84
|
+
getMappingData: (node?: AST.Node | null) => Data;
|
|
85
|
+
}
|
|
86
|
+
type MappingDataUndefinedOptions = { [K in keyof MappingDataOptions<0>]?: undefined };
|
|
87
|
+
type PrintOptions<Data> = PrintOptionsBase<Data> & ([Data] extends [undefined] ? MappingDataUndefinedOptions : MappingDataOptions<Data>);
|
|
84
88
|
interface PrinterContext<Data = any> {
|
|
85
89
|
readonly options: PrintOptions<Data>;
|
|
86
90
|
readonly source: string;
|
|
@@ -93,6 +97,8 @@ interface PrinterContext<Data = any> {
|
|
|
93
97
|
writeSource(start: number, end: number, data?: Data): void;
|
|
94
98
|
writePreservedNode(node: AST.Node): void;
|
|
95
99
|
appendMapping(sourceRange: SourceRange, generatedStart: number, generatedEnd: number, data?: Data): void;
|
|
100
|
+
/** Extra mappings that won't be merged automatically */
|
|
101
|
+
createExtraMapping(sourceRange: SourceRange, generatedStart: number, generatedEnd: number, data?: Data): void;
|
|
96
102
|
}
|
|
97
103
|
type NodePrinter<Key extends AST_NODE_TYPES, Data> = (node: Extract<AST.Node, {
|
|
98
104
|
type: Key;
|
|
@@ -100,12 +106,11 @@ type NodePrinter<Key extends AST_NODE_TYPES, Data> = (node: Extract<AST.Node, {
|
|
|
100
106
|
type Printers<Data> = { [K in AST_NODE_TYPES]?: NodePrinter<K, Data> };
|
|
101
107
|
//#endregion
|
|
102
108
|
//#region src/printer.d.ts
|
|
103
|
-
declare function print<Data>(node: AST.Node, options: PrintOptions<Data>): PrintResult<Data>;
|
|
104
|
-
declare function print<Data>(node: import("estree").Node, options: PrintOptions<Data>): PrintResult<Data>;
|
|
105
|
-
declare function print<Data>(node: NodeLike, options: PrintOptions<Data>): PrintResult<Data>;
|
|
109
|
+
declare function print<Data = undefined>(node: AST.Node, options: PrintOptions<Data>): PrintResult<Data>;
|
|
110
|
+
declare function print<Data = undefined>(node: import("estree").Node, options: PrintOptions<Data>): PrintResult<Data>;
|
|
111
|
+
declare function print<Data = undefined>(node: NodeLike, options: PrintOptions<Data>): PrintResult<Data>;
|
|
106
112
|
declare function defaultIsUntouched(node: AST.Node): boolean | SourceRange;
|
|
107
|
-
declare function
|
|
108
|
-
declare function defaultCombineMappingData(left: unknown, right: unknown): unknown;
|
|
113
|
+
declare function defaultCombineMappingData<T>(left: T, right: T): T;
|
|
109
114
|
//#endregion
|
|
110
115
|
//#region src/printers.d.ts
|
|
111
116
|
declare const defaultPrinters: {
|
|
@@ -385,4 +390,4 @@ declare function printKeywordType(node: Extract<AST.Node, {
|
|
|
385
390
|
type: `${string}Keyword`;
|
|
386
391
|
}>, context: PrinterContext): void;
|
|
387
392
|
//#endregion
|
|
388
|
-
export { type AST, type Comment, type NodeLike, type NodePrinter, type PrintOptions, type PrintResult, type PrinterContext, type SourceRange, defaultCombineMappingData,
|
|
393
|
+
export { type AST, type Comment, type NodeLike, type NodePrinter, type PrintOptions, type PrintResult, type PrinterContext, type SourceRange, defaultCombineMappingData, defaultIsUntouched, defaultPrinters, print };
|
package/dist/index.js
CHANGED
|
@@ -1346,8 +1346,14 @@ function pushMapping(mappings, mapping, combineMappingData) {
|
|
|
1346
1346
|
}
|
|
1347
1347
|
mappings.push(mapping);
|
|
1348
1348
|
}
|
|
1349
|
+
/**
|
|
1350
|
+
* Two adjacent mappings with identical lengths can be merged into one mapping.
|
|
1351
|
+
* @param left
|
|
1352
|
+
* @param right
|
|
1353
|
+
* @returns
|
|
1354
|
+
*/
|
|
1349
1355
|
function canMerge(left, right) {
|
|
1350
|
-
return left.sourceStart
|
|
1356
|
+
return left.sourceEnd - left.sourceStart === left.generatedEnd - left.generatedStart && right.sourceEnd - right.sourceStart === right.generatedEnd - right.generatedStart && left.sourceEnd === right.sourceStart && left.generatedEnd === right.generatedStart;
|
|
1351
1357
|
}
|
|
1352
1358
|
function toVolarMapping(mapping) {
|
|
1353
1359
|
const sourceLength = mapping.sourceEnd - mapping.sourceStart;
|
|
@@ -1377,19 +1383,18 @@ function print(node, options) {
|
|
|
1377
1383
|
function defaultIsUntouched(node) {
|
|
1378
1384
|
return getNodeRange(node) || false;
|
|
1379
1385
|
}
|
|
1380
|
-
function defaultGetMappingData(node) {
|
|
1381
|
-
return {};
|
|
1382
|
-
}
|
|
1383
1386
|
function defaultCombineMappingData(left, right) {
|
|
1384
|
-
return
|
|
1387
|
+
if (left === right) return left;
|
|
1388
|
+
throw new Error("Cannot combine mapping data with different values when no custom combineMappingData function is provided");
|
|
1385
1389
|
}
|
|
1386
1390
|
function createPrinterContext(options) {
|
|
1387
1391
|
const chunks = [];
|
|
1388
1392
|
const mappings = [];
|
|
1393
|
+
const extraMappings = [];
|
|
1389
1394
|
let generatedOffset = 0;
|
|
1390
1395
|
const isUntouched = options.isUntouched ?? defaultIsUntouched;
|
|
1391
|
-
const getMappingData = options.getMappingData ??
|
|
1392
|
-
const combineMappingData = options.combineMappingData ??
|
|
1396
|
+
const getMappingData = options.getMappingData ?? (() => void 0);
|
|
1397
|
+
const combineMappingData = options.combineMappingData ?? defaultCombineMappingData;
|
|
1393
1398
|
const printers = {
|
|
1394
1399
|
...defaultPrinters,
|
|
1395
1400
|
...options.printers
|
|
@@ -1486,14 +1491,23 @@ function createPrinterContext(options) {
|
|
|
1486
1491
|
context.writeSource(range.start, range.end, getMappingData(node));
|
|
1487
1492
|
},
|
|
1488
1493
|
appendMapping,
|
|
1494
|
+
createExtraMapping(sourceRange, generatedStart, generatedEnd, data) {
|
|
1495
|
+
extraMappings.push({
|
|
1496
|
+
sourceStart: sourceRange.start,
|
|
1497
|
+
sourceEnd: sourceRange.end,
|
|
1498
|
+
generatedStart,
|
|
1499
|
+
generatedEnd,
|
|
1500
|
+
data
|
|
1501
|
+
});
|
|
1502
|
+
},
|
|
1489
1503
|
result() {
|
|
1490
1504
|
return {
|
|
1491
1505
|
code: chunks.join(""),
|
|
1492
|
-
mappings: mappings.map(toVolarMapping)
|
|
1506
|
+
mappings: [...mappings, ...extraMappings].map(toVolarMapping)
|
|
1493
1507
|
};
|
|
1494
1508
|
}
|
|
1495
1509
|
};
|
|
1496
1510
|
return context;
|
|
1497
1511
|
}
|
|
1498
1512
|
//#endregion
|
|
1499
|
-
export { defaultCombineMappingData,
|
|
1513
|
+
export { defaultCombineMappingData, defaultIsUntouched, defaultPrinters, print };
|