Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/formatter/Layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ export default class Layout {
if (item.startsWith('-') && this.lastItemEndsWith('-')) {
this.items.push(WS.SPACE);
}
// Don't glue a "." onto a bare integer literal: "1." re-lexes as a number
// and absorbs the property-access operator, so "1 . x" would collapse to
// "1.x" and re-parse as a different expression. Identifiers that merely end
// in a digit ("t1.x") are unaffected.
if (item.startsWith('.') && this.lastItemIsIntegerLiteral()) {
this.items.push(WS.SPACE);
}
this.items.push(item);
}
}
Expand All @@ -73,6 +80,11 @@ export default class Layout {
return typeof lastItem === 'string' && lastItem.endsWith(suffix);
}

private lastItemIsIntegerLiteral(): boolean {
const lastItem = last(this.items);
return typeof lastItem === 'string' && /^[0-9]+$/.test(lastItem);
}

private trimHorizontalWhitespace() {
while (isHorizontalWhitespace(last(this.items))) {
this.items.pop();
Expand Down
12 changes: 12 additions & 0 deletions test/mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ describe('MySqlFormatter', () => {
`);
});

it('keeps a numeric property access idempotent', () => {
// "1 ." must not glue into "1." which re-lexes as a number literal and
// swallows the property-access operator.
const sql = 'SELECT 1 . /*x*/ 5e';
const result = dedent`
SELECT
1 ./*x*/ 5e
`;
expect(format(sql)).toBe(result);
expect(format(result)).toBe(result);
});

it('formats ALTER TABLE ... ALTER COLUMN', () => {
expect(
format(
Expand Down