Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add should keyword * Fix bad spelling Co-authored-by: Giorgio Bellisario <{ID}+{username}@users.noreply.github.com>
- Loading branch information
1 parent
9481390
commit beb1a4c
Showing
8 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
should await hello() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
try { | ||
await hello(); | ||
} catch (e) {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
should { | ||
var a = "hello" | ||
console.log(a) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
should hello() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
try { | ||
hello(); | ||
} catch (e) {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import {types} from 'putout'; | ||
import { | ||
addKeyword, | ||
TokenType, | ||
tokTypes as tt, | ||
} from '../operator/index.js'; | ||
|
||
const { | ||
isCallExpression, | ||
isAwaitExpression, | ||
} = types; | ||
|
||
export default function newSpeak(Parser) { | ||
const {keywordTypes} = Parser.acorn; | ||
keywordTypes.should = new TokenType('should', { | ||
keyword: 'should', | ||
}); | ||
|
||
return class extends Parser { | ||
parse() { | ||
this.keywords = addKeyword('should', this.keywords); | ||
return super.parse(); | ||
} | ||
parseStatement(context, topLevel, exports) { | ||
if (this.type === keywordTypes.should) { | ||
return this.parseShould(); | ||
} | ||
|
||
return super.parseStatement(context, topLevel, exports); | ||
} | ||
|
||
parseShould() { | ||
this.next(); | ||
|
||
const node = super.startNode(); | ||
|
||
if (this.type === tt.braceL) | ||
return this.raise(this.start, `After 'should' only 'await' and 'function call' can come, brakets are not suppoted`); | ||
|
||
const expression = this.parseExpression(); | ||
|
||
if (isCallExpression(expression)) | ||
node.expression = { | ||
type: 'TryStatement', | ||
block: { | ||
type: 'BlockStatement', | ||
body: [{ | ||
type: 'ExpressionStatement', | ||
expression: { | ||
type: 'CallExpression', | ||
callee: expression.callee, | ||
arguments: expression.arguments.slice(), | ||
}, | ||
}], | ||
}, | ||
handler: { | ||
type: 'CatchClause', | ||
param: { | ||
type: 'Identifier', | ||
name: 'e', | ||
}, | ||
body: { | ||
type: 'BlockStatement', | ||
body: [], | ||
}, | ||
}, | ||
}; | ||
|
||
else if (isAwaitExpression(expression)) | ||
node.expression = { | ||
type: 'TryStatement', | ||
block: { | ||
type: 'BlockStatement', | ||
body: [{ | ||
type: 'ExpressionStatement', | ||
expression, | ||
}], | ||
}, | ||
handler: { | ||
type: 'CatchClause', | ||
param: { | ||
type: 'Identifier', | ||
name: 'e', | ||
}, | ||
body: { | ||
type: 'BlockStatement', | ||
body: [], | ||
}, | ||
}, | ||
}; | ||
|
||
else | ||
this.raise(this.start, `After 'should' only 'await' and 'function call' can come`); | ||
|
||
return super.finishNode(node, 'ExpressionStatement'); | ||
} | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {createTest} from '../test/index.js'; | ||
import keywordFn from './index.js'; | ||
|
||
const test = createTest(import.meta.url, keywordFn); | ||
|
||
test('goldstein: keyword: should', (t) => { | ||
t.compile('should'); | ||
t.end(); | ||
}); | ||
|
||
test('goldstein: keyword: should (with await)', (t) => { | ||
t.compile('await-should'); | ||
t.end(); | ||
}); | ||
|
||
test('goldstein: keyword: should (brakets)', (t) => { | ||
t.raise('not-supported', `After 'should' only 'await' and 'function call' can come, brakets are not suppoted (1:7)`); | ||
t.end(); | ||
}); |