From cb5d4415afb04468c6673032e499073ca3bafbf9 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 28 Aug 2023 21:10:47 +0300 Subject: [PATCH] feature: rules: add convert-t-raise-to-raise --- rules/goldstein/README.md | 21 ++++++++++++++++ .../fixture/done.js | 4 +++ .../lib/convert-t-compile-to-compile/index.js | 18 +++---------- .../index.spec.js | 7 +++++- .../fixture/convert-t-raise-to-raise-fix.js | 3 +++ .../fixture/convert-t-raise-to-raise.js | 3 +++ .../lib/convert-t-raise-to-raise/index.js | 18 +++++++++++++ .../convert-t-raise-to-raise/index.spec.js | 19 ++++++++++++++ rules/goldstein/lib/transform.js | 25 +++++++++++++++++++ .../convert-t-compile-to-compile-fix.js | 1 - .../fixture/convert-t-raise-to-raise-fix.js | 3 +++ .../test/fixture/convert-t-raise-to-raise.js | 3 +++ rules/goldstein/test/goldstein.js | 5 ++++ 13 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 rules/goldstein/lib/convert-t-compile-to-compile/fixture/done.js create mode 100644 rules/goldstein/lib/convert-t-raise-to-raise/fixture/convert-t-raise-to-raise-fix.js create mode 100644 rules/goldstein/lib/convert-t-raise-to-raise/fixture/convert-t-raise-to-raise.js create mode 100644 rules/goldstein/lib/convert-t-raise-to-raise/index.js create mode 100644 rules/goldstein/lib/convert-t-raise-to-raise/index.spec.js create mode 100644 rules/goldstein/lib/transform.js create mode 100644 rules/goldstein/test/fixture/convert-t-raise-to-raise-fix.js create mode 100644 rules/goldstein/test/fixture/convert-t-raise-to-raise.js diff --git a/rules/goldstein/README.md b/rules/goldstein/README.md index ab4a87d..82cf1e0 100644 --- a/rules/goldstein/README.md +++ b/rules/goldstein/README.md @@ -48,6 +48,27 @@ test('goldstein: keyword: import', async ({compile}) => { }); ``` +## convert-t-compile-to-compile + +Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/5272c99178bbf9e75f30177e7e9c15a9/9a3e81a9e9dd756915cbecff32acc5c50cddeca3). + +### ❌ Example of incorrect code + +```js +test('goldstein: keyword: try: not-supported', (t) => { + t.raise('not-supported', `After 'try' only '{', 'await' and 'function call' can come (1:8)`); +}); +``` + +### ✅ Example of correct code + +```js +test('goldstein: keyword: try: not-supported', async ({raise}) => { + await raise('not-supported', `After 'try' only '{', 'await' and 'function call' can come (1:8)`); +}); + +``` + ## License MIT diff --git a/rules/goldstein/lib/convert-t-compile-to-compile/fixture/done.js b/rules/goldstein/lib/convert-t-compile-to-compile/fixture/done.js new file mode 100644 index 0000000..87a4991 --- /dev/null +++ b/rules/goldstein/lib/convert-t-compile-to-compile/fixture/done.js @@ -0,0 +1,4 @@ +test('goldstein: keyword: import', ({compile}) => { + compile('import'); +}); + diff --git a/rules/goldstein/lib/convert-t-compile-to-compile/index.js b/rules/goldstein/lib/convert-t-compile-to-compile/index.js index 80a6060..6b7a336 100644 --- a/rules/goldstein/lib/convert-t-compile-to-compile/index.js +++ b/rules/goldstein/lib/convert-t-compile-to-compile/index.js @@ -1,4 +1,5 @@ import {types} from 'putout'; +import {transform} from '../transform.js'; const { ObjectProperty, @@ -9,22 +10,9 @@ const { const COMPUTED = false; const SHORTHAND = true; -export const report = () => `Use 'compile()' instead of 't.compile'`; +export const report = () => `Use 'compile()' instead of 't.compile()'`; export const replace = () => ({ 't.end()': '', - 't.compile(__a)': (vars, path) => { - const {block} = path.scope; - - path.scope.block.async = true; - - const id = Identifier('compile'); - const param = ObjectPattern([ - ObjectProperty(id, id, COMPUTED, SHORTHAND), - ]); - - block.params = [param]; - - return 'await compile(__a)'; - }, + 't.compile(__a)': transform('compile', '__a'), }); diff --git a/rules/goldstein/lib/convert-t-compile-to-compile/index.spec.js b/rules/goldstein/lib/convert-t-compile-to-compile/index.spec.js index 1d66d17..ddaae78 100644 --- a/rules/goldstein/lib/convert-t-compile-to-compile/index.spec.js +++ b/rules/goldstein/lib/convert-t-compile-to-compile/index.spec.js @@ -9,7 +9,7 @@ const test = createTest(import.meta.url, { }); test('rules: convert-t-compile-to-compile: report', (t) => { - t.report('convert-t-compile-to-compile', `Use 'compile()' instead of 't.compile'`); + t.report('convert-t-compile-to-compile', `Use 'compile()' instead of 't.compile()'`); t.end(); }); @@ -17,3 +17,8 @@ test('rules: convert-t-compile-to-compile: transform', (t) => { t.transform('convert-t-compile-to-compile'); t.end(); }); + +test('rules: convert-t-compile-to-compile: no report: done', (t) => { + t.noReport('done'); + t.end(); +}); diff --git a/rules/goldstein/lib/convert-t-raise-to-raise/fixture/convert-t-raise-to-raise-fix.js b/rules/goldstein/lib/convert-t-raise-to-raise/fixture/convert-t-raise-to-raise-fix.js new file mode 100644 index 0000000..845cd32 --- /dev/null +++ b/rules/goldstein/lib/convert-t-raise-to-raise/fixture/convert-t-raise-to-raise-fix.js @@ -0,0 +1,3 @@ +test('goldstein: keyword: try: not-supported', async ({raise}) => { + await raise('not-supported', `After 'try' only '{', 'await' and 'function call' can come (1:8)`); +}); diff --git a/rules/goldstein/lib/convert-t-raise-to-raise/fixture/convert-t-raise-to-raise.js b/rules/goldstein/lib/convert-t-raise-to-raise/fixture/convert-t-raise-to-raise.js new file mode 100644 index 0000000..2551bda --- /dev/null +++ b/rules/goldstein/lib/convert-t-raise-to-raise/fixture/convert-t-raise-to-raise.js @@ -0,0 +1,3 @@ +test('goldstein: keyword: try: not-supported', (t) => { + t.raise('not-supported', `After 'try' only '{', 'await' and 'function call' can come (1:8)`); +}); diff --git a/rules/goldstein/lib/convert-t-raise-to-raise/index.js b/rules/goldstein/lib/convert-t-raise-to-raise/index.js new file mode 100644 index 0000000..fdd8014 --- /dev/null +++ b/rules/goldstein/lib/convert-t-raise-to-raise/index.js @@ -0,0 +1,18 @@ +import {types} from 'putout'; +import {transform} from '../transform.js'; + +const { + ObjectProperty, + ObjectPattern, + Identifier, +} = types; + +const COMPUTED = false; +const SHORTHAND = true; + +export const report = () => `Use 'raise()' instead of 't.raise()'`; + +export const replace = () => ({ + 't.end()': '', + 't.raise(__a, __b)': transform('raise', '__a, __b'), +}); diff --git a/rules/goldstein/lib/convert-t-raise-to-raise/index.spec.js b/rules/goldstein/lib/convert-t-raise-to-raise/index.spec.js new file mode 100644 index 0000000..60ea219 --- /dev/null +++ b/rules/goldstein/lib/convert-t-raise-to-raise/index.spec.js @@ -0,0 +1,19 @@ +import {createTest} from '@putout/test'; +import * as plugin from './index.js'; + +const test = createTest(import.meta.url, { + printer: 'putout', + plugins: [ + ['convert-t-raise-to-raise', plugin], + ], +}); + +test('rules: convert-t-raise-to-raise: report', (t) => { + t.report('convert-t-raise-to-raise', `Use 'raise()' instead of 't.raise()'`); + t.end(); +}); + +test('rules: convert-t-raise-to-raise: transform', (t) => { + t.transform('convert-t-raise-to-raise'); + t.end(); +}); diff --git a/rules/goldstein/lib/transform.js b/rules/goldstein/lib/transform.js new file mode 100644 index 0000000..b72c95e --- /dev/null +++ b/rules/goldstein/lib/transform.js @@ -0,0 +1,25 @@ +import {types} from 'putout'; + +const { + ObjectProperty, + ObjectPattern, + Identifier, +} = types; + +const COMPUTED = false; +const SHORTHAND = true; + +export const transform = (name, args) => (vars, path) => { + const {block} = path.scope; + + path.scope.block.async = true; + + const id = Identifier(name); + const param = ObjectPattern([ + ObjectProperty(id, id, COMPUTED, SHORTHAND), + ]); + + block.params = [param]; + + return `await ${name}(${args})`; +}; diff --git a/rules/goldstein/test/fixture/convert-t-compile-to-compile-fix.js b/rules/goldstein/test/fixture/convert-t-compile-to-compile-fix.js index 4e12c19..0286bbd 100644 --- a/rules/goldstein/test/fixture/convert-t-compile-to-compile-fix.js +++ b/rules/goldstein/test/fixture/convert-t-compile-to-compile-fix.js @@ -1,4 +1,3 @@ test('goldstein: keyword: import', async ({compile}) => { await compile('import'); - t.end(); }); diff --git a/rules/goldstein/test/fixture/convert-t-raise-to-raise-fix.js b/rules/goldstein/test/fixture/convert-t-raise-to-raise-fix.js new file mode 100644 index 0000000..2551bda --- /dev/null +++ b/rules/goldstein/test/fixture/convert-t-raise-to-raise-fix.js @@ -0,0 +1,3 @@ +test('goldstein: keyword: try: not-supported', (t) => { + t.raise('not-supported', `After 'try' only '{', 'await' and 'function call' can come (1:8)`); +}); diff --git a/rules/goldstein/test/fixture/convert-t-raise-to-raise.js b/rules/goldstein/test/fixture/convert-t-raise-to-raise.js new file mode 100644 index 0000000..2551bda --- /dev/null +++ b/rules/goldstein/test/fixture/convert-t-raise-to-raise.js @@ -0,0 +1,3 @@ +test('goldstein: keyword: try: not-supported', (t) => { + t.raise('not-supported', `After 'try' only '{', 'await' and 'function call' can come (1:8)`); +}); diff --git a/rules/goldstein/test/goldstein.js b/rules/goldstein/test/goldstein.js index 4ae964b..ecc1212 100644 --- a/rules/goldstein/test/goldstein.js +++ b/rules/goldstein/test/goldstein.js @@ -12,3 +12,8 @@ test('plugin-goldstein: transform: convert-t-compile-to-compile', (t) => { t.transform('convert-t-compile-to-compile'); t.end(); }); + +test('plugin-goldstein: transform: convert-t-raise-to-raise', (t) => { + t.transform('convert-t-raise-to-raise'); + t.end(); +});