From db6bcbea90442d09801e7f7d40a373ce3e12bc74 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 3 Oct 2023 18:38:42 +0300 Subject: [PATCH] feature: goldstein: types --- packages/convert/apply-try/index.js | 10 ++--- packages/keyword-if/index.js | 3 +- packages/keyword-try/index.js | 15 +++---- packages/printer/visitors/try-statement.js | 2 +- packages/types/if.js | 3 ++ packages/types/try.js | 50 ++++++++++++++++++++++ 6 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 packages/types/if.js create mode 100644 packages/types/try.js diff --git a/packages/convert/apply-try/index.js b/packages/convert/apply-try/index.js index 07eff6c..422bed2 100644 --- a/packages/convert/apply-try/index.js +++ b/packages/convert/apply-try/index.js @@ -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, }); diff --git a/packages/keyword-if/index.js b/packages/keyword-if/index.js index 8e4a18a..950fda5 100644 --- a/packages/keyword-if/index.js +++ b/packages/keyword-if/index.js @@ -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 { @@ -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'); } diff --git a/packages/keyword-try/index.js b/packages/keyword-try/index.js index 620ec21..f79563c 100644 --- a/packages/keyword-try/index.js +++ b/packages/keyword-try/index.js @@ -5,6 +5,7 @@ import { SCOPE_SIMPLE_CATCH, tokTypes as tt, } from '../operator/index.js'; +import {setGoldsteinTry} from '../types/try.js'; const { isCallExpression, @@ -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: { @@ -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'); } @@ -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'); } }; diff --git a/packages/printer/visitors/try-statement.js b/packages/printer/visitors/try-statement.js index 0807c3b..9a98bea 100644 --- a/packages/printer/visitors/try-statement.js +++ b/packages/printer/visitors/try-statement.js @@ -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'); }; diff --git a/packages/types/if.js b/packages/types/if.js new file mode 100644 index 0000000..bf59ae7 --- /dev/null +++ b/packages/types/if.js @@ -0,0 +1,3 @@ +export const setGoldsteinIf = (node) => { + node.goldsteinIf = true; +}; diff --git a/packages/types/try.js b/packages/types/try.js new file mode 100644 index 0000000..a7457dd --- /dev/null +++ b/packages/types/try.js @@ -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, + }; +};