Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions rewrite-javascript/rewrite/src/javascript/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import {JS} from "./tree";
import {JavaScriptVisitor} from "./visitor";
import {Comment, emptySpace, J, Statement} from "../java";
import {Comment, J, Statement} from "../java";
import {Draft, produce} from "immer";
import {Cursor, isScope, Tree} from "../tree";
import {
Expand Down Expand Up @@ -445,6 +445,10 @@ export class SpacesVisitor<P> extends JavaScriptVisitor<P> {
}
protected async visitVariable(variable: J.VariableDeclarations.NamedVariable, p: P): Promise<J | undefined> {
const ret = await super.visitVariable(variable, p) as J.VariableDeclarations.NamedVariable;
if (variable.initializer?.element?.kind == JS.Kind.StatementExpression
&& (variable.initializer.element as JS.StatementExpression).statement.kind == J.Kind.MethodDeclaration) {
return ret;
}
return produceAsync(ret, async draft => {
if (draft.initializer) {
draft.initializer.before.whitespace = this.style.aroundOperators.assignment ? " " : "";
Expand Down Expand Up @@ -968,7 +972,11 @@ export class BlankLinesVisitor<P> extends JavaScriptVisitor<P> {
});
return super.visit(cu, p, cursor);
}
if (tree.kind === J.Kind.MethodDeclaration) {
if (tree.kind === JS.Kind.StatementExpression && (tree as JS.StatementExpression).statement.kind == J.Kind.MethodDeclaration) {
tree = produce(tree as JS.StatementExpression, draft => {
this.ensurePrefixHasNewLine(draft);
});
} else if (tree.kind === J.Kind.MethodDeclaration && this.cursor.value.kind != JS.Kind.StatementExpression) {
tree = produce(tree as J.MethodDeclaration, draft => {
this.ensurePrefixHasNewLine(draft);
});
Expand Down Expand Up @@ -1149,7 +1157,7 @@ export class TabsAndIndentsVisitor<P> extends JavaScriptVisitor<P> {
let indentShouldIncrease =
tree.kind === J.Kind.Block
|| this.cursor.parent?.parent?.parent?.value.kind == J.Kind.Case
|| (tree.kind === J.Kind.MethodDeclaration && this.cursor.parent?.value.kind === JS.Kind.StatementExpression);
|| (tree.kind === JS.Kind.StatementExpression && (tree as JS.StatementExpression).statement.kind == J.Kind.MethodDeclaration);

const previousIndent = this.currentIndent;

Expand Down
7 changes: 5 additions & 2 deletions rewrite-javascript/rewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3131,12 +3131,15 @@ export class JavaScriptParserVisitor {
}

visitFunctionExpression(node: ts.FunctionExpression): JS.StatementExpression {
const delegate = this.mapFunctionDeclaration(node);
return {
kind: JS.Kind.StatementExpression,
id: randomId(),
prefix: emptySpace,
prefix: delegate.prefix,
markers: emptyMarkers,
statement: this.mapFunctionDeclaration(node)
statement: produce(delegate, draft => {
draft.prefix = emptySpace;
})
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,8 @@ describe('AutoformatVisitor', () => {
//language=typescript
typescript(
`const fn = function () {return 99;};`,
// TODO the space after `const fn=` is excessive
`
const fn =
const fn =
function () {
return 99;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,23 @@ describe('variable declaration mapping', () => {
}
`)
));

test.each([
"const c = function(): number { return 116; };",
"const c = 136;",
"const c = (1 > 0) ? 116 : 119;"
])('double space: %s', (code) =>
spec.rewriteRun({
//language=javascript
...typescript(code),
afterRecipe: (cu: JS.CompilationUnit) => {
expect(cu.statements).toHaveLength(1);
cu.statements.forEach(statement => {
const varDecl = statement.element as J.VariableDeclarations;
const initializer = varDecl.variables[0].element.initializer!;
expect(initializer.before.whitespace).toBe(" ");
expect(initializer.element.prefix.whitespace).toBe(" ");
});
}
}));
});