|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import fs from "fs"; |
| 5 | +import { kebabToCamelCase } from "./utils/kebabToKamelCase"; |
| 6 | + |
| 7 | +function transformer(file, api, options) { |
| 8 | + const j = api.jscodeshift; |
| 9 | + const { ruleName, exportIndexFilePath } = options; |
| 10 | + |
| 11 | + // Read the export index file content |
| 12 | + const exportIndexSource = j(fs.readFileSync(exportIndexFilePath, "utf8")); |
| 13 | + |
| 14 | + // Convert the rule name to camelCase for the export statement |
| 15 | + const exportRuleName = kebabToCamelCase(ruleName); |
| 16 | + |
| 17 | + // Validate ruleName and exportRuleName |
| 18 | + if (!ruleName || !exportRuleName) { |
| 19 | + throw new Error(`Invalid rule name or export rule name: ${ruleName}, ${exportRuleName}`); |
| 20 | + } |
| 21 | + |
| 22 | + // Create the new export specifier |
| 23 | + const specifier = j.exportSpecifier.from({ |
| 24 | + exported: j.identifier(exportRuleName), |
| 25 | + local: j.identifier("default") |
| 26 | + }); |
| 27 | + |
| 28 | + // Create the new export statement |
| 29 | + const newExportStatement = j.exportNamedDeclaration(null, [specifier], j.stringLiteral(`./${ruleName}`)); |
| 30 | + |
| 31 | + // Find all export statements |
| 32 | + const exportStatements = exportIndexSource.find(j.ExportNamedDeclaration); |
| 33 | + |
| 34 | + if (exportStatements.size() === 0) { |
| 35 | + // No export statements found, so insert at the beginning of the file |
| 36 | + exportIndexSource.get().node.program.body.unshift(newExportStatement); |
| 37 | + } else { |
| 38 | + // Insert the new export statement after the last one |
| 39 | + const lastExportStatement = exportStatements.paths()[exportStatements.size() - 1]; |
| 40 | + j(lastExportStatement).insertAfter(newExportStatement); |
| 41 | + |
| 42 | + // Re-query the export statements after the insertion |
| 43 | + const updatedExportStatements = exportIndexSource.find(j.ExportNamedDeclaration); |
| 44 | + |
| 45 | + // Manually sort the export statements alphabetically |
| 46 | + const sortedExports = updatedExportStatements |
| 47 | + .nodes() |
| 48 | + .map(node => { |
| 49 | + if (node.specifiers && node.specifiers[0] && node.specifiers[0].exported) { |
| 50 | + return node; |
| 51 | + } |
| 52 | + return null; // Ignore nodes without valid specifiers |
| 53 | + }) |
| 54 | + .filter(node => node !== null) // Remove nulls |
| 55 | + .sort((a, b) => { |
| 56 | + const aName = a.specifiers[0].exported.name; |
| 57 | + const bName = b.specifiers[0].exported.name; |
| 58 | + return aName.localeCompare(bName); |
| 59 | + }); |
| 60 | + |
| 61 | + // Remove all the original export statements |
| 62 | + exportIndexSource.find(j.ExportNamedDeclaration).remove(); |
| 63 | + |
| 64 | + // Now insert the sorted export statements back into the AST |
| 65 | + const body = exportIndexSource.get().node.program.body; |
| 66 | + sortedExports.forEach(exportNode => { |
| 67 | + body.push(exportNode); // Insert each export statement at the end of the body |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + // Write the modified index file back to the filesystem |
| 72 | + fs.writeFileSync(exportIndexFilePath, exportIndexSource.toSource({ quote: "double" }), "utf8"); |
| 73 | + |
| 74 | + // Return the original file source (this is for the main file passed in) |
| 75 | + return file.source; |
| 76 | +} |
| 77 | +module.exports = transformer; |
0 commit comments