Skip to content

Commit

Permalink
Merge pull request #36 from sveltejs/array-sequences
Browse files Browse the repository at this point in the history
Use arrays instead of `Sequence` nodes
  • Loading branch information
Rich-Harris authored Jan 9, 2025
2 parents aada856 + ee77f30 commit c64eda1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 79 deletions.
82 changes: 48 additions & 34 deletions src/handlers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @import { TSESTree } from '@typescript-eslint/types' */
/** @import { Chunk, Command, Dedent, Handlers, Indent, Newline, NodeWithComments, Sequence, State, TypeAnnotationNodes } from './types' */
/** @import { Command, Dedent, Handlers, Location, Indent, Newline, NodeWithComments, State, TypeAnnotationNodes } from './types' */

/** @type {Newline} */
const newline = { type: 'Newline' };
Expand All @@ -11,11 +11,10 @@ const indent = { type: 'Indent' };
const dedent = { type: 'Dedent' };

/**
* @param {Command[]} children
* @returns {Sequence}
* @returns {Command[]}
*/
function create_sequence(...children) {
return { type: 'Sequence', children };
function create_sequence() {
return [];
}

/**
Expand All @@ -30,11 +29,11 @@ function measure(commands, from, to = commands.length) {
const command = commands[i];
if (typeof command === 'string') {
total += command.length;
} else if (command.type === 'Chunk') {
total += command.content.length;
} else if (command.type === 'Sequence') {
// assume this is ', '
total += 2;
} else if (Array.isArray(command)) {
total +=
command.length === 0
? 2 // assume this is ', '
: measure(command, 0);
}
}

Expand Down Expand Up @@ -66,17 +65,32 @@ export function handle(node, state) {
}
}

/**
* @param {number} line
* @param {number} column
* @returns {Location}
*/
function l(line, column) {
return {
type: 'Location',
line,
column
};
}

/**
* @param {string} content
* @param {TSESTree.Node} node
* @returns {Chunk}
* @returns {string | Command[]}
*/
function c(content, node) {
return {
type: 'Chunk',
content,
loc: node?.loc ?? null
};
return node.loc
? [
l(node.loc.start.line, node.loc.start.column),
content,
l(node.loc.end.line, node.loc.end.column)
]
: content;
}

/**
Expand Down Expand Up @@ -288,7 +302,7 @@ const handle_body = (nodes, state) => {
grouped_expression_types.includes(last_statement.type)) &&
last_statement.type !== statement.type)
) {
margin.children.push('\n');
margin.push('\n');
}

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

if (multiline) {
state.multiline = true;
if (node.declarations.length > 1) open.children.push(indent);
join.children.push(',', newline);
if (node.declarations.length > 1) open.push(indent);
join.push(',', newline);
if (node.declarations.length > 1) state.commands.push(dedent);
} else {
join.children.push(', ');
join.push(', ');
}
};

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

open.children.push(indent, newline);
join.children.push(newline);
close.children.push(dedent, newline);
open.push(indent, newline);
join.push(newline);
close.push(dedent, newline);
} else {
if (spaces) open.children.push(' ');
join.children.push(' ');
if (spaces) close.children.push(' ');
if (spaces) open.push(' ');
join.push(' ');
if (spaces) close.push(' ');
}
}

Expand Down Expand Up @@ -710,11 +724,11 @@ const shared = {
}

if (multiline) {
open.children.push(indent, newline);
join.children.push(',', newline);
close.children.push(dedent, newline);
open.push(indent, newline);
join.push(',', newline);
close.push(dedent, newline);
} else {
join.children.push(', ');
join.push(', ');
}
},

Expand Down Expand Up @@ -906,12 +920,12 @@ const handlers = {
const multiline = child_state.multiline;

if (multiline) {
if_true.children.push(indent, newline, '? ');
if_false.children.push(newline, ': ');
if_true.push(indent, newline, '? ');
if_false.push(newline, ': ');
state.commands.push(dedent);
} else {
if_true.children.push(' ? ');
if_false.children.push(' : ');
if_true.push(' ? ');
if_false.push(' : ');
}
},

Expand Down
44 changes: 14 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,21 @@ export function print(node, opts = {}) {
return;
}

switch (command.type) {
case 'Chunk':
const loc = command.loc;

if (loc) {
current_line.push([
current_column,
0, // source index is always zero
loc.start.line - 1,
loc.start.column
]);
}

append(command.content);

if (loc) {
current_line.push([
current_column,
0, // source index is always zero
loc.end.line - 1,
loc.end.column
]);
}
if (Array.isArray(command)) {
for (let i = 0; i < command.length; i += 1) {
run(command[i]);
}
return;
}

switch (command.type) {
case 'Location':
current_line.push([
current_column,
0, // source index is always zero
command.line - 1,
command.column
]);
break;

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

case 'Sequence':
for (let i = 0; i < command.children.length; i += 1) {
run(command.children[i]);
}

break;

case 'Comment':
if (command.comment.type === 'Line') {
append(`//${command.comment.value}`);
Expand Down
18 changes: 5 additions & 13 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ export interface State {
quote: "'" | '"';
}

export interface Chunk {
type: 'Chunk';
content: string;
loc: null | {
start: { line: number; column: number };
end: { line: number; column: number };
};
export interface Location {
type: 'Location';
line: number;
column: number;
}

export interface Newline {
Expand All @@ -64,17 +61,12 @@ export interface IndentChange {
offset: number;
}

export interface Sequence {
type: 'Sequence';
children: Command[];
}

export interface CommentChunk {
type: 'Comment';
comment: TSESTree.Comment;
}

export type Command = string | Chunk | Newline | Indent | Dedent | Sequence | CommentChunk;
export type Command = string | Location | Newline | Indent | Dedent | CommentChunk | Command[];

export interface PrintOptions {
sourceMapSource?: string;
Expand Down
3 changes: 2 additions & 1 deletion test/samples/large-file/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@
}));
},
eq(i) {
var len = this.length, j = +i + (i < 0 ? len : 0);
var len = this.length,
j = +i + (i < 0 ? len : 0);

return this.pushStack(j >= 0 && j < len ? [this[j]] : []);
},
Expand Down
2 changes: 1 addition & 1 deletion test/samples/large-file/expected.js.map

Large diffs are not rendered by default.

0 comments on commit c64eda1

Please sign in to comment.