Skip to content

Commit 2b4c21c

Browse files
committed
Replace periscopic with estree-util-scope
periscopic is no longer maintained.
1 parent 6887ea9 commit 2b4c21c

File tree

3 files changed

+31
-44
lines changed

3 files changed

+31
-44
lines changed

lib/recma-nextjs-static-props.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* @import { Plugin } from 'unified'
44
*/
55

6-
import { analyze } from 'periscopic'
6+
import { createVisitors } from 'estree-util-scope'
7+
import { walk } from 'estree-walker'
78
import { define } from 'unist-util-mdx-define'
89

910
/**
@@ -53,39 +54,43 @@ function passTest(test, value) {
5354
const recmaNextjsStaticProps =
5455
({ exclude, include, name = 'getStaticProps' } = {}) =>
5556
(ast, file) => {
56-
/**
57-
* @type {string[]}
58-
*/
59-
const identifiers = []
60-
const { scope } = analyze(ast)
57+
const visitors = createVisitors()
6158

62-
for (const [id, node] of scope.declarations) {
63-
if (id === name) {
64-
return
65-
}
59+
walk(ast, {
60+
enter(node) {
61+
if (
62+
node.type === 'ArrowFunctionExpression' ||
63+
node.type === 'ClassDeclaration' ||
64+
node.type === 'ClassExpression' ||
65+
node.type === 'FunctionDeclaration' ||
66+
node.type === 'FunctionExpression'
67+
) {
68+
this.skip()
69+
return
70+
}
71+
72+
visitors.enter(node)
73+
},
6674

75+
leave: visitors.exit
76+
})
77+
78+
const identifiers = visitors.scopes[0].defined.filter((id) => {
6779
// Always filter JSX functions generated by MDX.
6880
if (id === '_jsx' || id === '_jsxs' || id === '_Fragment') {
69-
continue
81+
return false
7082
}
7183

7284
if (exclude && exclude.some((test) => passTest(test, id))) {
73-
continue
85+
return false
7486
}
7587

7688
if (include && !include.some((test) => passTest(test, id))) {
77-
continue
89+
return false
7890
}
7991

80-
// We’re not insterested in for example function or class declarations.
81-
if (
82-
node.type === 'ImportDefaultSpecifier' ||
83-
node.type === 'ImportSpecifier' ||
84-
node.type === 'VariableDeclaration'
85-
) {
86-
identifiers.push(id)
87-
}
88-
}
92+
return true
93+
})
8994

9095
if (!identifiers.length) {
9196
return

package-lock.json

Lines changed: 2 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
},
3535
"dependencies": {
3636
"@types/estree": "^1.0.0",
37-
"periscopic": "^3.0.0",
37+
"estree-util-scope": "^1.0.0",
38+
"estree-walker": "^3.0.0",
3839
"unified": "^11.0.0",
3940
"unist-util-mdx-define": "^1.0.0"
4041
},

0 commit comments

Comments
 (0)