Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow trailing semi-colon after typedef #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions src/main/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ export function createParser(

const annotations: Annotations | undefined = parseAnnotations()

readListSeparator()

return {
type: SyntaxType.TypedefDefinition,
name: createIdentifier(nameToken.text, nameToken.loc),
Expand Down
2 changes: 2 additions & 0 deletions src/tests/parser/fixtures/trailing-semi-colon.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const string test = 'asdf';
typedef i32 MyInteger;
13 changes: 13 additions & 0 deletions src/tests/parser/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ describe('Parser', () => {
assert.deepEqual(objectify(thrift), expected)
})

it('should correctly parse the consts and typedefs with trailing-comma', () => {
const content: string = loadSource('trailing-semi-colon')
const scanner: Scanner = createScanner(content)
const tokens: Array<Token> = scanner.scan()

const parser: Parser = createParser(tokens)
const thrift: ThriftDocument = parser.parse()

const expected: any = loadSolution('trailing-semi-colon')

assert.deepEqual(objectify(thrift), expected)
})

it('should correctly parse the syntax of an exception', () => {
const content: string = loadSource('exception')
const scanner: Scanner = createScanner(content)
Expand Down
115 changes: 115 additions & 0 deletions src/tests/parser/solutions/trailing-semi-colon.solution.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"type": "ThriftDocument",
"body": [
{
"type": "ConstDefinition",
"name": {
"type": "Identifier",
"value": "test",
"loc": {
"start": {
"line": 1,
"column": 14,
"index": 13
},
"end": {
"line": 1,
"column": 18,
"index": 17
}
}
},
"fieldType": {
"type": "StringKeyword",
"loc": {
"start": {
"line": 1,
"column": 7,
"index": 6
},
"end": {
"line": 1,
"column": 13,
"index": 12
}
}
},
"initializer": {
"type": "StringLiteral",
"value": "asdf",
"loc": {
"start": {
"line": 1,
"column": 21,
"index": 20
},
"end": {
"line": 1,
"column": 27,
"index": 26
}
}
},
"comments": [],
"loc": {
"start": {
"line": 1,
"column": 1,
"index": 0
},
"end": {
"line": 1,
"column": 27,
"index": 26
}
}
},
{
"type": "TypedefDefinition",
"name": {
"type": "Identifier",
"value": "MyInteger",
"loc": {
"start": {
"line": 2,
"column": 13,
"index": 40
},
"end": {
"line": 2,
"column": 22,
"index": 49
}
}
},
"definitionType": {
"type": "I32Keyword",
"loc": {
"start": {
"line": 2,
"column": 9,
"index": 36
},
"end": {
"line": 2,
"column": 12,
"index": 39
}
}
},
"comments": [],
"loc": {
"start": {
"line": 2,
"column": 1,
"index": 28
},
"end": {
"line": 2,
"column": 22,
"index": 49
}
}
}
]
}
4 changes: 4 additions & 0 deletions src/tests/scanner/fixtures/trailing-semi-colon.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const string test = 'asdf';
typedef i32 MyInteger;
Comment on lines +1 to +2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Duplicate const declaration on line 3

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Duplicate typedef declaration on line 4

const string test = 'asdf'
typedef i32 MyInteger
Comment on lines +3 to +4
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Inconsistent use of semicolons

10 changes: 10 additions & 0 deletions src/tests/scanner/scanner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ describe('Scanner', () => {
assert.deepEqual(tokens, expected)
})

it('should allow trailing semicolon in const and typedef', () => {
const content = loadSource('trailing-semi-colon')
const scanner: Scanner = createScanner(content)
const tokens: Array<Token> = scanner.scan()

const expected: any = loadSolution('trailing-semi-colon')

assert.deepEqual(tokens, expected)
})

it('should correctly handle assignment to a negative number', () => {
const content = loadSource('const-number')
const scanner: Scanner = createScanner(content)
Expand Down
Loading