Skip to content

Commit 647fcbe

Browse files
committed
WIP
1 parent 85b532b commit 647fcbe

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

src/handlers.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ function measure(commands, from, to = commands.length) {
3030
if (typeof command === 'string') {
3131
total += command.length;
3232
} else if (Array.isArray(command)) {
33-
total += 2; // assume this is ', '
34-
} else if (command.type === 'Chunk') {
35-
total += command.content.length;
33+
total +=
34+
command.length === 0
35+
? 2 // assume this is ', '
36+
: measure(command, 0);
3637
}
3738
}
3839

@@ -64,17 +65,32 @@ export function handle(node, state) {
6465
}
6566
}
6667

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+
6781
/**
6882
* @param {string} content
6983
* @param {TSESTree.Node} node
7084
* @returns {Chunk}
7185
*/
7286
function c(content, node) {
73-
return {
74-
type: 'Chunk',
75-
content,
76-
loc: node?.loc ?? null
77-
};
87+
return node.loc
88+
? [
89+
l(node.loc.start.line - 1, node.loc.start.column),
90+
content,
91+
l(node.loc.end.line - 1, node.loc.end.column)
92+
]
93+
: content;
7894
}
7995

8096
/**

src/index.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,13 @@ export function print(node, opts = {}) {
8787
}
8888

8989
switch (command.type) {
90-
case 'Chunk':
91-
const loc = command.loc;
92-
93-
if (loc) {
94-
current_line.push([
95-
current_column,
96-
0, // source index is always zero
97-
loc.start.line - 1,
98-
loc.start.column
99-
]);
100-
}
101-
102-
append(command.content);
103-
104-
if (loc) {
105-
current_line.push([
106-
current_column,
107-
0, // source index is always zero
108-
loc.end.line - 1,
109-
loc.end.column
110-
]);
111-
}
112-
90+
case 'Location':
91+
current_line.push([
92+
current_column,
93+
0, // source index is always zero
94+
command.line,
95+
command.column
96+
]);
11397
break;
11498

11599
case 'Newline':

src/types.d.ts

Lines changed: 5 additions & 8 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 {
@@ -69,7 +66,7 @@ export interface CommentChunk {
6966
comment: TSESTree.Comment;
7067
}
7168

72-
export type Command = string | Chunk | Newline | Indent | Dedent | CommentChunk | Command[];
69+
export type Command = string | Location | Newline | Indent | Dedent | CommentChunk | Command[];
7370

7471
export interface PrintOptions {
7572
sourceMapSource?: string;

0 commit comments

Comments
 (0)