esrap 1.4.0 → 1.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esrap",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "Parse in reverse",
5
5
  "repository": {
6
6
  "type": "git",
package/src/handlers.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /** @import { TSESTree } from '@typescript-eslint/types' */
2
- /** @import { Chunk, Command, Dedent, Handlers, Indent, Newline, NodeWithComments, Sequence, State, TypeAnnotationNodes } from './types' */
2
+ /** @import { Command, Dedent, Handlers, Location, Indent, Newline, NodeWithComments, State, TypeAnnotationNodes } from './types' */
3
3
 
4
4
  /** @type {Newline} */
5
5
  const newline = { type: 'Newline' };
@@ -11,11 +11,10 @@ const indent = { type: 'Indent' };
11
11
  const dedent = { type: 'Dedent' };
12
12
 
13
13
  /**
14
- * @param {Command[]} children
15
- * @returns {Sequence}
14
+ * @returns {Command[]}
16
15
  */
17
- function create_sequence(...children) {
18
- return { type: 'Sequence', children };
16
+ function create_sequence() {
17
+ return [];
19
18
  }
20
19
 
21
20
  /**
@@ -30,11 +29,11 @@ function measure(commands, from, to = commands.length) {
30
29
  const command = commands[i];
31
30
  if (typeof command === 'string') {
32
31
  total += command.length;
33
- } else if (command.type === 'Chunk') {
34
- total += command.content.length;
35
- } else if (command.type === 'Sequence') {
36
- // assume this is ', '
37
- total += 2;
32
+ } else if (Array.isArray(command)) {
33
+ total +=
34
+ command.length === 0
35
+ ? 2 // assume this is ', '
36
+ : measure(command, 0);
38
37
  }
39
38
  }
40
39
 
@@ -66,17 +65,32 @@ export function handle(node, state) {
66
65
  }
67
66
  }
68
67
 
68
+ /**
69
+ * @param {number} line
70
+ * @param {number} column
71
+ * @returns {Location}
72
+ */
73
+ function l(line, column) {
74
+ return {
75
+ type: 'Location',
76
+ line,
77
+ column
78
+ };
79
+ }
80
+
69
81
  /**
70
82
  * @param {string} content
71
83
  * @param {TSESTree.Node} node
72
- * @returns {Chunk}
84
+ * @returns {string | Command[]}
73
85
  */
74
86
  function c(content, node) {
75
- return {
76
- type: 'Chunk',
77
- content,
78
- loc: node?.loc ?? null
79
- };
87
+ return node.loc
88
+ ? [
89
+ l(node.loc.start.line, node.loc.start.column),
90
+ content,
91
+ l(node.loc.end.line, node.loc.end.column)
92
+ ]
93
+ : content;
80
94
  }
81
95
 
82
96
  /**
@@ -101,7 +115,26 @@ function prepend_comments(comments, state, newlines) {
101
115
  * @param {'\'' | '"'} char
102
116
  */
103
117
  function quote(string, char) {
104
- return char + string.replaceAll(char, '\\' + char) + char;
118
+ let out = char;
119
+ let escaped = false;
120
+
121
+ for (const c of string) {
122
+ if (escaped) {
123
+ out += c;
124
+ escaped = false;
125
+ } else if (c === '\\') {
126
+ out += '\\\\';
127
+ escaped = true;
128
+ } else if (c === char) {
129
+ out += '\\' + c;
130
+ } else if (c === '\n') {
131
+ out += '\\n';
132
+ } else {
133
+ out += c;
134
+ }
135
+ }
136
+
137
+ return out + char;
105
138
  }
106
139
 
107
140
  const OPERATOR_PRECEDENCE = {
@@ -288,7 +321,7 @@ const handle_body = (nodes, state) => {
288
321
  grouped_expression_types.includes(last_statement.type)) &&
289
322
  last_statement.type !== statement.type)
290
323
  ) {
291
- margin.children.push('\n');
324
+ margin.push('\n');
292
325
  }
293
326
 
294
327
  let add_newline = false;
@@ -332,11 +365,11 @@ const handle_var_declaration = (node, state) => {
332
365
 
333
366
  if (multiline) {
334
367
  state.multiline = true;
335
- if (node.declarations.length > 1) open.children.push(indent);
336
- join.children.push(',', newline);
368
+ if (node.declarations.length > 1) open.push(indent);
369
+ join.push(',', newline);
337
370
  if (node.declarations.length > 1) state.commands.push(dedent);
338
371
  } else {
339
- join.children.push(', ');
372
+ join.push(', ');
340
373
  }
341
374
  };
342
375
 
@@ -408,13 +441,13 @@ function sequence(nodes, state, spaces, fn, separator = ',') {
408
441
  if (multiline) {
409
442
  state.multiline = true;
410
443
 
411
- open.children.push(indent, newline);
412
- join.children.push(newline);
413
- close.children.push(dedent, newline);
444
+ open.push(indent, newline);
445
+ join.push(newline);
446
+ close.push(dedent, newline);
414
447
  } else {
415
- if (spaces) open.children.push(' ');
416
- join.children.push(' ');
417
- if (spaces) close.children.push(' ');
448
+ if (spaces) open.push(' ');
449
+ join.push(' ');
450
+ if (spaces) close.push(' ');
418
451
  }
419
452
  }
420
453
 
@@ -710,11 +743,11 @@ const shared = {
710
743
  }
711
744
 
712
745
  if (multiline) {
713
- open.children.push(indent, newline);
714
- join.children.push(',', newline);
715
- close.children.push(dedent, newline);
746
+ open.push(indent, newline);
747
+ join.push(',', newline);
748
+ close.push(dedent, newline);
716
749
  } else {
717
- join.children.push(', ');
750
+ join.push(', ');
718
751
  }
719
752
  },
