prettier/prettier-eslint

Formatting returns corrupted output when run on multiple typescript files

zeemi opened this issue · 5 comments

zeemi commented

Versions:

  • prettier-eslint version: v9.0.0
  • node version: v11.10.1
  • npm (or yarn) version: v6.9.0

Have you followed the debugging tips?

Yes
Meaningful log:

executeOnText returned the following report: Object {
  "errorCount": 1,
  "fixableErrorCount": 0,
  "fixableWarningCount": 0,
  "results": Array [
    Object {
      "errorCount": 1,
      "filePath": "...",
      "fixableErrorCount": 0,
      "fixableWarningCount": 0,
      "messages": Array [
        Object {
          "column": 11,
          "fatal": true,
          "line": 2,
          "message": "Parsing error: ';' expected.",
          "ruleId": null,
          "severity": 2,
        },
      ],
      "output": "const parse = () => {
     eturn {};
};
export default parse;
",

Relevant code or config

eslint config:

module.exports = {
    parser: '@typescript-eslint/parser',
    extends: [
        'plugin:@typescript-eslint/recommended',
    ],
    parserOptions: {
        project: './tsconfig.json',
        tsconfigRootDir: '.',
        ecmaVersion: 2018,  // Allows for the parsing of modern ECMAScript features
        sourceType: 'module',  // Allows for the use of imports
    },
};

tsconfig:

{
  "compilerOptions": {
    "outDir": "./dist/",       
    "sourceMap": true,         
    "strictNullChecks": true,
    "module": "es6",           
    "target": "es5",            
    "allowJs": true, 
    "moduleResolution": "node",
    "esModuleInterop":true,
    "strict": true
  },
  "include": [
    "./src/"
  ]
}

What I did:
Run prettier-eslint on few typescript files:
prettier-eslint --write '**/*.[tj]s'

What happened:
Reformatting generated corrupted file
input:

const parse = ()  => {
  return {};
};
export default parse;

output (return => eturn)

const parse = () => {
     eturn {};
};
export default parse;

Reproduction repository:
https://github.com/zeemi/prettier-eslint-bug-with-typescript

Problem description:
[run by prettier-eslint-cli]
When prettier-eslint is run on each file separately, everything is working great (npm run format-succ in reproduction repo)

When prettier-eslint is run on many files at once (via list or glob), some of the files are returned corrupted (npm run format-fail in reproduction repo).
During file corruption, parsing error is visible in the logs.

This issue occurs only when parserOptions.project property is set in eslint config.
Nevertheless, both: eslint and prettier run from command line works without any issues.

Suggested solution:
Check if prettier-eslint is not holding any state between files formatting that might corrupt next file parsing.

I'm experiencing the same, although I have no parserOptions.project configuration. The problem appears to occur during the eslint-fixing stage, since putting --prettier-last causes it to blow up when trying to run the resulting output through prettier.

I have an branch named prettier-misbehaving here. Pull that down and run yarn run prettier to reproduce the errors.

Same issue. I am using prettier-eslint-cli.
I have no issue when formatting only one file, but if I format multiple files, half of them are badly formatted.

const mapStateToProps = (state: RootState) => ({
  user: state.user,
});

become

const mapSta=eToProps = (state: RootSta  ) => ({
   user: state.user,
 });

OR

enableOtherChargeEdit = () => {
    const { employee } = this.props;
    this.setState({
      isEditingOtherCharge: true,
      otherChargeValue: String(employee.otherCharge / 100),
    });
  };

become

enableOtherChargeEdit = () => {
   na  st {  em ployee } =  t=is.props;
   na  s.setState({
   na    ditingOtherCharge: true,
   na    erChargeValue: String(employee.otherCharge / 1/0),
   na
  }na

Didn't have this issue with prettier-eslint v8.

Yeah, removing parserOptions.project from .eslintrc fix the issue.
But don't know if it's a good thing to do.

jhnns commented

I can confirm this issue (but only for tsx files for now).

I debugged it but couldn't find the definite cause. But I still had some insights:

  • The first file is always fixed without errors
  • All additional files get corrupted
  • The source code is always ok when passed to executeOnText but comes back corrupted. This indicates that the bug is somewhere in ESLint land.
  • Calling eslint --fix with multiple files does not corrupt the files. This is because the ESLint CLI calls executeOnFiles when multiple file paths are passed.
  • I changed the call from executeOnText to cliEngine.executeOnFiles([filePath]); and now the files are not corrupted anymore.

This is not a good fix because ESLint is going to read the file again (and this might not be desired in all scenarios) but it's a start :)

Stale issue