Skip to content

Commit

Permalink
feature: rules: add convert-t-raise-to-raise
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Aug 28, 2023
1 parent 86c5230 commit cb5d441
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 17 deletions.
21 changes: 21 additions & 0 deletions rules/goldstein/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test('goldstein: keyword: import', ({compile}) => {
compile('import');
});

18 changes: 3 additions & 15 deletions rules/goldstein/lib/convert-t-compile-to-compile/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {types} from 'putout';
import {transform} from '../transform.js';

const {
ObjectProperty,
Expand All @@ -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'),
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ 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();
});

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();
});
Original file line number Diff line number Diff line change
@@ -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)`);
});
Original file line number Diff line number Diff line change
@@ -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)`);
});
18 changes: 18 additions & 0 deletions rules/goldstein/lib/convert-t-raise-to-raise/index.js
Original file line number Diff line number Diff line change
@@ -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'),
});
19 changes: 19 additions & 0 deletions rules/goldstein/lib/convert-t-raise-to-raise/index.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
25 changes: 25 additions & 0 deletions rules/goldstein/lib/transform.js
Original file line number Diff line number Diff line change
@@ -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})`;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
test('goldstein: keyword: import', async ({compile}) => {
await compile('import');
t.end();
});
3 changes: 3 additions & 0 deletions rules/goldstein/test/fixture/convert-t-raise-to-raise-fix.js
Original file line number Diff line number Diff line change
@@ -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)`);
});
3 changes: 3 additions & 0 deletions rules/goldstein/test/fixture/convert-t-raise-to-raise.js
Original file line number Diff line number Diff line change
@@ -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)`);
});
5 changes: 5 additions & 0 deletions rules/goldstein/test/goldstein.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit cb5d441

Please sign in to comment.