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

Add should keyword #5

Merged
merged 2 commits into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ import tryToCatch from 'try-catch';
const [error, result] = await tryToCatch(1, 2, 3);
```

### `should`

`should` can be used as an expression.
This keyword is useful if you want to prevent a function call (also async) to throw an error because you don't need to have any result and the real execution is just optional (so runs if supported).

```gs
should hello()
```

Is the same as:

```js
try {
hello();
} catch (e) {};
```

> Warning: this feature can be helpful but also dangerous especially if you're debugging your application. In fact, this is made to be used as an optional function call (ex. should load content, but not necessary and knowing this feature is optional), if you call a function in this way while debugging, no error will be printed and the application will continue run as nothing happened.

### `if`

You can omit parens. But you must use braces in this case.
Expand Down
1 change: 1 addition & 0 deletions packages/keyword-should/fixture/await-should.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
should await hello()
3 changes: 3 additions & 0 deletions packages/keyword-should/fixture/await-should.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
try {
await hello();
} catch (e) {};
4 changes: 4 additions & 0 deletions packages/keyword-should/fixture/not-supported.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
should {
var a = "hello"
console.log(a)
}
1 change: 1 addition & 0 deletions packages/keyword-should/fixture/should.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
should hello()
3 changes: 3 additions & 0 deletions packages/keyword-should/fixture/should.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
try {
hello();
} catch (e) {};
99 changes: 99 additions & 0 deletions packages/keyword-should/index.js
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');
}
};
}

19 changes: 19 additions & 0 deletions packages/keyword-should/index.spec.js
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();
});