UNPKG

sortier

Version:
177 lines (176 loc) 8.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Reprinter = void 0; // Parsers const index_js_1 = require("../parsers/flow/index.js"); const index_js_2 = require("../parsers/typescript/index.js"); // Types of sorts const index_js_3 = require("../sortContents/index.js"); const index_js_4 = require("../sortImportDeclarationSpecifiers/index.js"); const index_js_5 = require("../sortImportDeclarations/index.js"); const index_js_6 = require("../sortJsxElement/index.js"); const index_js_7 = require("../sortObjectTypeAnnotation/index.js"); const index_js_8 = require("../sortSwitchCases/index.js"); const index_js_9 = require("../sortTSPropertySignatures/index.js"); const index_js_10 = require("../sortUnionTypeAnnotation/index.js"); const array_utils_js_1 = require("../../utilities/array-utils.js"); const log_utils_js_1 = require("../../utilities/log-utils.js"); const sort_utils_js_1 = require("../../utilities/sort-utils.js"); const string_utils_js_1 = require("../../utilities/string-utils.js"); class Reprinter { static EXTENSIONS = [".cjs", ".js", ".js.txt", ".jsx", ".mjs", ".ts", ".tsx", ".ts.txt"]; // @ts-expect-error: Need to move to a functional system _filename; // @ts-expect-error: Need to move to a functional system _helpModeHasPrintedFilename; // @ts-expect-error: Need to move to a functional system _options; getRewrittenContents(filename, fileContents, options) { this._filename = filename; this._options = this.getValidatedOptions(options); this._helpModeHasPrintedFilename = false; const parser = this.getParser(); const ast = parser(fileContents); const comments = ast.comments; return this.rewriteNodes([ast], comments, fileContents); } isFileSupported(filename) { return string_utils_js_1.StringUtils.stringEndsWithAny(filename, [...Reprinter.EXTENSIONS]); } getValidatedOptions(appOptions) { const partialOptions = appOptions.js || {}; let sortTypeAnnotations = undefined; if (partialOptions.sortTypeAnnotations != null) { sortTypeAnnotations = partialOptions.sortTypeAnnotations.slice(); array_utils_js_1.ArrayUtils.dedupe(sortTypeAnnotations); } return { ...partialOptions, sortTypeAnnotations, }; } getParser() { // If the options overide the parser type if (this._options.parser === "flow") { return index_js_1.parse; } // Make sure typescript can handle the extension const isSupportedFileExtension = string_utils_js_1.StringUtils.stringEndsWithAny(this._filename, Reprinter.EXTENSIONS); if (isSupportedFileExtension) { return index_js_2.parse; } throw new Error("File not supported"); } rewriteNodes(originalNodes, comments, fileContents) { let lastClassName = undefined; const nodes = originalNodes.slice(); while (nodes.length !== 0) { const node = nodes.shift(); if (Array.isArray(node)) { throw new Error("Unexpected Exception - Array sent as node in rewrite nodes"); } if (node == null) { throw new Error("Unexpected Exception - Node received is null"); } if ((0, sort_utils_js_1.isIgnored)(fileContents, comments, node)) { continue; } // Now go through and push any bodies in the current context to the stack try { const nodeValues = Object.values(node); for (const value of nodeValues) { const unFilteredPropertyArray = Array.isArray(value) ? value : [value]; const actionablePropertyArray = unFilteredPropertyArray.filter((value) => { return value != null && value.type != null; }); if (actionablePropertyArray.length === 0) { continue; } fileContents = this.rewriteNodes(actionablePropertyArray, comments, fileContents); } switch (node.type) { case "ClassBody": { const sortContentsOptions = this._options.sortContents; if (sortContentsOptions != null) { fileContents = (0, index_js_3.sortContents)(lastClassName, node.body, comments, fileContents, sortContentsOptions); } break; } case "ClassDeclaration": case "ClassExpression": { if (node.id != null) { lastClassName = node.id.name; } break; } case "ExportNamedDeclaration": case "ImportDeclaration": { if (node.specifiers.length > 1) { fileContents = (0, index_js_4.sortImportDeclarationSpecifiers)(node.specifiers, comments, fileContents, this._options.sortImportDeclarationSpecifiers); } break; } case "IntersectionTypeAnnotation": case "TSIntersectionType": case "TSUnionType": case "UnionTypeAnnotation": { fileContents = (0, index_js_10.sortUnionTypeAnnotation)(node, comments, fileContents, { groups: this._options.sortTypeAnnotations, }); break; } case "JSXElement": case "JSXFragment": case "JSXOpeningElement": { fileContents = (0, index_js_6.sortJsxElement)(node, comments, fileContents); break; } case "ObjectExpression": case "ObjectPattern": case "ObjectTypeAnnotation": { fileContents = (0, index_js_7.sortObjectTypeAnnotation)(node, comments, fileContents, { groups: this._options.sortTypeAnnotations, }); break; } case "Program": { fileContents = (0, index_js_5.sortImportDeclarations)(node, comments, fileContents, this._options.sortImportDeclarations && { orderBy: this._options.sortImportDeclarations, }); break; } case "SwitchStatement": { fileContents = (0, index_js_8.sortSwitchCases)(node.cases, comments, fileContents); break; } case "TSInterfaceBody": { fileContents = (0, index_js_9.sortTSPropertySignatures)(node.body, comments, fileContents, { groups: this._options.sortTypeAnnotations, }); break; } case "TSTypeLiteral": { fileContents = (0, index_js_9.sortTSPropertySignatures)(node.members, comments, fileContents, { groups: this._options.sortTypeAnnotations, }); break; } } } catch (e) { this.printHelpModeInfo(node, fileContents); throw e; } } return fileContents; } printHelpModeInfo(item, fileContents) { if (!this._helpModeHasPrintedFilename) { log_utils_js_1.LogUtils.log(log_utils_js_1.LoggerVerboseOption.Diagnostic, ""); log_utils_js_1.LogUtils.log(log_utils_js_1.LoggerVerboseOption.Diagnostic, this._filename); } log_utils_js_1.LogUtils.log(log_utils_js_1.LoggerVerboseOption.Diagnostic, ` - ${item.type} - ${JSON.stringify(item.loc.start)} - ${JSON.stringify(item.loc.end)}`); log_utils_js_1.LogUtils.log(log_utils_js_1.LoggerVerboseOption.Diagnostic, fileContents.substring(item.range[0], item.range[1])); } } exports.Reprinter = Reprinter;