diff --git a/packages/convert/index.js b/packages/convert/index.js index 8a221d9..7956984 100644 --- a/packages/convert/index.js +++ b/packages/convert/index.js @@ -1,6 +1,7 @@ import estreeToBabel from 'estree-to-babel'; import {transform} from 'putout'; import {print} from '../printer/index.js'; +import * as removeImportTry from './remove-import-try/index.js'; import * as applyTry from './apply-try/index.js'; import { fixEmpty, @@ -13,6 +14,7 @@ export const convert = (source) => { transform(ast, source, { plugins: [ ['apply-try', applyTry], + ['remove-import-try', removeImportTry], ], }); diff --git a/packages/convert/index.spec.js b/packages/convert/index.spec.js index cc6f9e1..7c72e9a 100644 --- a/packages/convert/index.spec.js +++ b/packages/convert/index.spec.js @@ -13,3 +13,19 @@ test('goldstein: convert: tryCatch', (t) => { t.equal(result, expected); t.end(); }); + +test('goldstein: convert: tryCatch: import', (t) => { + const source = ` + import tryCatch from 'try-catch'; + const a = tryCatch(f, 'hello') + `; + + const result = convert(source); + + const expected = montag` + const a = try f('hello');\n + `; + + t.equal(result, expected); + t.end(); +}); diff --git a/packages/convert/remove-import-try/fixture/try-fix.js b/packages/convert/remove-import-try/fixture/try-fix.js index f096f82..59b5bfe 100644 --- a/packages/convert/remove-import-try/fixture/try-fix.js +++ b/packages/convert/remove-import-try/fixture/try-fix.js @@ -1,2 +1 @@ -import tryCatch from 'try-catch'; -const a = tryCatch(f, 'hello') \ No newline at end of file +const a = tryCatch(f, 'hello'); diff --git a/packages/convert/remove-import-try/fixture/try.js b/packages/convert/remove-import-try/fixture/try.js index e69de29..f096f82 100644 --- a/packages/convert/remove-import-try/fixture/try.js +++ b/packages/convert/remove-import-try/fixture/try.js @@ -0,0 +1,2 @@ +import tryCatch from 'try-catch'; +const a = tryCatch(f, 'hello') \ No newline at end of file diff --git a/packages/convert/remove-import-try/index.js b/packages/convert/remove-import-try/index.js index 422bed2..f847eca 100644 --- a/packages/convert/remove-import-try/index.js +++ b/packages/convert/remove-import-try/index.js @@ -1,23 +1,7 @@ -import {createGoldsteinTry} from '../../types/try.js'; - -export const report = () => `Use 'try' instead of 'tryCatch/tryToCatch'`; +export const report = () => `Remove import of 'tryCatch/tryToCatch'`; export const replace = () => ({ - 'tryCatch(__args)': createTry({ - async: false, - }), - 'await tryToCatch(__args)': createTry({ - async: true, - }), + 'import tryCatch from "try-catch"': '', + 'import tryToCatch from "try-to-catch"': '', + 'const tryCatch = require("try-catch")': '', + 'const tryToCatch = require("try-to-catch")': '', }); - -const createTry = ({async}) => ({__args}, path) => { - const [callee, ...args] = __args; - - path.node.goldstein = createGoldsteinTry({ - async, - callee, - args, - }); - - return path; -}; diff --git a/packages/convert/remove-import-try/index.spec.js b/packages/convert/remove-import-try/index.spec.js index e69de29..78a801c 100644 --- a/packages/convert/remove-import-try/index.spec.js +++ b/packages/convert/remove-import-try/index.spec.js @@ -0,0 +1,19 @@ +import {createTest} from '@putout/test'; +import * as removeImportTry from './index.js'; + +const test = createTest(import.meta.url, { + printer: 'putout', + plugins: [ + ['remove-import-try', removeImportTry], + ], +}); + +test('plugin-goldstein: remove-import-try: report', (t) => { + t.report('try', `Remove import of 'tryCatch/tryToCatch'`); + t.end(); +}); + +test('plugin-goldstein: remove-import-try: transform', (t) => { + t.transform('try'); + t.end(); +});