|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import { analyze } from 'periscopic' |
| 7 | +import { define } from 'unist-util-mdx-define' |
7 | 8 |
|
8 | 9 | /** |
9 | 10 | * @typedef {RegExp | string | ((value: string) => boolean)} Test |
@@ -51,7 +52,7 @@ function passTest(test, value) { |
51 | 52 | */ |
52 | 53 | const recmaNextjsStaticProps = |
53 | 54 | ({ exclude, include, name = 'getStaticProps' } = {}) => |
54 | | - (ast) => { |
| 55 | + (ast, file) => { |
55 | 56 | /** |
56 | 57 | * @type {string[]} |
57 | 58 | */ |
@@ -90,78 +91,71 @@ const recmaNextjsStaticProps = |
90 | 91 | return |
91 | 92 | } |
92 | 93 |
|
93 | | - ast.body.push({ |
94 | | - type: 'ExportNamedDeclaration', |
95 | | - specifiers: [], |
96 | | - declaration: { |
97 | | - type: 'VariableDeclaration', |
98 | | - kind: 'const', |
99 | | - declarations: [ |
100 | | - { |
101 | | - type: 'VariableDeclarator', |
102 | | - id: { type: 'Identifier', name }, |
103 | | - init: { |
104 | | - type: 'ArrowFunctionExpression', |
105 | | - expression: true, |
106 | | - generator: false, |
107 | | - async: false, |
108 | | - params: [], |
109 | | - body: { |
110 | | - type: 'ObjectExpression', |
111 | | - properties: [ |
112 | | - { |
113 | | - type: 'Property', |
114 | | - method: false, |
115 | | - shorthand: false, |
| 94 | + define( |
| 95 | + ast, |
| 96 | + file, |
| 97 | + { |
| 98 | + [name]: { |
| 99 | + type: 'ArrowFunctionExpression', |
| 100 | + expression: true, |
| 101 | + generator: false, |
| 102 | + async: false, |
| 103 | + params: [], |
| 104 | + body: { |
| 105 | + type: 'ObjectExpression', |
| 106 | + properties: [ |
| 107 | + { |
| 108 | + type: 'Property', |
| 109 | + method: false, |
| 110 | + shorthand: false, |
| 111 | + computed: false, |
| 112 | + kind: 'init', |
| 113 | + key: { type: 'Identifier', name: 'props' }, |
| 114 | + value: { |
| 115 | + type: 'CallExpression', |
| 116 | + optional: false, |
| 117 | + callee: { |
| 118 | + type: 'MemberExpression', |
116 | 119 | computed: false, |
117 | | - kind: 'init', |
118 | | - key: { type: 'Identifier', name: 'props' }, |
119 | | - value: { |
| 120 | + optional: false, |
| 121 | + object: { type: 'Identifier', name: 'JSON' }, |
| 122 | + property: { type: 'Identifier', name: 'parse' } |
| 123 | + }, |
| 124 | + arguments: [ |
| 125 | + { |
120 | 126 | type: 'CallExpression', |
121 | 127 | optional: false, |
122 | 128 | callee: { |
123 | 129 | type: 'MemberExpression', |
124 | 130 | computed: false, |
125 | 131 | optional: false, |
126 | 132 | object: { type: 'Identifier', name: 'JSON' }, |
127 | | - property: { type: 'Identifier', name: 'parse' } |
| 133 | + property: { type: 'Identifier', name: 'stringify' } |
128 | 134 | }, |
129 | 135 | arguments: [ |
130 | 136 | { |
131 | | - type: 'CallExpression', |
132 | | - optional: false, |
133 | | - callee: { |
134 | | - type: 'MemberExpression', |
| 137 | + type: 'ObjectExpression', |
| 138 | + properties: identifiers.sort().map((id) => ({ |
| 139 | + type: 'Property', |
| 140 | + method: false, |
| 141 | + shorthand: true, |
135 | 142 | computed: false, |
136 | | - optional: false, |
137 | | - object: { type: 'Identifier', name: 'JSON' }, |
138 | | - property: { type: 'Identifier', name: 'stringify' } |
139 | | - }, |
140 | | - arguments: [ |
141 | | - { |
142 | | - type: 'ObjectExpression', |
143 | | - properties: identifiers.sort().map((id) => ({ |
144 | | - type: 'Property', |
145 | | - method: false, |
146 | | - shorthand: true, |
147 | | - computed: false, |
148 | | - kind: 'init', |
149 | | - key: { type: 'Identifier', name: id }, |
150 | | - value: { type: 'Identifier', name: id } |
151 | | - })) |
152 | | - } |
153 | | - ] |
| 143 | + kind: 'init', |
| 144 | + key: { type: 'Identifier', name: id }, |
| 145 | + value: { type: 'Identifier', name: id } |
| 146 | + })) |
154 | 147 | } |
155 | 148 | ] |
156 | 149 | } |
157 | | - } |
158 | | - ] |
| 150 | + ] |
| 151 | + } |
159 | 152 | } |
160 | | - } |
| 153 | + ] |
161 | 154 | } |
162 | | - ] |
163 | | - } |
164 | | - }) |
| 155 | + } |
| 156 | + }, |
| 157 | + { conflict: 'skip' } |
| 158 | + ) |
165 | 159 | } |
166 | 160 |
|
167 | 161 | export default recmaNextjsStaticProps |
0 commit comments