Skip to content

Commit

Permalink
feature: goldstein: types
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Oct 3, 2023
1 parent 44e688e commit db6bcbe
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 17 deletions.
10 changes: 5 additions & 5 deletions packages/convert/apply-try/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {createGoldsteinTry} from '../../keyword-try/index.js';
import {createGoldsteinTry} from '../../types/try.js';

export const report = () => `Use 'try' instead of 'tryCatch/tryToCatch'`;
export const replace = () => ({
'tryCatch(__args)': createTry({
awaitType: false,
async: false,
}),
'await tryToCatch(__args)': createTry({
awaitType: true,
async: true,
}),
});

const createTry = ({awaitType}) => ({__args}, path) => {
const createTry = ({async}) => ({__args}, path) => {
const [callee, ...args] = __args;

path.node.goldstein = createGoldsteinTry({
await: awaitType,
async,
callee,
args,
});
Expand Down
3 changes: 2 additions & 1 deletion packages/keyword-if/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {tokTypes as tt} from '../operator/index.js';
import {setGoldsteinIf} from '../types/if.js';

export default function fn(Parser) {
return class extends Parser {
Expand All @@ -19,7 +20,7 @@ export default function fn(Parser) {
node.consequent = this.parseStatement('if');
node.alternate = this.eat(tt._else) ? this.parseStatement('if') : null;

node.goldsteinIf = true;
setGoldsteinIf(node);

return this.finishNode(node, 'IfStatement');
}
Expand Down
15 changes: 5 additions & 10 deletions packages/keyword-try/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
SCOPE_SIMPLE_CATCH,
tokTypes as tt,
} from '../operator/index.js';
import {setGoldsteinTry} from '../types/try.js';

const {
isCallExpression,
Expand Down Expand Up @@ -43,20 +44,10 @@ export default function keywordTry(Parser) {
expression.callee,
...expression.arguments,
],
goldstein: createGoldsteinTry({
await: false,
callee: expression.callee,
args: expression.arguments,
}),
};
else if (isAwaitExpression(expression))
node.expression = {
type: 'AwaitExpression',
goldstein: createGoldsteinTry({
await: true,
callee: expression.argument.callee,
args: expression.argument.arguments,
}),
argument: {
type: 'CallExpression',
callee: {
Expand All @@ -72,6 +63,8 @@ export default function keywordTry(Parser) {
else
this.raise(this.start, `After 'try' only '{', 'await' and 'function call' can come`);

setGoldsteinTry(node.expression);

return super.finishNode(node, 'ExpressionStatement');
}

Expand Down Expand Up @@ -105,6 +98,8 @@ export default function keywordTry(Parser) {
if (!node.handler && !node.finalizer)
this.raise(node.start, 'Missing catch or finally clause');

setGoldsteinTry(node);

return this.finishNode(node, 'TryStatement');
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/printer/visitors/try-statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export const TryStatement = (path, printer, semantics) => {
return v.TryStatement(path, printer, semantics);

print('try ');
maybe.print(node.await, 'await ');
maybe.print(node.async, 'await ');
print('__argument');
};
3 changes: 3 additions & 0 deletions packages/types/if.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const setGoldsteinIf = (node) => {
node.goldsteinIf = true;
};
50 changes: 50 additions & 0 deletions packages/types/try.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {types} from 'putout';

const {
isAwaitExpression,
isCallExpression,
} = types;

export function createGoldsteinTry({async, callee, args}) {
return {
type: 'TryStatement',
expression: true,
async,
argument: {
type: 'CallExpression',
callee,
arguments: args,
},
};
}

export const setGoldsteinTry = (node) => {
if (!isCallExpression(node) && !isAwaitExpression(node))
return;

node.goldstein = createGoldsteinTry(parseExpression(node));
};

const parseExpression = (node) => {
const {argument, arguments: allArgs} = node;

const async = isAwaitExpression(node);

if (!async) {
const [callee, ...args] = allArgs;

return {
async,
args,
callee,
};
}

const [callee, ...args] = argument.arguments;

return {
async,
args,
callee,
};
};

0 comments on commit db6bcbe

Please sign in to comment.