You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/printer.ts
+50-8Lines changed: 50 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -1823,8 +1823,7 @@ export class PugPrinter {
1823
1823
returnval.slice(0,-1);
1824
1824
}
1825
1825
1826
-
// Since every line is parsed independently, babel will throw a SyntaxError if the line of code is only valid when there is another statement after it. This is a hack to get babel to properly parse what would otherwise be an invalid standalone JS line (e.g., `if (foo)`)
1827
-
privateasyncformatRawCodeWithFallback(
1826
+
privateasyncformatRawCodeWithFallbackNoElse(
1828
1827
val: string,
1829
1828
useSemi: boolean,
1830
1829
): Promise<string>{
@@ -1844,14 +1843,38 @@ export class PugPrinter {
1844
1843
// At this point, we know the SyntaxError is from the fact that there's no statement after our val's statement, implying we likely need a block after it. Using an empty block to get babel to parse it without affecting the code semantics.
1845
1844
// Example: `if (foo)` is not valid JS on its own, but `if (foo) {}` is.
1846
1845
try{
1847
-
val=awaitthis.formatRawCode(val+'{}',useSemi);
1846
+
// Look for comments at the end of the line, since we have to insert the block in between the statement and the comment or else the block will potentially be commented out
Strip out the empty block, which prettier has now formatted to split across two lines, and if there was a comment, it's now at the end of the second line. The first \s is to account for the space prettier inserted between the statement and the empty block.
1858
+
Input:
1859
+
`if (foo) // comment`
1848
1860
1849
-
// Dynamically find the index of the last `{` in the code, which is the start of the empty block, since it's been reformatted at this point and a newline has likely been added between (shouldn't rely on that behavior though)
1850
-
// Also account for any newly-introduced whitespace right before the empty block
logger.debug('[PugPrinter] fallback format error',secondError);
1857
1880
// throw original error since our fallback didn't work
@@ -1860,6 +1883,25 @@ export class PugPrinter {
1860
1883
}
1861
1884
}
1862
1885
1886
+
// Since every line is parsed independently, babel will throw a SyntaxError if the line of code is only valid when there is another statement after it, or if the line starts with `else if` or `else`. This is a hack to get babel to properly parse what would otherwise be an invalid standalone JS line (e.g., `if (foo)`, `else if (bar)`, `else`)
1887
+
privateasyncformatRawCodeWithFallback(
1888
+
val: string,
1889
+
useSemi: boolean,
1890
+
): Promise<string>{
1891
+
if(val.startsWith('else')){
1892
+
// If the code starts with `else`, then we can format the code without the `else` keyword, and then add it back onto the start.
1893
+
// We can call the same helper function in each case, just with different inputs, so we can easily handle all `if`, `else if`, and `else` cases without having to write out each one.
0 commit comments