Skip to content

Commit 06cf8d0

Browse files
committed
handle quotes earlier
1 parent 26c30d4 commit 06cf8d0

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/handlers.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ function c(content, node, quote = false) {
7676
return {
7777
type: 'Chunk',
7878
content,
79-
quote,
8079
loc: node?.loc ?? null
8180
};
8281
}
@@ -98,6 +97,14 @@ function prepend_comments(comments, state, newlines) {
9897
}
9998
}
10099

100+
/**
101+
* @param {string} string
102+
* @param {'\'' | '"'} char
103+
*/
104+
function quote(string, char) {
105+
return char + string.replaceAll(char, '\\' + char) + char;
106+
}
107+
101108
const OPERATOR_PRECEDENCE = {
102109
'||': 2,
103110
'&&': 3,
@@ -1133,11 +1140,9 @@ const handlers = {
11331140
return;
11341141
}
11351142

1136-
const isString = typeof node.value === 'string';
1137-
const addQuotes = isString;
1138-
value = isString ? node.value : String(node.value);
1143+
value = typeof node.value === 'string' ? quote(node.value, state.quote) : String(node.value);
11391144

1140-
state.commands.push(c(value, node, addQuotes));
1145+
state.commands.push(c(value, node));
11411146
},
11421147

11431148
LogicalExpression: shared['BinaryExpression|LogicalExpression'],

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export function print(node, opts = {}) {
3737
const state = {
3838
commands: [],
3939
comments: [],
40-
multiline: false
40+
multiline: false,
41+
quote: opts.quotes === 'double' ? '"' : "'"
4142
};
4243

4344
handle(/** @type {TSESTree.Node} */ (node), state);

src/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ export interface State {
3535
commands: Command[];
3636
comments: TSESTree.Comment[];
3737
multiline: boolean;
38+
quote: "'" | '"';
3839
}
3940

4041
export interface Chunk {
4142
type: 'Chunk';
4243
content: string;
43-
quote: boolean;
4444
loc: null | {
4545
start: { line: number; column: number };
4646
end: { line: number; column: number };

0 commit comments

Comments
 (0)