@zackad/prettier-plugin-twig 0.17.0 → 0.18.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 +1 -1
- package/src/melody/melody-extension-core/operators.js +3 -2
- package/src/melody/melody-extension-core/parser/for.js +13 -12
- package/src/melody/melody-extension-core/parser/macro.js +19 -2
- package/src/melody/melody-extension-core/parser/props.js +13 -1
- package/src/melody/melody-extension-core/parser/set.js +7 -3
- package/src/melody/melody-extension-core/parser/types.js +15 -1
- package/src/melody/melody-parser/GenericTagParser.js +1 -1
- package/src/melody/melody-parser/Lexer.js +25 -6
- package/src/melody/melody-parser/Parser.js +180 -77
- package/src/melody/melody-parser/TokenStream.js +89 -18
- package/src/melody/melody-parser/TokenTypes.js +10 -0
- package/src/melody/melody-parser/index.js +8 -0
- package/src/melody/melody-parser/util.js +35 -0
- package/src/melody/melody-types/index.js +18 -1
- package/src/parser.js +1 -0
- package/src/print/BinaryExpression.js +23 -2
- package/src/print/ConditionalExpression.js +8 -1
- package/src/print/ErroredStatement.js +15 -0
- package/src/print/ExpressionStatement.js +4 -2
- package/src/print/ForStatement.js +29 -0
- package/src/print/MacroDeclarationStatement.js +5 -0
- package/src/print/SetStatement.js +32 -7
- package/src/print/StringLiteral.js +53 -18
- package/src/print/TwigComment.js +6 -10
- package/src/print/UnarySubclass.js +11 -1
- package/src/printer.js +14 -3
- package/src/util/printFunctions.js +74 -10
- package/src/util/publicFunctions.js +38 -0
- package/src/util/publicSymbols.js +7 -0
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
ERROR,
|
|
19
19
|
ERROR_TABLE,
|
|
20
20
|
COMMENT,
|
|
21
|
+
INLINE_COMMENT,
|
|
21
22
|
WHITESPACE,
|
|
22
23
|
HTML_COMMENT,
|
|
23
24
|
TAG_START,
|
|
@@ -33,6 +34,7 @@ import codeFrame from "../melody-code-frame/index.js";
|
|
|
33
34
|
|
|
34
35
|
const TOKENS = Symbol("TOKENS");
|
|
35
36
|
const LENGTH = Symbol("LENGTH");
|
|
37
|
+
const INLINE_COMMENTS = Symbol("INLINE_COMMENTS");
|
|
36
38
|
|
|
37
39
|
export default class TokenStream {
|
|
38
40
|
constructor(lexer, options) {
|
|
@@ -48,23 +50,59 @@ export default class TokenStream {
|
|
|
48
50
|
},
|
|
49
51
|
options
|
|
50
52
|
);
|
|
51
|
-
this[
|
|
53
|
+
this[INLINE_COMMENTS] = [];
|
|
54
|
+
this[TOKENS] = getAllTokens(
|
|
55
|
+
lexer,
|
|
56
|
+
mergedOptions,
|
|
57
|
+
this[INLINE_COMMENTS]
|
|
58
|
+
);
|
|
52
59
|
this[LENGTH] = this[TOKENS].length;
|
|
60
|
+
}
|
|
53
61
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
takeLeadingComments(token, filter = () => true) {
|
|
63
|
+
if (!token || !token.leadingComments) {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
const comments = token.leadingComments.filter(filter);
|
|
67
|
+
comments.forEach(c => (c.handled = true));
|
|
68
|
+
return comments;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
takeCommentsInRange(start, end) {
|
|
72
|
+
for (const comment of this[INLINE_COMMENTS]) {
|
|
73
|
+
if (comment.pos.index >= start && comment.end <= end) {
|
|
74
|
+
comment.handled = true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// The innermost statement reports them, so prettier-ignore can keep it verbatim
|
|
80
|
+
claimUnattachedComments(start, end) {
|
|
81
|
+
const comments = this[INLINE_COMMENTS].filter(
|
|
82
|
+
c => !c.handled && c.pos.index >= start && c.end <= end
|
|
83
|
+
);
|
|
84
|
+
comments.forEach(c => (c.handled = true));
|
|
85
|
+
return comments.length > 0
|
|
86
|
+
? this.createUnsupportedCommentError(comments[0])
|
|
87
|
+
: null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
assertInlineCommentsAttached() {
|
|
91
|
+
const comment = this[INLINE_COMMENTS].find(c => !c.handled);
|
|
92
|
+
if (comment) {
|
|
93
|
+
throw this.createUnsupportedCommentError(comment);
|
|
65
94
|
}
|
|
66
95
|
}
|
|
67
96
|
|
|
97
|
+
createUnsupportedCommentError(comment) {
|
|
98
|
+
return this.createError(
|
|
99
|
+
"Unsupported inline comment position",
|
|
100
|
+
comment.pos,
|
|
101
|
+
"Inline comments are only supported on their own line before a variable declaration in the props, types, set, for and macro tags, or before a key in a hash.",
|
|
102
|
+
comment.length
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
68
106
|
la(offset) {
|
|
69
107
|
const index = this.index + offset;
|
|
70
108
|
return index < this[LENGTH] ? this[TOKENS][index] : EOF_TOKEN;
|
|
@@ -85,6 +123,18 @@ export default class TokenStream {
|
|
|
85
123
|
}
|
|
86
124
|
const token = this[TOKENS][this.index];
|
|
87
125
|
this.index++;
|
|
126
|
+
if (token.type === ERROR) {
|
|
127
|
+
this.error(
|
|
128
|
+
token.message,
|
|
129
|
+
token.pos,
|
|
130
|
+
token.advice,
|
|
131
|
+
token.endPos.index - token.pos.index || 1,
|
|
132
|
+
{
|
|
133
|
+
errorType: "UNEXPECTED_TOKEN",
|
|
134
|
+
token
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
}
|
|
88
138
|
return token;
|
|
89
139
|
}
|
|
90
140
|
|
|
@@ -106,11 +156,19 @@ export default class TokenStream {
|
|
|
106
156
|
`Expected ${ERROR_TABLE[type] || type || text} but found ${
|
|
107
157
|
ERROR_TABLE[token.type] || token.type || token.text
|
|
108
158
|
} instead.`,
|
|
109
|
-
token.length
|
|
159
|
+
token.length,
|
|
160
|
+
{
|
|
161
|
+
errorType: "UNEXPECTED_TOKEN",
|
|
162
|
+
token
|
|
163
|
+
}
|
|
110
164
|
);
|
|
111
165
|
}
|
|
112
166
|
|
|
113
167
|
error(message, pos, advice, length = 1, metadata = {}) {
|
|
168
|
+
throw this.createError(message, pos, advice, length, metadata);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
createError(message, pos, advice, length = 1, metadata = {}) {
|
|
114
172
|
let errorMessage = `ERROR: ${message}\n`;
|
|
115
173
|
errorMessage += codeFrame({
|
|
116
174
|
rawLines: this.input.source,
|
|
@@ -128,16 +186,32 @@ export default class TokenStream {
|
|
|
128
186
|
}
|
|
129
187
|
const result = new Error(errorMessage);
|
|
130
188
|
Object.assign(result, metadata);
|
|
131
|
-
|
|
189
|
+
return result;
|
|
132
190
|
}
|
|
133
191
|
}
|
|
134
192
|
|
|
135
|
-
function getAllTokens(lexer, options) {
|
|
193
|
+
function getAllTokens(lexer, options, inlineComments = []) {
|
|
136
194
|
let token;
|
|
137
195
|
const tokens = [];
|
|
138
196
|
let acceptWhitespaceControl = false;
|
|
139
197
|
let trimNext = false;
|
|
198
|
+
let pendingInlineComments = [];
|
|
199
|
+
let previousToken = null;
|
|
140
200
|
while ((token = lexer.next()) !== EOF_TOKEN) {
|
|
201
|
+
if (token.type === INLINE_COMMENT) {
|
|
202
|
+
token.trailing =
|
|
203
|
+
!!previousToken && previousToken.endPos.line === token.pos.line;
|
|
204
|
+
inlineComments.push(token);
|
|
205
|
+
pendingInlineComments.push(token);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (token.type !== WHITESPACE) {
|
|
209
|
+
previousToken = token;
|
|
210
|
+
if (pendingInlineComments.length > 0) {
|
|
211
|
+
token.leadingComments = pendingInlineComments;
|
|
212
|
+
pendingInlineComments = [];
|
|
213
|
+
}
|
|
214
|
+
}
|
|
141
215
|
const shouldTrimNext = trimNext;
|
|
142
216
|
trimNext = false;
|
|
143
217
|
if (acceptWhitespaceControl) {
|
|
@@ -175,9 +249,6 @@ function getAllTokens(lexer, options) {
|
|
|
175
249
|
tokens[tokens.length] = token;
|
|
176
250
|
}
|
|
177
251
|
acceptWhitespaceControl = options.applyWhitespaceTrimming;
|
|
178
|
-
if (token.type === ERROR) {
|
|
179
|
-
return tokens;
|
|
180
|
-
}
|
|
181
252
|
}
|
|
182
253
|
return tokens;
|
|
183
254
|
}
|
|
@@ -23,6 +23,7 @@ export const STRING_START = "stringStart";
|
|
|
23
23
|
export const STRING_END = "stringEnd";
|
|
24
24
|
export const DECLARATION_START = "declarationStart";
|
|
25
25
|
export const COMMENT = "comment";
|
|
26
|
+
export const INLINE_COMMENT = "inlineComment";
|
|
26
27
|
export const WHITESPACE = "whitespace";
|
|
27
28
|
export const HTML_COMMENT = "htmlComment";
|
|
28
29
|
export const TEXT = "text";
|
|
@@ -65,6 +66,15 @@ export const EOF_TOKEN = {
|
|
|
65
66
|
text: ""
|
|
66
67
|
};
|
|
67
68
|
|
|
69
|
+
export const DELIMITER_TABLE = {
|
|
70
|
+
[DECLARATION_START]: ELEMENT_END,
|
|
71
|
+
[ELEMENT_START]: ELEMENT_END,
|
|
72
|
+
[EXPRESSION_START]: EXPRESSION_END,
|
|
73
|
+
[INTERPOLATION_START]: INTERPOLATION_END,
|
|
74
|
+
[STRING_START]: STRING_END,
|
|
75
|
+
[TAG_START]: TAG_END
|
|
76
|
+
};
|
|
77
|
+
|
|
68
78
|
export const ERROR_TABLE = {
|
|
69
79
|
[EXPRESSION_END]: 'expression end "}}"',
|
|
70
80
|
[EXPRESSION_START]: 'expression start "{{"',
|
|
@@ -27,6 +27,10 @@ import {
|
|
|
27
27
|
copyLoc,
|
|
28
28
|
getNodeSource,
|
|
29
29
|
createNode,
|
|
30
|
+
attachLeadingComments,
|
|
31
|
+
attachListItemComments,
|
|
32
|
+
attachTrailingComments,
|
|
33
|
+
INLINE_COMMENT_ERROR,
|
|
30
34
|
hasTagStartTokenTrimLeft,
|
|
31
35
|
hasTagEndTokenTrimRight,
|
|
32
36
|
isMelodyExtension
|
|
@@ -88,6 +92,10 @@ export {
|
|
|
88
92
|
copyLoc,
|
|
89
93
|
getNodeSource,
|
|
90
94
|
createNode,
|
|
95
|
+
attachLeadingComments,
|
|
96
|
+
attachListItemComments,
|
|
97
|
+
attachTrailingComments,
|
|
98
|
+
INLINE_COMMENT_ERROR,
|
|
91
99
|
hasTagStartTokenTrimLeft,
|
|
92
100
|
hasTagEndTokenTrimRight,
|
|
93
101
|
Types
|
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
+
export const INLINE_COMMENT_ERROR = Symbol("INLINE_COMMENT_ERROR");
|
|
17
|
+
|
|
16
18
|
export function setStartFromToken(node, { pos: { index, line, column } }) {
|
|
17
19
|
node.loc.start = { line, column, index };
|
|
18
20
|
return node;
|
|
@@ -74,6 +76,39 @@ export function createNode(Type, token, ...args) {
|
|
|
74
76
|
return setEndFromToken(setStartFromToken(new Type(...args), token), token);
|
|
75
77
|
}
|
|
76
78
|
|
|
79
|
+
function addComment(node, key, comment) {
|
|
80
|
+
node[key] = node[key] || [];
|
|
81
|
+
node[key].push(comment.text.trimEnd());
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function attachLeadingComments(node, tokens, token) {
|
|
85
|
+
for (const comment of tokens.takeLeadingComments(token)) {
|
|
86
|
+
addComment(node, "leadingComments", comment);
|
|
87
|
+
}
|
|
88
|
+
return node;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Comments on the line of the previous list item stay with that item
|
|
92
|
+
export function attachListItemComments(node, previousNode, tokens, token) {
|
|
93
|
+
for (const comment of tokens.takeLeadingComments(token)) {
|
|
94
|
+
if (comment.trailing && previousNode) {
|
|
95
|
+
addComment(previousNode, "trailingComments", comment);
|
|
96
|
+
} else {
|
|
97
|
+
addComment(node, "leadingComments", comment);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function attachTrailingComments(node, tokens, token) {
|
|
104
|
+
if (!node) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
for (const comment of tokens.takeLeadingComments(token, c => c.trailing)) {
|
|
108
|
+
addComment(node, "trailingComments", comment);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
77
112
|
export function startNode(Type, token, ...args) {
|
|
78
113
|
return setStartFromToken(new Type(...args), token);
|
|
79
114
|
}
|
|
@@ -40,7 +40,9 @@ export class Node {
|
|
|
40
40
|
}
|
|
41
41
|
const value = this[name];
|
|
42
42
|
if (Array.isArray(value)) {
|
|
43
|
-
acc[name] = value.map(val =>
|
|
43
|
+
acc[name] = value.map(val =>
|
|
44
|
+
val && val.toJSON ? val.toJSON() : val
|
|
45
|
+
);
|
|
44
46
|
} else {
|
|
45
47
|
acc[name] = value && value.toJSON ? value.toJSON() : value;
|
|
46
48
|
}
|
|
@@ -157,6 +159,21 @@ type(PrintTextStatement, "PrintTextStatement");
|
|
|
157
159
|
alias(PrintTextStatement, "Statement", "PrintStatement");
|
|
158
160
|
visitor(PrintTextStatement, "value");
|
|
159
161
|
|
|
162
|
+
export class PrintErroredStatement extends Node {
|
|
163
|
+
/**
|
|
164
|
+
* @param {StringLiteral} input
|
|
165
|
+
* @param {?Error} [cause]
|
|
166
|
+
*/
|
|
167
|
+
constructor(input, cause = null) {
|
|
168
|
+
super();
|
|
169
|
+
this.value = input;
|
|
170
|
+
this.cause = cause;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
type(PrintErroredStatement, "PrintErroredStatement");
|
|
174
|
+
alias(PrintErroredStatement, "Statement", "PrintStatement");
|
|
175
|
+
visitor(PrintErroredStatement, "value");
|
|
176
|
+
|
|
160
177
|
export class ConstantValue extends Node {
|
|
161
178
|
constructor(value) {
|
|
162
179
|
super();
|
package/src/parser.js
CHANGED
|
@@ -88,6 +88,7 @@ const parse = (text, parsers, options) => {
|
|
|
88
88
|
const extensions = [coreExtension];
|
|
89
89
|
const parser = createConfiguredParser(text, multiTagConfig, ...extensions);
|
|
90
90
|
const ast = parser.parse();
|
|
91
|
+
parser.tokens.assertInlineCommentsAttached();
|
|
91
92
|
ast[ORIGINAL_SOURCE] = text;
|
|
92
93
|
return ast;
|
|
93
94
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { doc } from "prettier";
|
|
2
2
|
import { Node } from "../melody/melody-types/index.js";
|
|
3
3
|
import { extension as coreExtension } from "../melody/melody-extension-core/index.js";
|
|
4
|
+
import { LEFT } from "../melody/melody-parser/index.js";
|
|
4
5
|
import {
|
|
5
6
|
EXPRESSION_NEEDED,
|
|
6
7
|
STRING_NEEDS_QUOTES,
|
|
@@ -22,6 +23,14 @@ const operatorPrecedence = coreExtension.binaryOperators.reduce((acc, curr) => {
|
|
|
22
23
|
return acc;
|
|
23
24
|
}, {});
|
|
24
25
|
|
|
26
|
+
const operatorAssociativity = coreExtension.binaryOperators.reduce(
|
|
27
|
+
(acc, curr) => {
|
|
28
|
+
acc[curr.text] = curr.associativity;
|
|
29
|
+
return acc;
|
|
30
|
+
},
|
|
31
|
+
{}
|
|
32
|
+
);
|
|
33
|
+
|
|
25
34
|
const printInterpolatedString = (node, path, print, options) => {
|
|
26
35
|
node[STRING_NEEDS_QUOTES] = false;
|
|
27
36
|
node[INSIDE_OF_STRING] = true;
|
|
@@ -47,6 +56,8 @@ const hasLogicalOperator = node => {
|
|
|
47
56
|
return node.operator === "or" || node.operator === "and";
|
|
48
57
|
};
|
|
49
58
|
|
|
59
|
+
const isSignOperator = operator => operator === "-" || operator === "+";
|
|
60
|
+
|
|
50
61
|
const otherNeedsParentheses = (node, otherProp) => {
|
|
51
62
|
const other = node[otherProp];
|
|
52
63
|
const isBinaryOther = Node.isBinaryExpression(other);
|
|
@@ -54,14 +65,24 @@ const otherNeedsParentheses = (node, otherProp) => {
|
|
|
54
65
|
const otherPrecedence = isBinaryOther
|
|
55
66
|
? operatorPrecedence[node[otherProp].operator]
|
|
56
67
|
: Number.MAX_SAFE_INTEGER;
|
|
68
|
+
const sameSideAsAssociativity =
|
|
69
|
+
(operatorAssociativity[node.operator] === LEFT) ===
|
|
70
|
+
(otherProp === "left");
|
|
57
71
|
return (
|
|
58
72
|
otherPrecedence < ownPrecedence ||
|
|
73
|
+
(otherPrecedence === ownPrecedence &&
|
|
74
|
+
isBinaryOther &&
|
|
75
|
+
!other.wasImplicitConcatenation &&
|
|
76
|
+
!sameSideAsAssociativity) ||
|
|
59
77
|
(otherPrecedence > ownPrecedence &&
|
|
60
78
|
isBinaryOther &&
|
|
61
79
|
hasLogicalOperator(other)) ||
|
|
62
80
|
Node.isFilterExpression(other) ||
|
|
63
|
-
|
|
64
|
-
|
|
81
|
+
Node.isConditionalExpression(other) ||
|
|
82
|
+
(node.operator === "**" &&
|
|
83
|
+
otherProp === "left" &&
|
|
84
|
+
Node.isUnaryLike(other) &&
|
|
85
|
+
isSignOperator(other.operator))
|
|
65
86
|
);
|
|
66
87
|
};
|
|
67
88
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "../melody/melody-types/index.js";
|
|
2
3
|
import {
|
|
3
4
|
EXPRESSION_NEEDED,
|
|
4
5
|
STRING_NEEDS_QUOTES,
|
|
@@ -18,7 +19,13 @@ const printConditionalExpression = (node, path, print) => {
|
|
|
18
19
|
if (node.alternate) {
|
|
19
20
|
rest.push(line, ": ", path.call(print, "alternate"));
|
|
20
21
|
}
|
|
21
|
-
const
|
|
22
|
+
const printedTest = path.call(print, "test");
|
|
23
|
+
const parts = [
|
|
24
|
+
Node.isConditionalExpression(node.test)
|
|
25
|
+
? ["(", printedTest, ")"]
|
|
26
|
+
: printedTest,
|
|
27
|
+
indent(rest)
|
|
28
|
+
];
|
|
22
29
|
wrapExpressionIfNeeded(path, parts, node);
|
|
23
30
|
|
|
24
31
|
return group(parts);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { PRETTIER_IGNORED_CODE } from "../util/publicSymbols.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {import('../../melody-types/index.js').PrintErroredStatement} node
|
|
6
|
+
*/
|
|
7
|
+
const printErroredStatement = (node, path, print) => {
|
|
8
|
+
if (node[PRETTIER_IGNORED_CODE]) {
|
|
9
|
+
return path.call(print, "value");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
throw node.cause;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { printErroredStatement };
|
|
@@ -3,7 +3,8 @@ import { Node } from "../melody/melody-types/index.js";
|
|
|
3
3
|
import {
|
|
4
4
|
EXPRESSION_NEEDED,
|
|
5
5
|
STRING_NEEDS_QUOTES,
|
|
6
|
-
isContractableNodeType
|
|
6
|
+
isContractableNodeType,
|
|
7
|
+
hasComments
|
|
7
8
|
} from "../util/index.js";
|
|
8
9
|
|
|
9
10
|
const { group, indent, line } = doc.builders;
|
|
@@ -15,7 +16,8 @@ const printExpressionStatement = (node, path, print) => {
|
|
|
15
16
|
const closing = node.trimRight ? "-}}" : "}}";
|
|
16
17
|
const shouldContractValue =
|
|
17
18
|
isContractableNodeType(node.value) &&
|
|
18
|
-
!Node.isObjectExpression(node.value)
|
|
19
|
+
!Node.isObjectExpression(node.value) &&
|
|
20
|
+
!hasComments(node.value);
|
|
19
21
|
const padding = shouldContractValue ? " " : line;
|
|
20
22
|
const printedValue = [padding, path.call(print, "value")];
|
|
21
23
|
const value = shouldContractValue ? printedValue : indent(printedValue);
|
|
@@ -2,12 +2,41 @@ import { doc } from "prettier";
|
|
|
2
2
|
import {
|
|
3
3
|
EXPRESSION_NEEDED,
|
|
4
4
|
isWhitespaceNode,
|
|
5
|
+
hasLeadingComments,
|
|
5
6
|
indentWithHardline
|
|
6
7
|
} from "../util/index.js";
|
|
7
8
|
|
|
8
9
|
const { group, indent, line, hardline } = doc.builders;
|
|
9
10
|
|
|
11
|
+
const printForWithTargetComments = (node, path, print) => {
|
|
12
|
+
const targets = [];
|
|
13
|
+
if (node.keyTarget) {
|
|
14
|
+
targets.push(path.call(print, "keyTarget"), ",", line);
|
|
15
|
+
}
|
|
16
|
+
targets.push(
|
|
17
|
+
path.call(print, "valueTarget"),
|
|
18
|
+
" in ",
|
|
19
|
+
path.call(print, "sequence")
|
|
20
|
+
);
|
|
21
|
+
if (node.condition) {
|
|
22
|
+
targets.push(line, "if ", path.call(print, "condition"));
|
|
23
|
+
}
|
|
24
|
+
return group([
|
|
25
|
+
node.trimLeft ? "{%-" : "{%",
|
|
26
|
+
" for",
|
|
27
|
+
indent([hardline, targets]),
|
|
28
|
+
hardline,
|
|
29
|
+
node.trimRightFor ? "-%}" : "%}"
|
|
30
|
+
]);
|
|
31
|
+
};
|
|
32
|
+
|
|
10
33
|
const printFor = (node, path, print) => {
|
|
34
|
+
if (
|
|
35
|
+
hasLeadingComments(node.keyTarget) ||
|
|
36
|
+
hasLeadingComments(node.valueTarget)
|
|
37
|
+
) {
|
|
38
|
+
return printForWithTargetComments(node, path, print);
|
|
39
|
+
}
|
|
11
40
|
const parts = [node.trimLeft ? "{%-" : "{%", " for "];
|
|
12
41
|
if (node.keyTarget) {
|
|
13
42
|
parts.push(path.call(print, "keyTarget"), ", ");
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { doc } from "prettier";
|
|
2
|
+
import { hasTrailingComments } from "../util/index.js";
|
|
2
3
|
|
|
3
4
|
const { group, join, line, softline, hardline, indent } = doc.builders;
|
|
4
5
|
|
|
@@ -12,6 +13,10 @@ const printOpener = (node, path, print) => {
|
|
|
12
13
|
const mappedArguments = path.map(print, "arguments");
|
|
13
14
|
const joinedArguments = join([",", line], mappedArguments);
|
|
14
15
|
parts.push(indent([softline, joinedArguments]));
|
|
16
|
+
// The closing parenthesis must not end up in a trailing comment
|
|
17
|
+
if (hasTrailingComments(node.arguments[node.arguments.length - 1])) {
|
|
18
|
+
parts.push(softline);
|
|
19
|
+
}
|
|
15
20
|
parts.push(")", line, node.trimRightMacro ? "-%}" : "%}");
|
|
16
21
|
return group(parts);
|
|
17
22
|
};
|
|
@@ -3,11 +3,13 @@ import { Node } from "../melody/melody-types/index.js";
|
|
|
3
3
|
import {
|
|
4
4
|
printChildBlock,
|
|
5
5
|
isNotExpression,
|
|
6
|
+
hasLeadingComments,
|
|
7
|
+
printComments,
|
|
6
8
|
STRING_NEEDS_QUOTES,
|
|
7
9
|
GROUP_TOP_LEVEL_LOGICAL
|
|
8
10
|
} from "../util/index.js";
|
|
9
11
|
|
|
10
|
-
const { group, line, hardline } = doc.builders;
|
|
12
|
+
const { group, indent, line, hardline } = doc.builders;
|
|
11
13
|
|
|
12
14
|
const shouldAvoidBreakBeforeClosing = valueNode =>
|
|
13
15
|
Node.isObjectExpression(valueNode) ||
|
|
@@ -21,6 +23,19 @@ const buildSetStatement = (node, path, print, assignmentIndex) => {
|
|
|
21
23
|
varDeclaration.value
|
|
22
24
|
);
|
|
23
25
|
|
|
26
|
+
if (hasLeadingComments(varDeclaration)) {
|
|
27
|
+
return [
|
|
28
|
+
node.trimLeft ? "{%-" : "{%",
|
|
29
|
+
" set",
|
|
30
|
+
indent([
|
|
31
|
+
hardline,
|
|
32
|
+
path.call(print, "assignments", assignmentIndex)
|
|
33
|
+
]),
|
|
34
|
+
hardline,
|
|
35
|
+
node.trimRight ? "-%}" : "%}"
|
|
36
|
+
];
|
|
37
|
+
}
|
|
38
|
+
|
|
24
39
|
return group([
|
|
25
40
|
node.trimLeft ? "{%-" : "{%",
|
|
26
41
|
" set ",
|
|
@@ -54,12 +69,22 @@ const printRegularSet = (node, path, print) => {
|
|
|
54
69
|
};
|
|
55
70
|
|
|
56
71
|
const printEmbracingSet = (node, path, print) => {
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
72
|
+
const assignment = node.assignments[0];
|
|
73
|
+
const printedName = path.call(print, "assignments", "0", "name");
|
|
74
|
+
const parts = hasLeadingComments(assignment)
|
|
75
|
+
? [
|
|
76
|
+
node.trimLeft ? "{%-" : "{%",
|
|
77
|
+
" set",
|
|
78
|
+
indent([hardline, printComments(assignment, printedName)]),
|
|
79
|
+
hardline,
|
|
80
|
+
node.trimRightSet ? "-%}" : "%}"
|
|
81
|
+
]
|
|
82
|
+
: [
|
|
83
|
+
node.trimLeft ? "{%-" : "{%",
|
|
84
|
+
" set ",
|
|
85
|
+
printedName,
|
|
86
|
+
node.trimRightSet ? " -%}" : " %}"
|
|
87
|
+
];
|
|
63
88
|
node[STRING_NEEDS_QUOTES] = false;
|
|
64
89
|
const printedContents = printChildBlock(
|
|
65
90
|
node,
|
|
@@ -2,35 +2,66 @@ import {
|
|
|
2
2
|
firstValueInAncestorChain,
|
|
3
3
|
quoteChar,
|
|
4
4
|
STRING_NEEDS_QUOTES,
|
|
5
|
-
OVERRIDE_QUOTE_CHAR
|
|
5
|
+
OVERRIDE_QUOTE_CHAR,
|
|
6
|
+
INSIDE_OF_STRING
|
|
6
7
|
} from "../util/index.js";
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Lists the quote characters of a string literal value that are not
|
|
11
|
+
* part of an escape sequence (a backslash escapes the next character)
|
|
12
|
+
*/
|
|
13
|
+
const unescapedQuotes = s => {
|
|
14
|
+
const quotes = new Set();
|
|
15
|
+
for (let i = 0; i < s.length; i++) {
|
|
16
|
+
if (s[i] === "\\") {
|
|
17
|
+
i++;
|
|
18
|
+
} else if (s[i] === "'" || s[i] === '"') {
|
|
19
|
+
quotes.add(s[i]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return quotes;
|
|
10
23
|
};
|
|
11
24
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Escapes the occurrences of the given quote character that are not
|
|
27
|
+
* already part of an escape sequence
|
|
28
|
+
*/
|
|
29
|
+
const escapeQuotes = (s, quote) => {
|
|
30
|
+
let result = "";
|
|
31
|
+
for (let i = 0; i < s.length; i++) {
|
|
32
|
+
if (s[i] === "\\") {
|
|
33
|
+
result += s.slice(i, i + 2);
|
|
34
|
+
i++;
|
|
35
|
+
} else {
|
|
36
|
+
result += s[i] === quote ? "\\" + quote : s[i];
|
|
17
37
|
}
|
|
18
|
-
pos = s.indexOf(char, pos + 1);
|
|
19
38
|
}
|
|
20
|
-
return
|
|
39
|
+
return result;
|
|
21
40
|
};
|
|
22
41
|
|
|
23
|
-
const
|
|
24
|
-
const containsUnmaskedDoubleQuote = containsUnmasked('"');
|
|
42
|
+
const alternate = quote => (quote === '"' ? "'" : '"');
|
|
25
43
|
|
|
26
44
|
const getQuoteChar = (s, options) => {
|
|
27
|
-
|
|
28
|
-
|
|
45
|
+
const preferred = quoteChar(options);
|
|
46
|
+
const quotes = unescapedQuotes(s);
|
|
47
|
+
if (quotes.has(preferred) && !quotes.has(alternate(preferred))) {
|
|
48
|
+
return alternate(preferred);
|
|
29
49
|
}
|
|
30
|
-
|
|
31
|
-
|
|
50
|
+
return preferred;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Whether the string literal is a fragment of an interpolated
|
|
55
|
+
* string ("... #{ expr } ..."), which is always double quoted
|
|
56
|
+
*/
|
|
57
|
+
const isInterpolatedStringFragment = path => {
|
|
58
|
+
for (let i = path.stack.length - 2; i >= 0; i--) {
|
|
59
|
+
const element = path.stack[i];
|
|
60
|
+
if (element && element[STRING_NEEDS_QUOTES] !== undefined) {
|
|
61
|
+
return element[INSIDE_OF_STRING] === true;
|
|
62
|
+
}
|
|
32
63
|
}
|
|
33
|
-
return
|
|
64
|
+
return false;
|
|
34
65
|
};
|
|
35
66
|
|
|
36
67
|
const printStringLiteral = (node, path, print, options) => {
|
|
@@ -54,7 +85,11 @@ const printStringLiteral = (node, path, print, options) => {
|
|
|
54
85
|
const quote = overridingQuoteChar
|
|
55
86
|
? overridingQuoteChar
|
|
56
87
|
: getQuoteChar(node.value, options);
|
|
57
|
-
return quote + node.value + quote;
|
|
88
|
+
return quote + escapeQuotes(node.value, quote) + quote;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (isInterpolatedStringFragment(path)) {
|
|
92
|
+
return escapeQuotes(node.value, '"');
|
|
58
93
|
}
|
|
59
94
|
|
|
60
95
|
return node.value;
|
package/src/print/TwigComment.js
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
normalizeTwigComment,
|
|
2
|
+
parseTwigComment,
|
|
3
|
+
unifyWhitespace,
|
|
5
4
|
countNewlines
|
|
6
5
|
} from "../util/index.js";
|
|
7
6
|
|
|
8
7
|
const printTwigComment = node => {
|
|
9
8
|
const originalText = node.value.value || "";
|
|
10
|
-
const
|
|
11
|
-
const trimLeft = originalText.length >= 3 ? originalText[2] === "-" : false;
|
|
12
|
-
const trimRight =
|
|
13
|
-
originalText.length >= 3 ? originalText.slice(-3, -2) === "-" : false;
|
|
9
|
+
const { text, open, close } = parseTwigComment(originalText);
|
|
14
10
|
|
|
15
|
-
const numNewlines = countNewlines(
|
|
11
|
+
const numNewlines = countNewlines(text);
|
|
16
12
|
if (numNewlines === 0) {
|
|
17
|
-
return
|
|
13
|
+
return [open, " ", unifyWhitespace(text), " ", close];
|
|
18
14
|
}
|
|
19
15
|
|
|
20
|
-
return [
|
|
16
|
+
return [open, text, close];
|
|
21
17
|
};
|
|
22
18
|
|
|
23
19
|
export { printTwigComment };
|