espolar 0.1.6 → 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 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: returns `right` |
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 PrintOptions<Data> {
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 defaultGetMappingData(node?: AST.Node | null): unknown;
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, defaultGetMappingData, defaultIsUntouched, defaultPrinters, print };
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
@@ -1383,19 +1383,18 @@ function print(node, options) {
1383
1383
  function defaultIsUntouched(node) {
1384
1384
  return getNodeRange(node) || false;
1385
1385
  }
1386
- function defaultGetMappingData(node) {
1387
- return {};
1388
- }
1389
1386
  function defaultCombineMappingData(left, right) {
1390
- return right;
1387
+ if (left === right) return left;
1388
+ throw new Error("Cannot combine mapping data with different values when no custom combineMappingData function is provided");
1391
1389
  }
1392
1390
  function createPrinterContext(options) {
1393
1391
  const chunks = [];
1394
1392
  const mappings = [];
1393
+ const extraMappings = [];
1395
1394
  let generatedOffset = 0;
1396
1395
  const isUntouched = options.isUntouched ?? defaultIsUntouched;
1397
- const getMappingData = options.getMappingData ?? defaultGetMappingData;
1398
- const combineMappingData = options.combineMappingData ?? (options.getMappingData ? (left) => left : defaultCombineMappingData);
1396
+ const getMappingData = options.getMappingData ?? (() => void 0);
1397
+ const combineMappingData = options.combineMappingData ?? defaultCombineMappingData;
1399
1398
  const printers = {
1400
1399
  ...defaultPrinters,
1401
1400
  ...options.printers
@@ -1492,14 +1491,23 @@ function createPrinterContext(options) {
1492
1491
  context.writeSource(range.start, range.end, getMappingData(node));
1493
1492
  },
1494
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
+ },
1495
1503
  result() {
1496
1504
  return {
1497
1505
  code: chunks.join(""),
1498
- mappings: mappings.map(toVolarMapping)
1506
+ mappings: [...mappings, ...extraMappings].map(toVolarMapping)
1499
1507
  };
1500
1508
  }
1501
1509
  };
1502
1510
  return context;
1503
1511
  }
1504
1512
  //#endregion
1505
- export { defaultCombineMappingData, defaultGetMappingData, defaultIsUntouched, defaultPrinters, print };
1513
+ export { defaultCombineMappingData, defaultIsUntouched, defaultPrinters, print };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "espolar",
3
- "version": "0.1.6",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "exports": {