720
753
 
@@ -906,12 +939,12 @@ const handlers = {
906
939
  const multiline = child_state.multiline;
907
940
 
908
941
  if (multiline) {
909
- if_true.children.push(indent, newline, '? ');
910
- if_false.children.push(newline, ': ');
942
+ if_true.push(indent, newline, '? ');
943
+ if_false.push(newline, ': ');
911
944
  state.commands.push(dedent);
912
945
  } else {
913
- if_true.children.push(' ? ');
914
- if_false.children.push(' : ');
946
+ if_true.push(' ? ');
947
+ if_false.push(' : ');
915
948
  }
916
949
  },
917
950
 
package/src/index.js CHANGED
@@ -79,30 +79,21 @@ export function print(node, opts = {}) {
79
79
  return;
80
80
  }
81
81
 
82
- switch (command.type) {
83
- case 'Chunk':
84
- const loc = command.loc;
85
-
86
- if (loc) {
87
- current_line.push([
88
- current_column,
89
- 0, // source index is always zero
90
- loc.start.line - 1,
91
- loc.start.column
92
- ]);
93
- }
94
-
95
- append(command.content);
96
-
97
- if (loc) {
98
- current_line.push([
99
- current_column,
100
- 0, // source index is always zero
101
- loc.end.line - 1,
102
- loc.end.column
103
- ]);
104
- }
82
+ if (Array.isArray(command)) {
83
+ for (let i = 0; i < command.length; i += 1) {
84
+ run(command[i]);
85
+ }
86
+ return;
87
+ }
105
88
 
89
+ switch (command.type) {
90
+ case 'Location':
91
+ current_line.push([
92
+ current_column,
93
+ 0, // source index is always zero
94
+ command.line - 1,
95
+ command.column
96
+ ]);
106
97
  break;
107
98
 
108
99
  case 'Newline':
@@ -117,13 +108,6 @@ export function print(node, opts = {}) {
117
108
  newline = newline.slice(0, -indent.length);
118
109
  break;
119
110
 
120
- case 'Sequence':
121
- for (let i = 0; i < command.children.length; i += 1) {
122
- run(command.children[i]);
123
- }
124
-
125
- break;
126
-
127
111
  case 'Comment':
128
112
  if (command.comment.type === 'Line') {
129
113
  append(`//${command.comment.value}`);
package/src/types.d.ts CHANGED
@@ -38,13 +38,10 @@ export interface State {
38
38
  quote: "'" | '"';
39
39
  }
40
40
 
41
- export interface Chunk {
42
- type: 'Chunk';
43
- content: string;
44
- loc: null | {
45
- start: { line: number; column: number };
46
- end: { line: number; column: number };
47
- };
41
+ export interface Location {
42
+ type: 'Location';
43
+ line: number;
44
+ column: number;
48
45
  }
49
46
 
50
47
  export interface Newline {
@@ -64,17 +61,12 @@ export interface IndentChange {
64
61
  offset: number;
65
62
  }
66
63
 
67
- export interface Sequence {
68
- type: 'Sequence';
69
- children: Command[];
70
- }
71
-
72
64
  export interface CommentChunk {
73
65
  type: 'Comment';
74
66
  comment: TSESTree.Comment;
75
67
  }
76
68
 
77
- export type Command = string | Chunk | Newline | Indent | Dedent | Sequence | CommentChunk;
69
+ export type Command = string | Location | Newline | Indent | Dedent | CommentChunk | Command[];
78
70
 
79
71
  export interface PrintOptions {
80
72
  sourceMapSource?: string;
@@ -13,6 +13,6 @@
13
13
  null,
14
14
  null
15
15
  ],
16
- "mappings": ";kBA8EiBA,YAAYA;;;;;;;;;;iBCvDbC,KAAKA",
16
+ "mappings": ";kBAsEiBA,YAAYA;;;;;;;;;;iBC/CbC,KAAKA",
17
17
  "ignoreList": []
18
18
  }