mdx-analyzer icon indicating copy to clipboard operation
mdx-analyzer copied to clipboard

Support loose (invalid) JavaScript

Open remcohaszing opened this issue 2 years ago • 3 comments

Initial checklist

Problem

While typing, code is often invalid. Various IntelliSense features typically work on invalid code. TypeScript supports this, but acorn does not. Currently invalid syntax is caught, but IntelliSense doesn’t work. We need to find a way to deal with invalid syntax.

Solution

Needs investigation

Alternatives

:shrug:

remcohaszing avatar Jan 13 '23 13:01 remcohaszing

Maybe https://github.com/acornjs/acorn/tree/master/acorn-loose/ would be of interest?

ChristianMurphy avatar Jan 20 '23 20:01 ChristianMurphy

the micromark utils do a low an acorn instance to be passed, so that might also indeed allow the loose version? Worth to check, but might not be strong enough

wooorm avatar Jan 20 '23 23:01 wooorm

I think acorn-loose is a good candidate, although currently it doesn’t work as a drop-in replacement, because it doesn’t implement parseExpressionAt.

import {Parser} from 'acorn'
import {LooseParser} from 'acorn-loose'
import jsx from 'acorn-jsx'
import remarkMdx from 'remark-mdx'
import remarkParse from 'remark-parse'
import {unified} from 'unified'

// const parser = Parser.extend(jsx())
const parser = LooseParser.extend(jsx())

const processor = unified().use(remarkParse).use(remarkMdx, {acorn: parser})

const mdx = `
import { Button } from './button.js'

Click the button

<Button>Click me!</Button>
`

console.dir(processor.parse(mdx))

Also worth considering is @typescript-eslint/typescript-estree, or possibly even just TypeScript, as we’re not really interested in the estree.

remcohaszing avatar Jan 22 '23 13:01 remcohaszing