Skip to content

Commit

Permalink
Formatting valid json content is causing an invalid json. Fixes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Nov 13, 2020
1 parent c75fa3c commit 79105e5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/impl/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

import { Range, FormattingOptions, Edit, SyntaxKind, ScanError} from '../main';
import { Range, FormattingOptions, Edit, SyntaxKind, ScanError } from '../main';
import { createScanner } from './scanner';

export function format(documentText: string, range: Range | undefined, options: FormattingOptions): Edit[] {
Expand Down Expand Up @@ -81,12 +81,14 @@ export function format(documentText: string, range: Range | undefined, options:
let secondToken = scanNext();

let replaceContent = '';
let needsLineBreak = false;
while (!lineBreak && (secondToken === SyntaxKind.LineCommentTrivia || secondToken === SyntaxKind.BlockCommentTrivia)) {
// comments on the same line: keep them on the same line, but ignore them otherwise
let commentTokenStart = scanner.getTokenOffset() + formatTextStart;
addEdit(' ', firstTokenEnd, commentTokenStart);
firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart;
replaceContent = secondToken === SyntaxKind.LineCommentTrivia ? newLineAndIndent() : '';
needsLineBreak = secondToken === SyntaxKind.LineCommentTrivia;
replaceContent = needsLineBreak ? newLineAndIndent() : '';
secondToken = scanNext();
}

Expand Down Expand Up @@ -114,17 +116,21 @@ export function format(documentText: string, range: Range | undefined, options:
case SyntaxKind.BlockCommentTrivia:
if (lineBreak) {
replaceContent = newLineAndIndent();
} else {
} else if (!needsLineBreak) {
// symbol following comment on the same line: keep on same line, separate with ' '
replaceContent = ' ';
}
break;
case SyntaxKind.ColonToken:
replaceContent = ' ';
if (!needsLineBreak) {
replaceContent = ' ';
}
break;
case SyntaxKind.StringLiteral:
if (secondToken === SyntaxKind.ColonToken) {
replaceContent = '';
if (!needsLineBreak) {
replaceContent = '';
}
break;
}
// fall through
Expand All @@ -135,7 +141,9 @@ export function format(documentText: string, range: Range | undefined, options:
case SyntaxKind.CloseBraceToken:
case SyntaxKind.CloseBracketToken:
if (secondToken === SyntaxKind.LineCommentTrivia || secondToken === SyntaxKind.BlockCommentTrivia) {
replaceContent = ' ';
if (!needsLineBreak) {
replaceContent = ' ';
}
} else if (secondToken !== SyntaxKind.CommaToken && secondToken !== SyntaxKind.EOF) {
hasError = true;
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,28 @@ suite('JSON - formatter', () => {

format(content, expected);
});

test('line comment bug 33 ', () => {
var content = [
'{"settings": // This is some text',
'{',
'"foo": 1',
'}',
'}'
].join('\n');

var expected = [
'{',
' "settings": // This is some text',
' {',
' "foo": 1',
' }',
'}'
].join('\n');

format(content, expected);
});

test('random content', () => {
var content = [
'a 1 b 1 3 true'
Expand Down

0 comments on commit 79105e5

Please sign in to comment.