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
9 changes: 4 additions & 5 deletions rewrite-javascript/rewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2637,11 +2637,10 @@ export class JavaScriptParserVisitor {
}

visitVariableStatement(node: ts.VariableStatement): JS.ScopedVariableDeclarations | J.VariableDeclarations {
const prefix = this.prefix(node);
return produce(this.visitVariableDeclarationList(node.declarationList), draft => {
if (node.modifiers) {
draft.modifiers = this.mapModifiers(node).concat(draft.modifiers);
}
draft.prefix = this.prefix(node);
draft.prefix = prefix;
draft.modifiers = this.mapModifiers(node).concat(draft.modifiers);
});
}

Expand Down Expand Up @@ -3064,7 +3063,7 @@ export class JavaScriptParserVisitor {
modifiers.push({
kind: J.Kind.Modifier,
id: randomId(),
prefix: this.prefix(kind),
prefix: modifiers.length === 0 ? this.prefix(kind) : this.prefix(kind),
markers: emptyMarkers,
annotations: [],
keyword: kind.kind === ts.SyntaxKind.VarKeyword ? 'var' :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ describe('AutoformatVisitor', () => {
if (1 > 0) {
console.log("four", "three", "six");
}

let i = 1;
while (i < 4) {
i++;
Expand All @@ -112,7 +111,6 @@ describe('AutoformatVisitor', () => {
} finally {
console.log("finally");
}

const isTypeScriptFun = i > 3 ? "yes" : "hell yeah!";
for (let j = 1; j <= 5; j++) {
console.log(\`Number: \` + j);
Expand All @@ -138,7 +136,6 @@ describe('AutoformatVisitor', () => {
)});

test('a statement following an if', () => {
// TODO not sure if there should be a newline after the if
return spec.rewriteRun(
// @formatter:off
//language=typescript
Expand All @@ -150,7 +147,6 @@ describe('AutoformatVisitor', () => {
`
if (1 > 0) {
}

let i = 1;
`)
// @formatter:on
Expand Down
43 changes: 43 additions & 0 deletions rewrite-javascript/rewrite/test/javascript/remove-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,5 +959,48 @@ describe('RemoveImport visitor', () => {
)
);
});

test('should remove leading empty lines with variable declaration', async () => {
const spec = new RecipeSpec();
spec.recipe = fromVisitor(new RemoveImport("fs", "readFile"));

//language=typescript
await spec.rewriteRun(
typescript(
`
import {readFile} from "fs";

const foo = 1;
console.log(foo);
`,
`
const foo = 1;
console.log(foo);`
)
);
});

test('should remove leading empty lines with function declaration', async () => {
const spec = new RecipeSpec();
spec.recipe = fromVisitor(new RemoveImport("fs", "readFile"));

//language=typescript
await spec.rewriteRun(
typescript(
`
import {readFile} from "fs";

function foo() {
return 42;
}
`,
`
function foo() {
return 42;
}
`
)
);
});
});
});