@zackad/prettier-plugin-twig 0.17.0 → 0.18.0
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/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 +18 -0
- 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/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/TwigComment.js +6 -10
- 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
|
};
|
|
@@ -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,
|
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 };
|
package/src/printer.js
CHANGED
|
@@ -5,6 +5,7 @@ import { printElement } from "./print/Element.js";
|
|
|
5
5
|
import { printAttribute } from "./print/Attribute.js";
|
|
6
6
|
import { printIdentifier } from "./print/Identifier.js";
|
|
7
7
|
import { printExpressionStatement } from "./print/ExpressionStatement.js";
|
|
8
|
+
import { printErroredStatement } from "./print/ErroredStatement.js";
|
|
8
9
|
import { printMemberExpression } from "./print/MemberExpression.js";
|
|
9
10
|
import { printFilterExpression } from "./print/FilterExpression.js";
|
|
10
11
|
import { printArrowFunction } from "./print/ArrowFunction.js";
|
|
@@ -49,11 +50,14 @@ import { printSwitchTag } from "./print/SwitchTag.js";
|
|
|
49
50
|
import { printTypesDeclarationStatement } from "./print/TypesDeclarationStatement.js";
|
|
50
51
|
import { printTypesVariableDeclaration } from "./print/TypesVariableDeclaration.js";
|
|
51
52
|
import {
|
|
53
|
+
printComments,
|
|
52
54
|
isWhitespaceNode,
|
|
53
55
|
isHtmlCommentEqualTo,
|
|
54
56
|
isTwigCommentEqualTo
|
|
55
57
|
} from "./util/index.js";
|
|
58
|
+
import { PRETTIER_IGNORED_CODE } from "./util/publicSymbols.js";
|
|
56
59
|
import { ORIGINAL_SOURCE } from "./parser.js";
|
|
60
|
+
import { INLINE_COMMENT_ERROR } from "./melody/melody-parser/index.js";
|
|
57
61
|
|
|
58
62
|
const printFunctions = {};
|
|
59
63
|
|
|
@@ -102,15 +106,21 @@ const print = (path, options, print) => {
|
|
|
102
106
|
}
|
|
103
107
|
|
|
104
108
|
checkForIgnoreEnd(node);
|
|
105
|
-
|
|
109
|
+
node[PRETTIER_IGNORED_CODE] =
|
|
106
110
|
(shouldApplyIgnoreNext(node) && ignoreNext) || ignoreRegion;
|
|
107
111
|
const hasPrintFunction = printFunctions[nodeType];
|
|
108
112
|
|
|
109
113
|
// Happy path: We have a formatting function, and the user wants the
|
|
110
114
|
// node formatted
|
|
111
|
-
if (!
|
|
115
|
+
if (!node[PRETTIER_IGNORED_CODE] && hasPrintFunction) {
|
|
116
|
+
if (node[INLINE_COMMENT_ERROR]) {
|
|
117
|
+
throw node[INLINE_COMMENT_ERROR];
|
|
118
|
+
}
|
|
112
119
|
checkForIgnoreStart(node);
|
|
113
|
-
return
|
|
120
|
+
return printComments(
|
|
121
|
+
node,
|
|
122
|
+
printFunctions[nodeType](node, path, print, options)
|
|
123
|
+
);
|
|
114
124
|
} else if (!hasPrintFunction) {
|
|
115
125
|
console.warn(`No print function available for node type "${nodeType}"`);
|
|
116
126
|
}
|
|
@@ -193,6 +203,7 @@ printFunctions["Element"] = printElement;
|
|
|
193
203
|
printFunctions["Attribute"] = printAttribute;
|
|
194
204
|
printFunctions["PrintTextStatement"] = printTextStatement;
|
|
195
205
|
printFunctions["PrintExpressionStatement"] = printExpressionStatement;
|
|
206
|
+
printFunctions["PrintErroredStatement"] = printErroredStatement;
|
|
196
207
|
printFunctions["MemberExpression"] = printMemberExpression;
|
|
197
208
|
printFunctions["FilterExpression"] = printFilterExpression;
|
|
198
209
|
printFunctions["ArrowFunction"] = printArrowFunction;
|
|
@@ -1,28 +1,54 @@
|
|
|
1
1
|
import { doc } from "prettier";
|
|
2
2
|
import { Node } from "../melody/melody-types/index.js";
|
|
3
3
|
|
|
4
|
-
const { line, indent, group } =
|
|
4
|
+
const { line, indent, group, hardline, join, lineSuffix, breakParent } =
|
|
5
|
+
doc.builders;
|
|
5
6
|
|
|
6
7
|
const noSpaceBeforeToken = {
|
|
7
8
|
",": true
|
|
8
9
|
};
|
|
9
10
|
|
|
11
|
+
const noSpaceAfterToken = {};
|
|
12
|
+
|
|
10
13
|
const printSingleTwigTag = (node, path, print) => {
|
|
11
14
|
const opener = node.trimLeft ? "{%-" : "{%";
|
|
12
15
|
const parts = [opener, " ", node.tagName];
|
|
13
16
|
const printedParts = path.map(print, "parts");
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
|
|
18
|
+
const groupedNodes = [];
|
|
19
|
+
const groupedDocs = [];
|
|
20
|
+
for (let i = 0; i < node.parts.length; i++) {
|
|
21
|
+
const equalsToken = node.parts[i + 1];
|
|
22
|
+
const isAssignment =
|
|
23
|
+
Node.isGenericToken(equalsToken) &&
|
|
24
|
+
equalsToken.tokenText === "=" &&
|
|
25
|
+
node.parts[i + 2] !== undefined;
|
|
26
|
+
if (isAssignment) {
|
|
27
|
+
groupedNodes.push(node.parts[i]);
|
|
28
|
+
groupedDocs.push([printedParts[i], " = ", printedParts[i + 2]]);
|
|
29
|
+
i += 2;
|
|
30
|
+
} else {
|
|
31
|
+
groupedNodes.push(node.parts[i]);
|
|
32
|
+
groupedDocs.push(printedParts[i]);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (groupedDocs.length > 0) {
|
|
37
|
+
parts.push(" ", groupedDocs[0]);
|
|
16
38
|
}
|
|
17
39
|
const indentedParts = [];
|
|
18
|
-
for (let i = 1; i <
|
|
19
|
-
const part =
|
|
40
|
+
for (let i = 1; i < groupedDocs.length; i++) {
|
|
41
|
+
const part = groupedNodes[i];
|
|
42
|
+
const prevPart = groupedNodes[i - 1];
|
|
20
43
|
const isToken = Node.isGenericToken(part);
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
44
|
+
const isPrevToken = Node.isGenericToken(prevPart);
|
|
45
|
+
const noSpaceBefore = isToken && noSpaceBeforeToken[part.tokenText];
|
|
46
|
+
const noSpaceAfter =
|
|
47
|
+
isPrevToken && noSpaceAfterToken[prevPart.tokenText];
|
|
48
|
+
const separator = noSpaceBefore || noSpaceAfter ? "" : line;
|
|
49
|
+
indentedParts.push(separator, groupedDocs[i]);
|
|
24
50
|
}
|
|
25
|
-
if (
|
|
51
|
+
if (groupedDocs.length > 1) {
|
|
26
52
|
parts.push(indent(indentedParts));
|
|
27
53
|
}
|
|
28
54
|
const closing = node.trimRight ? "-%}" : "%}";
|
|
@@ -30,4 +56,42 @@ const printSingleTwigTag = (node, path, print) => {
|
|
|
30
56
|
return group(parts);
|
|
31
57
|
};
|
|
32
58
|
|
|
33
|
-
|
|
59
|
+
const hasLeadingComments = node =>
|
|
60
|
+
!!node &&
|
|
61
|
+
Array.isArray(node.leadingComments) &&
|
|
62
|
+
node.leadingComments.length > 0;
|
|
63
|
+
|
|
64
|
+
const hasTrailingComments = node =>
|
|
65
|
+
!!node &&
|
|
66
|
+
Array.isArray(node.trailingComments) &&
|
|
67
|
+
node.trailingComments.length > 0;
|
|
68
|
+
|
|
69
|
+
const hasComments = node =>
|
|
70
|
+
hasLeadingComments(node) || hasTrailingComments(node);
|
|
71
|
+
|
|
72
|
+
// Own group, so the line breaks forced by the comments do not break the node itself
|
|
73
|
+
const printComments = (node, printed) => {
|
|
74
|
+
if (!hasComments(node)) {
|
|
75
|
+
return printed;
|
|
76
|
+
}
|
|
77
|
+
const parts = [];
|
|
78
|
+
if (hasLeadingComments(node)) {
|
|
79
|
+
parts.push(join(hardline, node.leadingComments), hardline);
|
|
80
|
+
}
|
|
81
|
+
parts.push(group(printed));
|
|
82
|
+
if (hasTrailingComments(node)) {
|
|
83
|
+
parts.push(
|
|
84
|
+
lineSuffix([" ", join(" ", node.trailingComments)]),
|
|
85
|
+
breakParent
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return parts;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export {
|
|
92
|
+
printSingleTwigTag,
|
|
93
|
+
printComments,
|
|
94
|
+
hasLeadingComments,
|
|
95
|
+
hasTrailingComments,
|
|
96
|
+
hasComments
|
|
97
|
+
};
|