IanVS / prettier-plugin-sort-imports

An opinionated but flexible prettier plugin to sort import statements
Apache License 2.0
951 stars 21 forks source link

Parser Error with the JSX parser plugin & TypeScript old-style assertions <SomeType>foo #78

Closed fbartho closed 1 year ago

fbartho commented 1 year ago

Your Environment

Describe the bug

I ran 4.0.0-alpha.3 and 4.0.0-alpha.4 on my large monorepo codebase, and found a couple places where the parsing crashed, with a useless error message:

$ yarn prettier --write […]/devices.service.ts
[error] […]/devices.service.ts: SyntaxError: Unexpected token (48:76)
[error] // and thus follows all the lines of the file.

To Reproduce

  1. Have jsx in your importOrderParserPlugins
    importOrderParserPlugins : ['typescript', 'jsx', 'decorators-legacy', 'classProperties']
  2. Have typescript code like so:
    
    import { b } from "b";
    import { a } from "a";

// Minimal replication: a();

// Less minimal: // export class SomeClass { // async someMethod( // anArg:string // ): Promise { // return { // a: something.a, // b: something.b, // }; // } // }


When running inside this repo's test-cases, I get this useful log message:

Serialized Error: { "clone": "Function", "code": "BABEL_PARSER_SYNTAX_ERROR", "codeFrame": " 1 | 2 | import { b } from "b"; 3 | import { a } from "a"; 4 | 5 | // Minimal replication: 6 | a(); 7 |", "details": { "expected": null, }, "loc": { "column": 0, "constructor": "Function", "index": 284, "line": 19, }, "pos": 284, "reasonCode": "UnexpectedToken", }


**Expected behavior**

1. No crash
1. When crashes do occur, I'd like a better error message

**Configuration File (cat .prettierrc, prettier.config.js, .prettier.js)**

```js
module.exports = {
    overrides: [
        {
            // prettier will strip newlines out of package.json files unless you tell it to use the json parser
            files: ["package.json", "**/package.json"],
            options: { parser: "json" },
        },
    ],
    printWidth: 100,
    semi: true,
    singleQuote: false, // Single quotes are common in text-strings
    trailingComma: "all", // Improves refactoring / minimizes git-conflicts
    tabWidth: 2,
    useTabs: true,
    // @ianvs/prettier-plugin-sort-imports
    importOrder: ["<THIRD_PARTY_MODULES>", "", "^@hca", "", "^[.]+"],
    importOrderTypeScriptVersion: "4.9.5", // Doesn't need to be tracked automatically, see https://github.com/IanVS/prettier-plugin-sort-imports#importordertypescriptversion if curious
    importOrderParserPlugins: ["typescript", "decorators-legacy", "classProperties"],
    // @prettier/plugin-xml
    xmlWhitespaceSensitivity: "ignore",
};

Contribute to @ianvs/prettier-plugin-sort-imports

  • [x] I'm willing to fix this bug 🥇
fbartho commented 1 year ago

I have a workaround for our repo (remove the jsx parser plugin) -- all our code is typescript-based, so this isn't a problem for us. Not sure what mixed ts/tsx/js/jsx codebases are supposed to do.