Skip to content

Commit 51f1014

Browse files
committed
refactor: don't use alias to export print function
The modular print function has signature ```js const p = (node, path, print, options) => { // function body here } ``` It is easy to recognise which is the main function responsibe to build the doc. Unfortunately it has to be aliased when exported to prevent name collision. And it is not obvious where the callstack come from when debugging since it share the same name across multiple file/module.
1 parent f5185be commit 51f1014

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+97
-94
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## unreleased
44

5+
### Internals
6+
- DX: Don't use alias to export print function to make it easier to identify when debugging call stack
7+
58
---
69
## 0.14.0 (2024-12-18)
710

src/print/AliasExpression.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const p = (node, path, print) => {
1+
const printAliasExpression = (node, path, print) => {
22
return [path.call(print, "name"), " as ", path.call(print, "alias")];
33
};
44

5-
export { p as printAliasExpression };
5+
export { printAliasExpression };

src/print/ArrayExpression.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { STRING_NEEDS_QUOTES } from "../util/index.js";
33

44
const { group, softline, line, indent, join } = doc.builders;
55

6-
const p = (node, path, print) => {
6+
const printArrayExpression = (node, path, print) => {
77
node[STRING_NEEDS_QUOTES] = true;
88
const mappedElements = path.map(print, "elements");
99
const indentedContent = [softline, join([",", line], mappedElements)];
1010

1111
return group(["[", indent(indentedContent), softline, "]"]);
1212
};
1313

14-
export { p as printArrayExpression };
14+
export { printArrayExpression };

src/print/ArrowFunction.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { doc } from "prettier";
22
const { group, indent, join, line, softline } = doc.builders;
33

4-
const p = (node, path, print) => {
4+
const printArrowFunction = (node, path, print) => {
55
const args = node.args;
66
const body = path.call(print, "body");
77

@@ -34,4 +34,4 @@ const p = (node, path, print) => {
3434
return group(parts);
3535
};
3636

37-
export { p as printArrowFunction };
37+
export { printArrowFunction };

src/print/Attribute.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const printConcatenatedString = (valueNode, path, print, ...initialPath) => {
1919
return printedFragments;
2020
};
2121

22-
const p = (node, path, print = print) => {
22+
const printAttribute = (node, path, print = print) => {
2323
node[EXPRESSION_NEEDED] = false;
2424
const docs = [path.call(print, "name")];
2525
node[EXPRESSION_NEEDED] = true;
@@ -47,4 +47,4 @@ const p = (node, path, print = print) => {
4747
return docs;
4848
};
4949

50-
export { p as printAttribute };
50+
export { printAttribute };

src/print/AutoescapeBlock.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const createOpener = (node, options) => {
2525
];
2626
};
2727

28-
const p = (node, path, print, options) => {
28+
const printAutoescapeBlock = (node, path, print, options) => {
2929
const parts = [createOpener(node, options)];
3030
parts.push(printChildBlock(node, path, print, "expressions"));
3131
parts.push(
@@ -38,4 +38,4 @@ const p = (node, path, print, options) => {
3838
return parts;
3939
};
4040

41-
export { p as printAutoescapeBlock };
41+
export { printAutoescapeBlock };

src/print/BinaryExpression.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const otherNeedsParentheses = (node, otherProp) => {
6565
);
6666
};
6767

68-
const printBinaryExpression = (node, path, print) => {
68+
const internalPrintBinaryExpression = (node, path, print) => {
6969
node[EXPRESSION_NEEDED] = false;
7070
node[STRING_NEEDS_QUOTES] = true;
7171

@@ -145,11 +145,11 @@ const printBinaryExpression = (node, path, print) => {
145145
return shouldGroupResult ? group(result) : result;
146146
};
147147

148-
const p = (node, path, print, options) => {
148+
const printBinaryExpression = (node, path, print, options) => {
149149
if (Node.isBinaryConcatExpression(node) && node.wasImplicitConcatenation) {
150150
return printInterpolatedString(node, path, print, options);
151151
}
152-
return printBinaryExpression(node, path, print);
152+
return internalPrintBinaryExpression(node, path, print);
153153
};
154154

155-
export { p as printBinaryExpression };
155+
export { printBinaryExpression };

src/print/BlockStatement.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88

99
const { hardline, group } = doc.builders;
1010

11-
const p = (node, path, print, options) => {
11+
const printBlockStatement = (node, path, print, options) => {
1212
node[EXPRESSION_NEEDED] = false;
1313
const hasChildren = Array.isArray(node.body);
1414
const printEndblockName = options.twigOutputEndblockName === true;
@@ -55,4 +55,4 @@ const p = (node, path, print, options) => {
5555
}
5656
};
5757

58-
export { p as printBlockStatement };
58+
export { printBlockStatement };

src/print/CallExpression.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88

99
const { group, softline, line, indent, join } = doc.builders;
1010

11-
const p = (node, path, print) => {
11+
const printCallExpression = (node, path, print) => {
1212
node[EXPRESSION_NEEDED] = false;
1313
node[STRING_NEEDS_QUOTES] = true;
1414
const mappedArguments = path.map(print, "arguments");
@@ -35,4 +35,4 @@ const p = (node, path, print) => {
3535
return group(parts);
3636
};
3737

38-
export { p as printCallExpression };
38+
export { printCallExpression };

src/print/ConditionalExpression.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77

88
const { line, indent, group } = doc.builders;
99

10-
const p = (node, path, print) => {
10+
const printConditionalExpression = (node, path, print) => {
1111
node[EXPRESSION_NEEDED] = false;
1212
node[STRING_NEEDS_QUOTES] = true;
1313

@@ -24,4 +24,4 @@ const p = (node, path, print) => {
2424
return group(parts);
2525
};
2626

27-
export { p as printConditionalExpression };
27+
export { printConditionalExpression };

src/print/Declaration.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { STRING_NEEDS_QUOTES, OVERRIDE_QUOTE_CHAR } from "../util/index.js";
33

44
const { fill, join } = doc.builders;
55

6-
const p = (node, path, print) => {
6+
const printDeclaration = (node, path, print) => {
77
node[STRING_NEEDS_QUOTES] = true;
88
node[OVERRIDE_QUOTE_CHAR] = '"';
99
const start = "<!" + (node.declarationType || "").toUpperCase();
@@ -12,4 +12,4 @@ const p = (node, path, print) => {
1212
return fill([start, " ", join(" ", printedParts), ">"]);
1313
};
1414

15-
export { p as printDeclaration };
15+
export { printDeclaration };

src/print/DoStatement.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const p = (node, path, print) => {
1+
const printDoStatement = (node, path, print) => {
22
return [
33
node.trimLeft ? "{%-" : "{%",
44
" do ",
@@ -7,4 +7,4 @@ const p = (node, path, print) => {
77
];
88
};
99

10-
export { p as printDoStatement };
10+
export { printDoStatement };

src/print/Element.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const printSeparatedList = (path, print, separator, attrName) => {
4646
return join([separator, line], path.map(print, attrName));
4747
};
4848

49-
const p = (node, path, print, options) => {
49+
const printElement = (node, path, print, options) => {
5050
// Set a flag in case attributes contain, e.g., a FilterExpression
5151
node[EXPRESSION_NEEDED] = true;
5252
const openingGroup = group(printOpeningTag(node, path, print, options));
@@ -83,4 +83,4 @@ const p = (node, path, print, options) => {
8383
return group(result, { id: groupElement });
8484
};
8585

86-
export { p as printElement };
86+
export { printElement };

src/print/EmbedStatement.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const printOpener = (node, path, print) => {
2222
return group(parts);
2323
};
2424

25-
const p = (node, path, print) => {
25+
const printEmbedStatement = (node, path, print) => {
2626
const children = printChildBlock(node, path, print, "blocks");
2727
const printedOpener = printOpener(node, path, print);
2828
const closing = [
@@ -35,4 +35,4 @@ const p = (node, path, print) => {
3535
return [printedOpener, children, closing];
3636
};
3737

38-
export { p as printEmbedStatement };
38+
export { printEmbedStatement };

src/print/ExpressionStatement.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88

99
const { group, indent, line } = doc.builders;
1010

11-
const p = (node, path, print) => {
11+
const printExpressionStatement = (node, path, print) => {
1212
node[EXPRESSION_NEEDED] = false;
1313
node[STRING_NEEDS_QUOTES] = true;
1414
const opener = node.trimLeft ? "{{-" : "{{";
@@ -22,4 +22,4 @@ const p = (node, path, print) => {
2222
return group([opener, value, padding, closing]);
2323
};
2424

25-
export { p as printExpressionStatement };
25+
export { printExpressionStatement };

src/print/ExtendsStatement.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { STRING_NEEDS_QUOTES } from "../util/index.js";
22

3-
const p = (node, path, print) => {
3+
const printExtendsStatement = (node, path, print) => {
44
node[STRING_NEEDS_QUOTES] = true;
55
return [
66
node.trimLeft ? "{%-" : "{%",
@@ -10,4 +10,4 @@ const p = (node, path, print) => {
1010
];
1111
};
1212

13-
export { p as printExtendsStatement };
13+
export { printExtendsStatement };

src/print/FilterBlockStatement.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const printOpeningGroup = (node, path, print) => {
1010
return group(parts);
1111
};
1212

13-
const p = (node, path, print) => {
13+
const printFilterBlockStatement = (node, path, print) => {
1414
node[FILTER_BLOCK] = true;
1515
const openingGroup = printOpeningGroup(node, path, print);
1616
const body = printChildBlock(node, path, print, "body");
@@ -24,4 +24,4 @@ const p = (node, path, print) => {
2424
return [openingGroup, body, closingStatement];
2525
};
2626

27-
export { p as printFilterBlockStatement };
27+
export { printFilterBlockStatement };

src/print/FilterExpression.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const joinFilters = (filterExpressions, space = "") => {
5353
);
5454
};
5555

56-
const p = (node, path, print, options) => {
56+
const printFilterExpression = (node, path, print, options) => {
5757
let currentNode = node;
5858
node[EXPRESSION_NEEDED] = false;
5959
node[STRING_NEEDS_QUOTES] = true;
@@ -128,4 +128,4 @@ const p = (node, path, print, options) => {
128128
return group(parts);
129129
};
130130

131-
export { p as printFilterExpression };
131+
export { printFilterExpression };

src/print/FlushStatement.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const p = (node, path, print) => {
1+
const printFlushStatement = (node, path, print) => {
22
const dashLeft = node.trimLeft ? "-" : "";
33
const dashRight = node.trimRight ? "-" : "";
44
return `{%${dashLeft} flush ${dashRight}%}`;
55
};
66

7-
export { p as printFlushStatement };
7+
export { printFlushStatement };

src/print/ForStatement.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const printFor = (node, path, print) => {
2424
return group(parts);
2525
};
2626

27-
const p = (node, path, print) => {
27+
const printForStatement = (node, path, print) => {
2828
node[EXPRESSION_NEEDED] = false;
2929
const parts = [printFor(node, path, print)];
3030
const isBodyEmpty =
@@ -55,4 +55,4 @@ const p = (node, path, print) => {
5555
return parts;
5656
};
5757

58-
export { p as printForStatement };
58+
export { printForStatement };

src/print/FromStatement.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const printImportDeclaration = node => {
1111
return parts;
1212
};
1313

14-
const p = (node, path, print) => {
14+
const printFromStatement = (node, path, print) => {
1515
node[STRING_NEEDS_QUOTES] = true;
1616
// Unfortunately, ImportDeclaration has different
1717
// formatting needs here compared to when used
@@ -29,4 +29,4 @@ const p = (node, path, print) => {
2929
]);
3030
};
3131

32-
export { p as printFromStatement };
32+
export { printFromStatement };

src/print/GenericToken.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const p = (node, path, print) => {
1+
const printGenericToken = (node, path, print) => {
22
return node.tokenText;
33
};
44

5-
export { p as printGenericToken };
5+
export { printGenericToken };

src/print/GenericTwigTag.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99

1010
const { hardline } = doc.builders;
1111

12-
const p = (node, path, print) => {
12+
const printGenericTwigTag = (node, path, print) => {
1313
node[STRING_NEEDS_QUOTES] = true;
1414
const openingTag = printSingleTwigTag(node, path, print);
1515
const parts = [openingTag];
@@ -27,4 +27,4 @@ const p = (node, path, print) => {
2727
return parts;
2828
};
2929

30-
export { p as printGenericTwigTag };
30+
export { printGenericTwigTag };

src/print/HtmlComment.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88

99
const { join, indent, hardline } = doc.builders;
1010

11-
const p = (node, path, print) => {
11+
const printHtmlComment = (node, path, print) => {
1212
const commentText = stripHtmlCommentChars(node.value.value || "");
1313

1414
const numNewlines = countNewlines(commentText);
@@ -19,4 +19,4 @@ const p = (node, path, print) => {
1919
return ["<!-- ", commentText, " -->"];
2020
};
2121

22-
export { p as printHtmlComment };
22+
export { printHtmlComment };

src/print/Identifier.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77

88
const { group } = doc.builders;
99

10-
const p = (node, path, print) => {
10+
const printIdentifier = (node, path, print) => {
1111
node[EXPRESSION_NEEDED] = false;
1212
node[STRING_NEEDS_QUOTES] = true;
1313

@@ -21,4 +21,4 @@ const p = (node, path, print) => {
2121
return parts.length === 1 ? result : group(result);
2222
};
2323

24-
export { p as printIdentifier };
24+
export { printIdentifier };

src/print/IfStatement.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const { group, indent, line, hardline } = doc.builders;
1212

1313
const IS_ELSEIF = Symbol("IS_ELSEIF");
1414

15-
const p = (node, path, print) => {
15+
const printIfStatement = (node, path, print) => {
1616
node[EXPRESSION_NEEDED] = false;
1717
const hasElseBranch =
1818
Array.isArray(node.alternate) && node.alternate.length > 0;
@@ -78,4 +78,4 @@ const p = (node, path, print) => {
7878
return parts;
7979
};
8080

81-
export { p as printIfStatement };
81+
export { printIfStatement };

src/print/ImportDeclaration.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { STRING_NEEDS_QUOTES } from "../util/index.js";
33

44
const { group, line, indent } = doc.builders;
55

6-
const p = (node, path, print) => {
6+
const printImportDeclaration = (node, path, print) => {
77
node[STRING_NEEDS_QUOTES] = true;
88
return group([
99
node.trimLeft ? "{%-" : "{%",
@@ -15,4 +15,4 @@ const p = (node, path, print) => {
1515
]);
1616
};
1717

18-
export { p as printImportDeclaration };
18+
export { printImportDeclaration };

0 commit comments

Comments
 (0)