Skip to content

Commit

Permalink
feature: goldstein: convert: rm useless import of try-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Oct 4, 2023
1 parent 44a3d27 commit 8192b7a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
2 changes: 2 additions & 0 deletions packages/convert/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,6 +14,7 @@ export const convert = (source) => {
transform(ast, source, {
plugins: [
['apply-try', applyTry],
['remove-import-try', removeImportTry],
],
});

Expand Down
16 changes: 16 additions & 0 deletions packages/convert/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
3 changes: 1 addition & 2 deletions packages/convert/remove-import-try/fixture/try-fix.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
import tryCatch from 'try-catch';
const a = tryCatch(f, 'hello')
const a = tryCatch(f, 'hello');
2 changes: 2 additions & 0 deletions packages/convert/remove-import-try/fixture/try.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import tryCatch from 'try-catch';
const a = tryCatch(f, 'hello')
26 changes: 5 additions & 21 deletions packages/convert/remove-import-try/index.js
Original file line number Diff line number Diff line change
@@ -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;
};
19 changes: 19 additions & 0 deletions packages/convert/remove-import-try/index.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 8192b7a

Please sign in to comment.