|
| 1 | +/* eslint-disable @typescript-eslint/prefer-regexp-exec */ |
| 2 | +import * as vsc from 'vscode'; |
| 3 | +import { |
| 4 | + checkGateReplacement, |
| 5 | + getConfigReplacement, |
| 6 | + getExperimentReplacement, |
| 7 | + getStatsigAPICheckGateRegex, |
| 8 | + getStatsigAPIGetConfigRegex, |
| 9 | + getStatsigAPIGetExperimentRegex, |
| 10 | + getVariableAssignmentRegex, |
| 11 | + isLanguageSupported, |
| 12 | + nthIndexOf, |
| 13 | + SupportedLanguageType, |
| 14 | + SUPPORTED_FILE_EXTENSIONS, |
| 15 | +} from '../lib/languageUtils'; |
| 16 | +import { DiagnosticCode, findStaleConfigs } from './diagnostics'; |
| 17 | + |
| 18 | +type CodeActionType = { |
| 19 | + command: string; |
| 20 | + title: string; |
| 21 | + kind: vsc.CodeActionKind; |
| 22 | +}; |
| 23 | + |
| 24 | +export const CODE_ACTIONS: Record<string, CodeActionType> = { |
| 25 | + removeStaleConfig: { |
| 26 | + command: 'remove-stale-config', |
| 27 | + title: 'Cleanup', |
| 28 | + kind: vsc.CodeActionKind.QuickFix, |
| 29 | + }, |
| 30 | +}; |
| 31 | + |
| 32 | +export class ConfigCodeActionProvider implements vsc.CodeActionProvider { |
| 33 | + public static readonly providedCodeActionKinds = [ |
| 34 | + vsc.CodeActionKind.QuickFix, |
| 35 | + ]; |
| 36 | + |
| 37 | + provideCodeActions( |
| 38 | + doc: vsc.TextDocument, |
| 39 | + range: vsc.Range | vsc.Selection, |
| 40 | + context: vsc.CodeActionContext, |
| 41 | + _token: vsc.CancellationToken, |
| 42 | + ): vsc.ProviderResult<(vsc.CodeAction | vsc.Command)[]> { |
| 43 | + if (!isLanguageSupported(doc.languageId)) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + const actions: vsc.CodeAction[] = []; |
| 48 | + context.diagnostics |
| 49 | + .filter((diagnostic) => diagnostic.code === DiagnosticCode.staleCheck) |
| 50 | + .forEach((diagnostic) => { |
| 51 | + actions.push( |
| 52 | + this.newCodeAction(doc, range, diagnostic, { |
| 53 | + ...CODE_ACTIONS.removeStaleConfig, |
| 54 | + }), |
| 55 | + ); |
| 56 | + }); |
| 57 | + return actions; |
| 58 | + } |
| 59 | + |
| 60 | + private newCodeAction( |
| 61 | + doc: vsc.TextDocument, |
| 62 | + range: vsc.Range | vsc.Selection, |
| 63 | + diagnostic: vsc.Diagnostic, |
| 64 | + type: CodeActionType, |
| 65 | + ): vsc.CodeAction { |
| 66 | + const action = new vsc.CodeAction(type.title, type.kind); |
| 67 | + const edit = new vsc.WorkspaceEdit(); |
| 68 | + const configName = doc.getText(range); |
| 69 | + const success = ConfigCodeActionProvider.handleEdit( |
| 70 | + doc, |
| 71 | + edit, |
| 72 | + [configName], |
| 73 | + range, |
| 74 | + ); |
| 75 | + action.edit = edit; |
| 76 | + action.command = { |
| 77 | + command: type.command, |
| 78 | + title: action.title, |
| 79 | + arguments: [configName, success], |
| 80 | + }; |
| 81 | + action.diagnostics = [diagnostic]; |
| 82 | + action.isPreferred = true; |
| 83 | + return action; |
| 84 | + } |
| 85 | + |
| 86 | + public static handleEdit( |
| 87 | + doc: vsc.TextDocument, |
| 88 | + edit: vsc.WorkspaceEdit, |
| 89 | + configs: string[], |
| 90 | + range?: vsc.Range | vsc.Selection, |
| 91 | + ): boolean { |
| 92 | + let changesMade = false; |
| 93 | + let searchableText = doc.getText(); |
| 94 | + let offset = 0; |
| 95 | + if (range) { |
| 96 | + const line = doc.lineAt(range.start.line); |
| 97 | + searchableText = line.text; |
| 98 | + offset = doc.offsetAt(line.range.start); |
| 99 | + } |
| 100 | + const language = doc.languageId as SupportedLanguageType; |
| 101 | + const seen: { [key: string]: number } = {}; |
| 102 | + |
| 103 | + const uniqueConfigs = new Set(configs); |
| 104 | + uniqueConfigs.forEach((config) => { |
| 105 | + const addReplacementEdit = ( |
| 106 | + matches: RegExpMatchArray | null, |
| 107 | + replacement: string, |
| 108 | + ): void => { |
| 109 | + if (!matches) { |
| 110 | + return; |
| 111 | + } |
| 112 | + for (const match of matches) { |
| 113 | + seen[match] ? ++seen[match] : (seen[match] = 1); |
| 114 | + const matchIndex = |
| 115 | + nthIndexOf(searchableText, match, seen[match]) + offset; |
| 116 | + const matchRange = new vsc.Range( |
| 117 | + doc.positionAt(matchIndex), |
| 118 | + doc.positionAt(matchIndex + match.length), |
| 119 | + ); |
| 120 | + edit.replace(doc.uri, matchRange, replacement); |
| 121 | + changesMade = true; |
| 122 | + } |
| 123 | + }; |
| 124 | + const checkGateMatch = searchableText.match( |
| 125 | + getStatsigAPICheckGateRegex(language, config), |
| 126 | + ); |
| 127 | + addReplacementEdit(checkGateMatch, checkGateReplacement(language)); |
| 128 | + |
| 129 | + const getConfigMatch = searchableText.match( |
| 130 | + getStatsigAPIGetConfigRegex(language, config), |
| 131 | + ); |
| 132 | + addReplacementEdit(getConfigMatch, getConfigReplacement(language)); |
| 133 | + |
| 134 | + const getExperimentMatch = searchableText.match( |
| 135 | + getStatsigAPIGetExperimentRegex(language, config), |
| 136 | + ); |
| 137 | + addReplacementEdit( |
| 138 | + getExperimentMatch, |
| 139 | + getExperimentReplacement(language), |
| 140 | + ); |
| 141 | + |
| 142 | + const variableAssignmentMatch = searchableText.match( |
| 143 | + getVariableAssignmentRegex(language, config), |
| 144 | + ); |
| 145 | + if (variableAssignmentMatch) { |
| 146 | + for (const match of variableAssignmentMatch) { |
| 147 | + seen[match] ? ++seen[match] : (seen[match] = 1); |
| 148 | + const matchIndex = |
| 149 | + nthIndexOf(searchableText, match, seen[match]) + offset; |
| 150 | + const line = doc.lineAt(doc.positionAt(matchIndex).line); |
| 151 | + edit.delete(doc.uri, line.rangeIncludingLineBreak); |
| 152 | + changesMade = true; |
| 153 | + } |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + return changesMade; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +const removeStaleConfigCommandHandler = (config: string, success: boolean) => { |
| 162 | + if (success) { |
| 163 | + void vsc.window.showInformationMessage(`Removed ${config}`); |
| 164 | + } else { |
| 165 | + void vsc.window.showInformationMessage(`Could not remove ${config}`); |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +const removeAllStaleConfgsCommandHandler = async () => { |
| 170 | + const output = vsc.window.createOutputChannel('Statsig output'); |
| 171 | + const edit = new vsc.WorkspaceEdit(); |
| 172 | + const files = await vsc.workspace.findFiles( |
| 173 | + `**/*.{${SUPPORTED_FILE_EXTENSIONS.join()}}`, |
| 174 | + ); |
| 175 | + const cleanedFiles = []; |
| 176 | + const configCounts: { [key: string]: number } = {}; |
| 177 | + for (const file of files) { |
| 178 | + const doc = await vsc.workspace.openTextDocument(file); |
| 179 | + const staleConfigs = findStaleConfigs(doc); |
| 180 | + if (staleConfigs.length === 0) { |
| 181 | + continue; |
| 182 | + } |
| 183 | + if (ConfigCodeActionProvider.handleEdit(doc, edit, staleConfigs)) { |
| 184 | + staleConfigs.forEach((config) => { |
| 185 | + configCounts[config] |
| 186 | + ? ++configCounts[config] |
| 187 | + : (configCounts[config] = 1); |
| 188 | + }); |
| 189 | + cleanedFiles.push(file.path); |
| 190 | + output.appendLine(`(success) ${file.path}`); |
| 191 | + } else { |
| 192 | + output.appendLine(`(failed) ${file.path}`); |
| 193 | + } |
| 194 | + } |
| 195 | + output.appendLine(''); |
| 196 | + output.appendLine('Summary of Instances Removed: '); |
| 197 | + Object.entries(configCounts) |
| 198 | + .sort((a, b) => b[1] - a[1]) |
| 199 | + .forEach((counts) => { |
| 200 | + output.appendLine(`${counts[0]}: ${counts[1]}`); |
| 201 | + }); |
| 202 | + const success = await vsc.workspace.applyEdit(edit); |
| 203 | + await vsc.workspace.saveAll(); |
| 204 | + output.show(); |
| 205 | + if (success) { |
| 206 | + await vsc.window.showInformationMessage( |
| 207 | + `Cleaned up ${cleanedFiles.length} ${ |
| 208 | + cleanedFiles.length === 1 ? 'file' : 'files' |
| 209 | + }`, |
| 210 | + ); |
| 211 | + } else { |
| 212 | + await vsc.window.showInformationMessage(`Something went wrong`); |
| 213 | + } |
| 214 | +}; |
| 215 | + |
| 216 | +export function registerCommands(context: vsc.ExtensionContext): void { |
| 217 | + context.subscriptions.push( |
| 218 | + vsc.commands.registerCommand( |
| 219 | + CODE_ACTIONS.removeStaleConfig.command, |
| 220 | + removeStaleConfigCommandHandler, |
| 221 | + ), |
| 222 | + vsc.commands.registerCommand( |
| 223 | + 'statsig.cleanupStale', |
| 224 | + removeAllStaleConfgsCommandHandler, |
| 225 | + ), |
| 226 | + ); |
| 227 | +} |
0 commit comments