Skip to content

Commit c64eda1

Browse files
authored
Merge pull request #36 from sveltejs/array-sequences
Use arrays instead of `Sequence` nodes
2 parents aada856 + ee77f30 commit c64eda1

File tree

5 files changed

+70
-79
lines changed

5 files changed

+70
-79
lines changed

src/handlers.js

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** @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' */
33

44
/** @type {Newline} */
55
const newline = { type: 'Newline' };
@@ -11,11 +11,10 @@ const indent = { type: 'Indent' };
1111
const dedent = { type: 'Dedent' };
1212

1313
/**
14-
* @param {Command[]} children
15-
* @returns {Sequence}
14+
* @returns {Command[]}
1615
*/
17-
function create_sequence(...children) {
18-
return { type: 'Sequence', children };
16+
function create_sequence() {
17+
return [];
1918
}
2019

2120
/**
@@ -30,11 +29,11 @@ function measure(commands, from, to = commands.length) {
3029
const command = commands[i];
3130
if (typeof command === 'string') {
3231
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);
3837
}
3938
}
4039

@@ -66,17 +65,32 @@ export function handle(node, state) {
6665
}
6766
}
6867

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+
6981
/**
7082
* @param {string} content
7183
* @param {TSESTree.Node} node
72-
* @returns {Chunk}
84+
* @returns {string | Command[]}
7385
*/
7486
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;
8094
}
8195

8296
/**
@@ -288,7 +302,7 @@ const handle_body = (nodes, state) => {
288302
grouped_expression_types.includes(last_statement.type)) &&
289303
last_statement.type !== statement.type)
290304
) {
291-
margin.children.push('\n');
305+
margin.push('\n');
292306
}
293307

294308
let add_newline = false;
@@ -332,11 +346,11 @@ const handle_var_declaration = (node, state) => {
332346

333347
if (multiline) {
334348
state.multiline = true;
335-
if (node.declarations.length > 1) open.children.push(indent);
336-
join.children.push(',', newline);
349+
if (node.declarations.length > 1) open.push(indent);
350+
join.push(',', newline);
337351
if (node.declarations.length > 1) state.commands.push(dedent);
338352
} else {
339-
join.children.push(', ');
353+
join.push(', ');
340354
}
341355
};
342356

@@ -408,13 +422,13 @@ function sequence(nodes, state, spaces, fn, separator = ',') {
408422
if (multiline) {
409423
state.multiline = true;
410424

411-
open.children.push(indent, newline);
412-
join.children.push(newline);
413-
close.children.push(dedent, newline);
425+
open.push(indent, newline);
426+
join.push(newline);
427+
close.push(dedent, newline);
414428
} else {
415-
if (spaces) open.children.push(' ');
416-
join.children.push(' ');
417-
if (spaces) close.children.push(' ');
429+
if (spaces) open.push(' ');
430+
join.push(' ');
431+
if (spaces) close.push(' ');
418432
}
419433
}
420434

@@ -710,11 +724,11 @@ const shared = {
710724
}
711725

712726
if (multiline) {
713-
open.children.push(indent, newline);
714-
join.children.push(',', newline);
715-
close.children.push(dedent, newline);
727+
open.push(indent, newline);
728+
join.push(',', newline);
729+
close.push(dedent, newline);
716730
} else {
717-
join.children.push(', ');
731+
join.push(', ');
718732
}
719733
},
720734

@@ -906,12 +920,12 @@ const handlers = {
906920
const multiline = child_state.multiline;
907921

908922
if (multiline) {
909-
if_true.children.push(indent, newline, '? ');
910-
if_false.children.push(newline, ': ');
923+
if_true.push(indent, newline, '? ');
924+
if_false.push(newline, ': ');
911925
state.commands.push(dedent);
912926
} else {
913-
if_true.children.push(' ? ');
914-
if_false.children.push(' : ');
927+
if_true.push(' ? ');
928+
if_false.push(' : ');
915929
}
916930
},
917931

src/index.js

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,30 +79,21 @@ export function print(node, opts = {}) {
7979
return;
8080
}
8181

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+
}
10588

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+
]);
10697
break;
10798

10899
case 'Newline':
@@ -117,13 +108,6 @@ export function print(node, opts = {}) {
117108
newline = newline.slice(0, -indent.length);
118109
break;
119110

120-
case 'Sequence':
121-
for (let i = 0; i < command.children.length; i += 1) {
122-
run(command.children[i]);
123-
}
124-
125-
break;
126-
127111
case 'Comment':
128112
if (command.comment.type === 'Line') {
129113
append(`//${command.comment.value}`);

src/types.d.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,10 @@ export interface State {
3838
quote: "'" | '"';
3939
}
4040

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;
4845
}
4946

5047
export interface Newline {
@@ -64,17 +61,12 @@ export interface IndentChange {
6461
offset: number;
6562
}
6663

67-
export interface Sequence {
68-
type: 'Sequence';
69-
children: Command[];
70-
}
71-
7264
export interface CommentChunk {
7365
type: 'Comment';
7466
comment: TSESTree.Comment;
7567
}
7668

77-
export type Command = string | Chunk | Newline | Indent | Dedent | Sequence | CommentChunk;
69+
export type Command = string | Location | Newline | Indent | Dedent | CommentChunk | Command[];
7870

7971
export interface PrintOptions {
8072
sourceMapSource?: string;

test/samples/large-file/expected.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@
191191
}));
192192
},
193193
eq(i) {
194-
var len = this.length, j = +i + (i < 0 ? len : 0);
194+
var len = this.length,
195+
j = +i + (i < 0 ? len : 0);
195196

196197
return this.pushStack(j >= 0 && j < len ? [this[j]] : []);
197198
},

test/samples/large-file/expected.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)