Skip to content

Commit 7f89b9c

Browse files
committed
JavaScript: More formatter fixes
1 parent af1000e commit 7f89b9c

8 files changed

Lines changed: 529 additions & 76 deletions

File tree

rewrite-javascript/rewrite/src/java/formatting-utils.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,56 @@ export function replaceIndentAfterLastNewline(ws: string, newIndent: string): st
6464
return ws.substring(0, lastNewline + 1) + newIndent;
6565
}
6666

67+
/**
68+
* Checks if a Space contains any newlines (in whitespace or comment suffixes).
69+
*/
70+
export function spaceContainsNewline(space: J.Space | undefined): boolean {
71+
if (!space) return false;
72+
if (space.whitespace.includes("\n")) return true;
73+
return space.comments.some(c => c.suffix.includes("\n"));
74+
}
75+
76+
/**
77+
* Normalizes indentation in an entire Space, including both whitespace and comment suffixes.
78+
* Each newline followed by whitespace gets its indentation normalized to the target indent.
79+
*
80+
* @param space The Space to normalize
81+
* @param targetIndent The indentation to use after newlines
82+
* @returns The normalized Space, or the original if unchanged
83+
*/
84+
export function normalizeSpaceIndent(space: J.Space, targetIndent: string): J.Space {
85+
let changed = false;
86+
87+
// Normalize whitespace
88+
let newWhitespace = space.whitespace;
89+
if (space.whitespace.includes("\n")) {
90+
newWhitespace = replaceIndentAfterLastNewline(space.whitespace, targetIndent);
91+
changed = changed || newWhitespace !== space.whitespace;
92+
}
93+
94+
// Normalize comment suffixes
95+
const newComments = space.comments.map(comment => {
96+
if (comment.suffix.includes("\n")) {
97+
const newSuffix = replaceIndentAfterLastNewline(comment.suffix, targetIndent);
98+
if (newSuffix !== comment.suffix) {
99+
changed = true;
100+
return {...comment, suffix: newSuffix};
101+
}
102+
}
103+
return comment;
104+
});
105+
106+
if (!changed) {
107+
return space;
108+
}
109+
110+
return {
111+
...space,
112+
whitespace: newWhitespace,
113+
comments: newComments
114+
};
115+
}
116+
67117
/**
68118
* Handles element removal from lists while preserving LST formatting.
69119
* Automatically applies prefixes from removed elements to the next kept element,

rewrite-javascript/rewrite/src/javascript/format.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,17 @@ export class SpacesVisitor<P> extends JavaScriptVisitor<P> {
311311
const ret = await super.visitImportDeclaration(jsImport, p) as JS.Import;
312312
return produce(ret, draft => {
313313
if (draft.importClause) {
314-
draft.importClause.prefix.whitespace = draft.importClause.namedBindings && !draft.importClause.typeOnly ? "" : " ";
314+
// Space after 'import' keyword:
315+
// - If there's a default import (name), space goes in importClause.prefix
316+
// - If typeOnly (import type ...), space goes in importClause.prefix (before 'type')
317+
// - If only namedBindings (no default, no type), space goes in namedBindings.prefix (importClause.prefix is empty)
318+
const hasDefaultImport = !!draft.importClause.name;
319+
draft.importClause.prefix.whitespace = (hasDefaultImport || draft.importClause.typeOnly) ? " " : "";
315320
if (draft.importClause.name) {
316321
draft.importClause.name.after.whitespace = "";
317322
}
318323
if (draft.importClause.namedBindings) {
324+
// Space before namedBindings - always needed
319325
draft.importClause.namedBindings.prefix.whitespace = " ";
320326
if (draft.importClause.namedBindings.kind == JS.Kind.NamedImports) {
321327
const ni = draft.importClause.namedBindings as Draft<JS.NamedImports>;

rewrite-javascript/rewrite/src/javascript/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3474,7 +3474,7 @@ export class JavaScriptParserVisitor {
34743474
statements: node.clauses.map(clause =>
34753475
this.rightPadded(
34763476
this.visit(clause),
3477-
this.suffix(clause)
3477+
emptySpace
34783478
)),
34793479
end: end
34803480
}

0 commit comments

Comments
 (0)