mdx-js / mdx-analyzer

MDX extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=unifiedjs.vscode-mdx
MIT License
342 stars 21 forks source link

Support loose (invalid) JavaScript #267

Open remcohaszing opened 1 year ago

remcohaszing commented 1 year ago

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:

ChristianMurphy commented 1 year ago

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

wooorm commented 1 year ago

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

remcohaszing commented 1 year ago

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.