Skip to content

Commit

Permalink
Add parser support for async and generator funcs, fixes #90
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffijoe committed May 21, 2018
1 parent 6b734eb commit 27b6a6d
Show file tree
Hide file tree
Showing 6 changed files with 391 additions and 238 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v3.0.8

* Add support for parsing async and generator functions; these no longer break the parser. ([#90](https://github.com/jeffijoe/awilix/issues/90))
* Update dependencies.

# v3.0.7

* Skip code comments in parser ([#87](https://github.com/jeffijoe/awilix/issues/87))
Expand Down
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,29 @@
"homepage": "https://github.com/jeffijoe/awilix#readme",
"devDependencies": {
"@types/glob": "^5.0.35",
"@types/jest": "^22.2.2",
"@types/node": "^9.6.2",
"@types/prettier": "^1.10.0",
"babel-jest": "^22.4.3",
"@types/jest": "^22.2.3",
"@types/node": "^10.1.2",
"@types/prettier": "^1.12.2",
"babel-jest": "^22.4.4",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"coveralls": "^3.0.0",
"babel-preset-env": "^1.7.0",
"coveralls": "^3.0.1",
"husky": "^0.14.3",
"istanbul": "^0.4.5",
"jest": "^22.4.3",
"lint-staged": "^7.0.4",
"prettier": "^1.11.1",
"rollup": "^0.57.1",
"rollup-plugin-commonjs": "^9.1.0",
"jest": "^22.4.4",
"lint-staged": "^7.1.1",
"prettier": "^1.12.1",
"rollup": "^0.59.1",
"rollup-plugin-commonjs": "^9.1.3",
"rollup-plugin-node-resolve": "^3.3.0",
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-typescript2": "^0.12.0",
"rollup-plugin-typescript2": "^0.14.0",
"smid": "^0.1.1",
"ts-jest": "^22.4.2",
"tslint": "^5.9.1",
"tslint-config-prettier": "^1.10.0",
"ts-jest": "^22.4.6",
"tslint": "^5.10.0",
"tslint-config-prettier": "^1.13.0",
"tslint-config-standard": "^7.0.0",
"typescript": "^2.8.1"
"typescript": "^2.8.3"
},
"dependencies": {
"camel-case": "^3.0.0",
Expand Down
53 changes: 53 additions & 0 deletions src/__tests__/param-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,57 @@ class UserController {
{ name: 'userService', optional: false }
])
})

it('skips async keyword', () => {
expect(parseParameterList(`async function (first, second) {}`)).toEqual([
{
name: 'first',
optional: false
},
{
name: 'second',
optional: false
}
])
expect(parseParameterList(`async (first, second) => {}`)).toEqual([
{
name: 'first',
optional: false
},
{
name: 'second',
optional: false
}
])
expect(parseParameterList(`async () => {}`)).toEqual([])
expect(parseParameterList(`async => {}`)).toEqual([
{
name: 'async',
optional: false
}
])
})

it('skips generator star', () => {
expect(parseParameterList(`async function* (first, second) {}`)).toEqual([
{
name: 'first',
optional: false
},
{
name: 'second',
optional: false
}
])
expect(parseParameterList(`async function *(first, second) {}`)).toEqual([
{
name: 'first',
optional: false
},
{
name: 'second',
optional: false
}
])
})
})
4 changes: 4 additions & 0 deletions src/function-tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type TokenType =
| ')'
| ','
| '='
| '*'
| 'function'
| 'class'
| 'EOF'
Expand Down Expand Up @@ -75,6 +76,9 @@ export function createTokenizer(source: string) {
pos++
parenRight++
return (type = ch)
case '*':
pos++
return (type = ch)
case ',':
pos++
return (type = ch)
Expand Down
16 changes: 13 additions & 3 deletions src/param-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ export function parseParameterList(source: string): Array<Parameter> {
nextToken()
break
case 'function':
if (nextToken().type === 'ident') {
// This is the function name. Skip it.
const next = nextToken()
if (next.type === 'ident' || next.type === '*') {
// This is the function name or a generator star. Skip it.
nextToken()
}
break
Expand All @@ -52,7 +53,16 @@ export function parseParameterList(source: string): Array<Parameter> {
case 'ident':
// Likely a paren-less arrow function
// which can have no default args.
params.push({ name: t.value!, optional: false })
const param = { name: t.value!, optional: false }
if (t.value === 'async') {
// Given it's the very first token, we can assume it's an async function,
// so skip the async keyword.
const next = nextToken()
if (next && next.type !== '=') {
break
}
}
params.push(param)
return params
/* istanbul ignore next */
default:
Expand Down
Loading

0 comments on commit 27b6a6d

Please sign in to comment.