MCPcopy Index your code
hub / github.com/adobe/react-spectrum / processJS

Function processJS

scripts/extractStarter.mjs:142–266  ·  view source on GitHub ↗

Processes the JS code for a "Reusable wrappers" example from the docs.

(sourceFilename, code, usedClasses)

Source from the content-addressed store, hash-verified

140
141/** Processes the JS code for a "Reusable wrappers" example from the docs. */
142function processJS(sourceFilename, code, usedClasses) {
143 if (/^<(.|\n)*>$/m.test(code)) {
144 code = code.replace(/^(<(.|\n)*>)$/m, '').trim() + '\n';
145 }
146
147 let ast = recast.parse(code, {
148 parser: {
149 parse() {
150 return parse(code, {
151 sourceFilename,
152 sourceType: 'module',
153 plugins: ['jsx', 'typescript'],
154 tokens: true,
155 errorRecovery: true
156 });
157 }
158 }
159 });
160
161 removeDuplicateImports(ast);
162
163 // Remove unused imports, add exports.
164 let imports = new Map();
165 let component = basename(sourceFilename, '.mdx');
166 let importsToAppend = [t.importDeclaration([], t.stringLiteral(`./${component}.css`))];
167 traverse.default(ast, {
168 'ImportSpecifier|ImportDefaultSpecifier'(specifier) {
169 let binding = specifier.scope.getBinding(specifier.node.local.name);
170 if (binding?.referencePaths.length === 0) {
171 specifier.remove();
172 }
173 },
174 ImportDeclaration: {
175 exit(path) {
176 if (path.node.source.value.endsWith('.css')) {
177 return;
178 }
179
180 if (path.node.specifiers.length === 0) {
181 path.remove();
182 } else if (imports.has(path.node.source.value)) {
183 // merge imports with the same source
184 imports.get(path.node.source.value).pushContainer('specifiers', path.node.specifiers);
185 path.remove();
186 path.scope.crawl();
187 } else {
188 imports.set(path.node.source.value, path);
189 }
190 }
191 },
192 'FunctionDeclaration|TSInterfaceDeclaration': {
193 exit(path) {
194 // GridList/Table define their own checkboxes, but we can share the one from the Checkbox docs.
195 if (path.node.id.name === 'MyCheckbox' && component !== 'Checkbox') {
196 path.scope.getBinding('Checkbox')?.path.remove();
197 path.scope.getBinding('CheckboxProps')?.path.remove();
198 path.scope.rename('MyCheckbox', 'Checkbox');
199 if (path.scope.getBinding('Checkbox').referencePaths.length > 0) {

Callers 1

extractStarter.mjsFile · 0.85

Calls 3

removeDuplicateImportsFunction · 0.85
parseMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected