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

fix(js_formatter): don't insert trailing comma on type import statement #4454

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fireairforce
Copy link
Contributor

Summary

closes: #4334

Test Plan

@github-actions github-actions bot added A-Parser Area: parser A-Tooling Area: internal tools L-JavaScript Language: JavaScript and super languages labels Nov 3, 2024
// a: import("./test").T<typeof X>
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// a: import("./test")<string>
// ^^^^^^^^^^^^^^^^^^^^^^^^^
TsImportType =
'typeof'?
'import'
arguments: JsCallArguments
arguments: TsImportArguments
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, based on the typescript source, it looks like it should be like this:

// (not tested)
TsImportType =
	'typeof'?
	'import'
	argument: AnyTsType
	(
		','
		'{'
		('assert' | 'with')
		':'
		'{'
		assertions: TsImportTypeAssertionList
		'}'
		'}'
	)?
	qualifier_clause: TsImportTypeQualifier?
	type_arguments: TsTypeArguments?

TsImportTypeAssertionList =
	(TsImportTypeAssertion (',' TsImportTypeAssertion)* ','?)

TsImportTypeAssertion =
	key: ('identifier' | 'js_string_literal')
	':'
	value: AnyJsExpression // parse_assignment_expression_or_higher

https://github.com/microsoft/TypeScript/blob/main/src/compiler/parser.ts#L4546

  • The first argument is any TS type
    • (not only a string - the "String literal expected" error comes from the typechecker)
  • The ,{assert:{ and }} are hardcoded tokens, it's not a comma separated list
  • The part inside the inner braces is a comma separated list of key:expression where key can only be an identifier or string literal, and expression is a JS expression (parseAssignmentExpressionOrHigher).
    • Trailing commas are allowed

Examples:

type A = typeof import(1); // ok (typechecker error)
type B = typeof import("a.json",{}); // syntax error - missing assert keyword
type C = typeof import("a.json",{assert:{}}); // ok
type D = typeof import("a.json",{assert:{a:"1"}}); // ok
type E = typeof import("a.json",{assert:{[a]:"1"}}); // syntax error - keys need to be identifiers or string literals
type F = typeof import("a.json",{assert:{"a":"1"}}); // ok
type G = typeof import("a.json",{assert:{a:"1", b:"2",}}); // ok
type H = typeof import("a.json",{assert:{}}, 1); // syntax error - 3rd arg not valid
type I = typeof import(import(Record<string,string>),{assert:{a:await p}}); // ok (typechecker error)

https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEBbA9sArhBAiKA6AVgM7IB2W8A3gL4BQNALgJ4AOCAgvALzxOvIBm8AJaJmyGPQAUARgCUAbngB6JfGQBreJN7gAFuHUgY8IzHGyGLBACEuPKwOGjxUnAWJkANNQXLVhRhJ6KAAPExgzYwBaJCFCQiESAHN4KHijenhDRgB3cWBLVngAYTsdRxExCUk3IlIsbzTCDIAuaipfFTV1QoQAETKHQUqXGrw6rwom1qmWrGksKg7FLo1e+ABRQb5h52rajwap9Ik2gG0oAF05haXO-0DgsNNxeBjswngSEBBgHmR4AAjBBCUBBIT8IRGT6vQj0GCJFIQIT0IxQCCEdYAMW2IAqe1c40OjRO9DaOCwN0Wyz83XWAHFcfiqoT3PUSc1TrN5g0gXMAEwNO4rVRrHTwAASTN2LLGbMm0y5S088DkIvgASCoXCkTe8AAzDA-rAUiRkJkAG7o0HrACS0qcspG1QASuB8gAeOEI5Keb2IgB8sg5MygLSgOSgKPgzGFtI0Wh0YH0YEMxheMFkQA

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😯, thank you very much to provide the typescript context, i will base this on to update my code here, I haven't seen the relevant parser specification reference for this before.

Comment on lines +1677 to +1680
// test ts ts_import_type_arguments
// type A = import("foo");
// type B = import("foo", "bar");
// type C = typeof import("foo");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We moved away from this testing. Now just create a file inside the specs and run cargo t to generate the snapshot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, I asked why I panic here when I execute cargo codegen test recently. i will update later~ now the parser js test case is align with css parser.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. The comments in the source code are still there because the PR was too big. We will have to delete them eventually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Parser Area: parser A-Tooling Area: internal tools L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📝 Redundant trailing comma on import statement
3 participants