Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
fbartho opened this issue May 12, 2023 · 1 comment · Fixed by #91
Closed
1 task done
Assignees

Comments

@fbartho
Copy link
Collaborator

fbartho commented May 12, 2023

Your Environment

  • Prettier version: 2.8.8
  • node version: v18.11.0
  • package manager: yarn@1.22.19
  • IDE: VScode up-to-date

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']
  1. Have typescript code like so:
import { b } from "b";
import { a } from "a";

// Minimal replication:
<A>a();

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

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

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

Expected behavior

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

Configuration File (cat .prettierrc, prettier.config.js, .prettier.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

  • I'm willing to fix this bug 🥇
@fbartho
Copy link
Collaborator Author

fbartho commented May 12, 2023

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.

@IanVS IanVS closed this as completed in #91 May 17, 2023
IanVS added a commit that referenced this issue May 17, 2023
This fixes a few issues with parser plugins.

It fixes #78
by removing the `jsx` parser plugin when the file is named `.ts`
(typescript requires the use of `.tsx` extension for jsx).

It also applies similar logic for .vue files, ensuring that the correct
parser plugins are used, with similar logic to the vue compiler itself.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants