Skip to content

Commit

Permalink
Fix crash when .. syntax encountered in Transact-SQL
Browse files Browse the repository at this point in the history
Fixes #700
  • Loading branch information
nene committed Jan 21, 2024
1 parent 9c22894 commit e382659
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/formatter/ExpressionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export default class ExpressionFormatter {

private formatPropertyAccess(node: PropertyAccessNode) {
this.formatNode(node.object);
this.layout.add(WS.NO_SPACE, '.');
this.layout.add(WS.NO_SPACE, node.operator);
this.formatNode(node.property);
}

Expand Down
2 changes: 1 addition & 1 deletion src/lexer/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default class Tokenizer {
]),
},
{ type: TokenType.ASTERISK, regex: /[*]/uy },
{ type: TokenType.DOT, regex: /[.]/uy },
{ type: TokenType.DOT, regex: /[.]+/uy },
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/lexer/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export enum TokenType {
OPERATOR = 'OPERATOR',
COMMA = 'COMMA',
ASTERISK = 'ASTERISK', // *
DOT = 'DOT', // .
DOT = 'DOT', // . (or .. in Transact-SQL)
OPEN_PAREN = 'OPEN_PAREN',
CLOSE_PAREN = 'CLOSE_PAREN',
LINE_COMMENT = 'LINE_COMMENT',
Expand Down
1 change: 1 addition & 0 deletions src/parser/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export interface LiteralNode extends BaseNode {
export interface PropertyAccessNode extends BaseNode {
type: NodeType.property_access;
object: AstNode;
operator: string;
property: IdentifierNode | ArraySubscriptNode | AllColumnsAsteriskNode;
}

Expand Down
1 change: 1 addition & 0 deletions src/parser/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ property_access -> atomic_expression _ %DOT _ (identifier | array_subscript | al
return {
type: NodeType.property_access,
object: addComments(object, { trailing: _1 }),
operator: dot.text,
property: addComments(property, { leading: _2 }),
};
}
Expand Down
9 changes: 9 additions & 0 deletions test/transactsql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ describe('TransactSqlFormatter', () => {
`);
});

it('formats .. shorthand for database.schema.table', () => {
expect(format('SELECT x FROM db..tbl')).toBe(dedent`
SELECT
x
FROM
db..tbl
`);
});

it('formats ALTER TABLE ... ALTER COLUMN', () => {
expect(format(`ALTER TABLE t ALTER COLUMN foo INT NOT NULL DEFAULT 5;`)).toBe(dedent`
ALTER TABLE t
Expand Down
3 changes: 3 additions & 0 deletions test/unit/__snapshots__/Parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ Array [
"text": "ident",
"type": "identifier",
},
"operator": ".",
"property": Object {
"type": "all_columns_asterisk",
},
Expand Down Expand Up @@ -642,13 +643,15 @@ Array [
"text": "foo",
"type": "identifier",
},
"operator": ".",
"property": Object {
"quoted": false,
"text": "bar",
"type": "identifier",
},
"type": "property_access",
},
"operator": ".",
"property": Object {
"quoted": false,
"text": "baz",
Expand Down

0 comments on commit e382659

Please sign in to comment.