Skip to content

Commit 54bebbc

Browse files
committed
Replace periscopic with estree-util-scope
This gives us more control. We now don’t have to visit the entire AST for scope analysis.
1 parent b2e9706 commit 54bebbc

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

packages/language-service/lib/virtual-code.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* @import {VFileMessage} from 'vfile-message'
99
*/
1010

11+
import {createVisitors} from 'estree-util-scope'
1112
import {walk} from 'estree-walker'
12-
import {analyze} from 'periscopic'
1313
import {getNodeEndOffset, getNodeStartOffset} from './mdast-utils.js'
1414
import {ScriptSnapshot} from './script-snapshot.js'
1515
import {isInjectableComponent} from './jsx-utils.js'
@@ -367,14 +367,7 @@ function getEmbeddedCodes(mdx, ast, checkMdx, jsxImportSource) {
367367
let markdown = ''
368368
let nextMarkdownSourceStart = 0
369369

370-
/** @type {Program} */
371-
const esmProgram = {
372-
type: 'Program',
373-
sourceType: 'module',
374-
start: 0,
375-
end: 0,
376-
body: []
377-
}
370+
const visitors = createVisitors()
378371

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

386379
if (estree) {
387-
esmProgram.body.push(...estree.body)
380+
walk(estree, {
381+
enter(node) {
382+
visitors.enter(node)
383+
384+
if (
385+
node.type === 'ArrowFunctionExpression' ||
386+
node.type === 'FunctionDeclaration' ||
387+
node.type === 'FunctionExpression'
388+
) {
389+
this.skip()
390+
visitors.exit(node)
391+
}
392+
},
393+
leave: visitors.exit
394+
})
388395
}
389396
}
390397

391-
const variables = [...analyze(esmProgram).scope.declarations.keys()].sort()
398+
const variables = [...visitors.scopes[0].defined].sort()
392399

393400
/**
394401
* Update the **markdown** mappings from a start and end offset of a **JavaScript** chunk.

packages/language-service/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
"@types/mdast": "^4.0.0",
4040
"@types/unist": "^3.0.0",
4141
"@volar/language-service": "~2.4.0",
42+
"estree-util-scope": "^1.0.0",
4243
"estree-walker": "^3.0.0",
4344
"mdast-util-mdxjs-esm": "^2.0.0",
4445
"mdast-util-to-markdown": "^2.0.0",
45-
"periscopic": "^3.0.0",
4646
"remark-mdx": "^3.0.0",
4747
"remark-parse": "^11.0.0",
4848
"unified": "^11.0.0",

0 commit comments

Comments
 (0)