Skip to content

Commit

Permalink
Replace periscopic with estree-util-scope
Browse files Browse the repository at this point in the history
This gives us more control. We now don’t have to visit the entire AST
for scope analysis.
  • Loading branch information
remcohaszing committed Dec 23, 2024
1 parent b2e9706 commit 54bebbc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
29 changes: 18 additions & 11 deletions packages/language-service/lib/virtual-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* @import {VFileMessage} from 'vfile-message'
*/

import {createVisitors} from 'estree-util-scope'
import {walk} from 'estree-walker'
import {analyze} from 'periscopic'
import {getNodeEndOffset, getNodeStartOffset} from './mdast-utils.js'
import {ScriptSnapshot} from './script-snapshot.js'
import {isInjectableComponent} from './jsx-utils.js'
Expand Down Expand Up @@ -367,14 +367,7 @@ function getEmbeddedCodes(mdx, ast, checkMdx, jsxImportSource) {
let markdown = ''
let nextMarkdownSourceStart = 0

/** @type {Program} */
const esmProgram = {
type: 'Program',
sourceType: 'module',
start: 0,
end: 0,
body: []
}
const visitors = createVisitors()

for (const child of ast.children) {
if (child.type !== 'mdxjsEsm') {
Expand All @@ -384,11 +377,25 @@ function getEmbeddedCodes(mdx, ast, checkMdx, jsxImportSource) {
const estree = child.data?.estree

if (estree) {
esmProgram.body.push(...estree.body)
walk(estree, {
enter(node) {
visitors.enter(node)

if (
node.type === 'ArrowFunctionExpression' ||
node.type === 'FunctionDeclaration' ||
node.type === 'FunctionExpression'
) {
this.skip()
visitors.exit(node)
}
},
leave: visitors.exit
})
}
}

const variables = [...analyze(esmProgram).scope.declarations.keys()].sort()
const variables = [...visitors.scopes[0].defined].sort()

/**
* Update the **markdown** mappings from a start and end offset of a **JavaScript** chunk.
Expand Down
2 changes: 1 addition & 1 deletion packages/language-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
"@types/mdast": "^4.0.0",
"@types/unist": "^3.0.0",
"@volar/language-service": "~2.4.0",
"estree-util-scope": "^1.0.0",
"estree-walker": "^3.0.0",
"mdast-util-mdxjs-esm": "^2.0.0",
"mdast-util-to-markdown": "^2.0.0",
"periscopic": "^3.0.0",
"remark-mdx": "^3.0.0",
"remark-parse": "^11.0.0",
"unified": "^11.0.0",
Expand Down

0 comments on commit 54bebbc

Please sign in to comment.