coderaiser / putout

🐊 Pluggable and configurable JavaScript Linter, code transformer and formatter, drop-in ESLint superpower replacement 💪 with built-in support for js, jsx, typescript, flow, markdown, yaml and json. Write declarative codemods in a simplest possible way 😏
https://putout.cloudcmd.io/
MIT License
712 stars 40 forks source link

Return support for plugin-extract-sequence-expressions #11

Closed Yesterday17 closed 5 years ago

Yesterday17 commented 5 years ago

For example:

function t() {
  return a, b, c, d;
}

equals

function t() {
  a;b;c;
  return d;
}
coderaiser commented 5 years ago

There was no support of such cases in putout. But it is a good idea :).

traverse should be modified to handle ReturnStatement, same with fix.

coderaiser commented 5 years ago

Added support of ReturnStatement to @putout/plugin-extract-sequence-expressions v1.7.0. Please reinstall putout.

Is it works for you?

Yesterday17 commented 5 years ago

It works on many occasions, but on some code it may break, for example, it throws an error after trying to transform the following code(sequenced expression with multiple layers):

const putout = require('putout');

const code = `function t(){return 1,2,(3,4),5,(6,7)}`;

console.log(
  putout(code, {
    plugins: ['extract-sequence-expressions']
  }).code
);
/home/yesterday17/GitHub/majsoul-source-prettier/node_modules/@putout/engine-runner/lib/run-fix.js:13
    throw e;
    ^

Error: cannot turn SequenceExpression to a statement
    at toStatement (/home/yesterday17/GitHub/majsoul-source-prettier/node_modules/@babel/types/lib/converters/toStatement.js:38:13)
    at wrap (/home/yesterday17/GitHub/majsoul-source-prettier/node_modules/@putout/plugin-extract-sequence-expressions/lib/extract-sequence-expressions.js:21:12)
    at Array.map (<anonymous>)
    at module.exports.fix (/home/yesterday17/GitHub/majsoul-source-prettier/node_modules/@putout/plugin-extract-sequence-expressions/lib/extract-sequence-expressions.js:53:28)
    at tryCatch (/home/yesterday17/GitHub/majsoul-source-prettier/node_modules/try-catch/lib/try-catch.js:7:26)
    at tryToFix (/home/yesterday17/GitHub/majsoul-source-prettier/node_modules/@putout/engine-runner/lib/run-fix.js:6:17)
    at module.exports (/home/yesterday17/GitHub/majsoul-source-prettier/node_modules/@putout/engine-runner/lib/run-fix.js:20:5)
    at push (/home/yesterday17/GitHub/majsoul-source-prettier/node_modules/@putout/engine-runner/lib/merge-visitors.js:63:9)
    at SequenceExpression (/home/yesterday17/GitHub/majsoul-source-prettier/node_modules/@putout/plugin-extract-sequence-expressions/lib/extract-sequence-expressions.js:71:17)
    at NodePath._call (/home/yesterday17/GitHub/majsoul-source-prettier/node_modules/@babel/traverse/lib/path/context.js:53:20) {
  loc: { line: 1, column: 20 }
}

And on transforming arrow function, it produced a wrong output:

const putout = require('putout');

const code = `() => 1,2,3,4,5,6,7`;

console.log(
  putout(code, {
    plugins: ['extract-sequence-expressions']
  }).code
);

Output:

() => 1;
2;
3;
4;
5;
6;
7;
coderaiser commented 5 years ago

Do you have such code in your codebase? Look every case should be programmed independently (ArrowFunctions, nested SequenceExpressions etc) to get things works. I think that only code from a real codebase should be used as example.

Possible cases to break things is countless 🙂. But if you think that it's important you can always send a pull request.

coderaiser commented 5 years ago

OK, multiple layers worth fixing, it is landed in @putout/plugin-extract-sequence-expressions v1.8.0, but transforming this:

() => 1,2,3,4,5,6,7

into this:

() => 1;
2;
3;
4;
5;
6;
7;

Is absolutely correct because () => 1 is one of sequence expressions.

If you want to put all numbers into body of an arrow function you need to add brackets:

() => (1,2,3,4,5,6,7)

And it will be:

() => {
  1;
  2;
  3;
  4;
  5;
  6;
  return 7;
}