marko icon indicating copy to clipboard operation
marko copied to clipboard

Cannot break generic parameter declarations into multiple lines

Open svallory opened this issue 1 year ago • 3 comments

Marko Version: 5.32.7

VS Code Extension Version: 1.1.18

Details

When declaring an Input type with generic parameters, breaking the generic parameters into multiple lines breaks the compiler

Expected Behavior

image

Actual Behavior

One generic param

export interface Input<
  A
> {}

image

image

$ mtc          
src/components/generics-bug.marko - error TS1005
',' expected.

src/components/generics-bug.marko - error TS1359
Identifier expected. 'class' is a reserved word that cannot be used here.

Two generic params of one with trailing comma

export interface Input<
  A,
  B
> {}

or

export interface Input<
  A,
> {}

image

image

 npm run build

> @tokilabs/[email protected] build
> npm run clean && mtc


> @tokilabs/[email protected] clean
> rm -rf coverage dist node_modules/.{cache,vite,vitest}

src/components/generics-bug.marko - error TS1359
Identifier expected. 'class' is a reserved word that cannot be used here.
Additional Info

Your Environment

VS Code on Mac

Steps to Reproduce

Create an Input type with at least one generic parameter declaring each parameter in its own line

Stack Trace

svallory avatar Jan 29 '24 21:01 svallory

My understanding is that this is a limitation/issue in the htmljs parser. There is no native representation of import/export in the parser, they are parsed as concise mode tags.

One solution would be to give the htmljs-parser the ability to parse import and export statements natively. There may be an easier fix, but I don't have a good enough understanding of the htmljs-parser to know!

AngusMorton avatar Feb 06 '24 10:02 AngusMorton

@AngusMorton @svallory the htmljs parser tries hard not to do a full parse of the javascript sections of code. For import, export and friends they are parsed in "statement" mode (previously it was parsed as a concise mode tag but now it goes directly into our expression parsing similar to attribute values).

When the parser is scanning for the end of an expression it will skip over strings, matched bracket pairs, regexps and some javascript operators and keywords. The problem is that this logic does not currently account for generics and since the statement parse state exits when there is a newline (assuming the expression isn't continued because of being a string, bracketed, etc) it will error when you have multiline generics outside of a bracketed section.

We need to think of a good way to make the parser aware of generics, while still allowing it to do as little processing as possible.

DylanPiercey avatar Feb 06 '24 15:02 DylanPiercey

@DylanPiercey I don't see many options other balancing < in htmljs like it does for other tokens in EXPRESSION.ts. The trick part will be differentiating a "less than" from a generic section start.

svallory avatar Feb 10 '24 22:02 svallory