UNPKG

refakts

Version:

TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.

116 lines 4.85 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.RegexPatternMatcher = void 0; const pattern_matcher_1 = require("./selection/pattern-matcher"); const result_formatters_1 = require("./selection/result-formatters"); const path = __importStar(require("path")); class RegexPatternMatcher { constructor() { this.matcher = new pattern_matcher_1.SelectPatternMatcher(); this.formatters = new Map([ ['basic', new result_formatters_1.BasicFormatter()], ['line', new result_formatters_1.LineFormatter()], ['preview', new result_formatters_1.PreviewFormatter()], ['definition', new result_formatters_1.DefinitionFormatter()] ]); } findRegexMatches(sourceFile, options) { const content = sourceFile.getFullText(); const fileName = path.basename(sourceFile.getFilePath()); const patterns = this.createRegexPatterns(options.regex || ''); const allMatches = this.findAllPatternMatches(content, patterns); return this.processMatches(allMatches, fileName, sourceFile.getFilePath(), options); } createRegexPatterns(regexOption) { const regexStrings = Array.isArray(regexOption) ? regexOption : [regexOption]; return regexStrings.map(regex => new RegExp(regex, 'g')); } findAllPatternMatches(content, patterns) { const allMatches = []; for (const pattern of patterns) { const matches = this.matcher.findMatches(content, pattern); allMatches.push(...matches); } return this.sortMatchesByPosition(allMatches); } sortMatchesByPosition(matches) { return matches.sort((a, b) => { if (a.line !== b.line) { return a.line - b.line; } return a.column - b.column; }); } processMatches(matches, fileName, filePath, options) { const formatter = this.determineFormatter(options); return formatter.format(matches, fileName, filePath); } determineFormatter(options) { const formatterType = this.getFormatterType(options); const formatter = this.formatters.get(formatterType); if (!formatter) { throw new Error(`Unknown formatter type: ${formatterType}`); } return formatter; } getFormatterType(options) { const optionChecks = this.createOptionChecks(options); const found = optionChecks.find(option => option.check); return found ? found.type : 'basic'; } createOptionChecks(options) { return [ { check: this.hasDefinitionOption(options), type: 'definition' }, { check: this.hasLineOption(options), type: 'line' }, { check: this.hasPreviewMatchOption(options), type: 'preview' }, { check: this.hasPreviewOption(options), type: 'preview' } ]; } hasDefinitionOption(options) { return Boolean(options.includeDefinition || options['include-definition']); } hasLineOption(options) { return Boolean(options.includeLine || options['include-line']); } hasPreviewOption(options) { return Boolean(options.previewLine || options['preview-line']); } hasPreviewMatchOption(options) { return Boolean(options.previewMatch || options['preview-match']); } } exports.RegexPatternMatcher = RegexPatternMatcher; //# sourceMappingURL=regex-pattern-matcher.js.map