Skip to content

Commit

Permalink
fix(core): section code fix not working
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Dec 2, 2024
1 parent 3752318 commit 19233b5
Showing 1 changed file with 44 additions and 61 deletions.
105 changes: 44 additions & 61 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ export function createLinter(ctx: ProjectContext, config: Config | Config[], wit
}
const ts = ctx.typescript;
const fileRules = new Map<string, Rules>();
const fileFixes = new Map<string, Map<string, {
diagnostic: ts.DiagnosticWithLocation;
actions: {
const fileFixes = new Map<
string /* fileName */,
Map<ts.DiagnosticWithLocation, {
title: string;
getEdits: () => ts.FileTextChanges[];
}[];
}>>();
const fileRefactors: typeof fileFixes = new Map();
}[]>
>();
const fileRefactors = new Map<
string /* fileName */,
{
title: string;
diagnostic: ts.DiagnosticWithLocation;
getEdits: () => ts.FileTextChanges[];
}[]
>();
const sourceFiles = new Map<string, [boolean, ts.SourceFile]>();
const basePath = path.dirname(ctx.configFile);
const configs = (Array.isArray(config) ? config : [config])
Expand Down Expand Up @@ -107,7 +114,7 @@ export function createLinter(ctx: ProjectContext, config: Config | Config[], wit
let currentRefactors = 0;

fixes.clear();
refactors.clear();
refactors.length = 0;

if (debugInfo) {
debugInfo.messageText += '- Rules:\n';
Expand Down Expand Up @@ -168,22 +175,12 @@ export function createLinter(ctx: ProjectContext, config: Config | Config[], wit

const diagnosticSet = new Set(diagnostics);

for (const [ruleId, { diagnostic, actions }] of [...fixes]) {
if (actions.length && diagnosticSet.has(diagnostic)) {
fixes.set(ruleId, { diagnostic, actions });
}
else {
fixes.delete(ruleId);
}
}
for (const [ruleId, { diagnostic, actions }] of [...refactors]) {
if (actions.length && diagnosticSet.has(diagnostic)) {
fixes.set(ruleId, { diagnostic, actions });
}
else {
refactors.delete(ruleId);
for (const diagnostic of [...fixes.keys()]) {
if (!diagnosticSet.has(diagnostic)) {
fixes.delete(diagnostic);
}
}
fileRefactors.set(fileName, refactors.filter(refactor => diagnosticSet.has(refactor.diagnostic)));

return diagnostics;

Expand Down Expand Up @@ -237,27 +234,16 @@ export function createLinter(ctx: ProjectContext, config: Config | Config[], wit
},
withFix(title, getEdits) {
currentFixes++;
if (!fixes.has(currentRuleId)) {
fixes.set(currentRuleId, {
diagnostic: error,
actions: [],
});
if (!fixes.has(error)) {
fixes.set(error, []);
}
fixes.get(currentRuleId)!.actions.push(({
title,
getEdits,
}));
fixes.get(error)!.push(({ title, getEdits }));
return this;
},
withRefactor(title, getEdits) {
currentRefactors++;
if (!refactors.has(currentRuleId)) {
refactors.set(currentRuleId, {
diagnostic: error,
actions: [],
});
}
refactors.get(currentRuleId)!.actions.push(({
refactors.push(({
diagnostic: error,
title,
getEdits,
}));
Expand Down Expand Up @@ -310,7 +296,7 @@ export function createLinter(ctx: ProjectContext, config: Config | Config[], wit

const fixesMap = getFileFixes(fileName);

for (const [_ruleId, { actions }] of fixesMap) {
for (const [_diagnostic, actions] of fixesMap) {
if (actions.length) {
return true;
}
Expand All @@ -323,7 +309,7 @@ export function createLinter(ctx: ProjectContext, config: Config | Config[], wit
const fixesMap = getFileFixes(fileName);
const result: ts.CodeFixAction[] = [];

for (const [ruleId, { diagnostic, actions }] of fixesMap) {
for (const [diagnostic, actions] of fixesMap) {
if (diagnostics?.length && !diagnostics.includes(diagnostic)) {
continue;
}
Expand All @@ -338,7 +324,7 @@ export function createLinter(ctx: ProjectContext, config: Config | Config[], wit
let codeFixes: ts.CodeFixAction[] = [];
for (const action of actions) {
codeFixes.push({
fixName: `tsslint:${ruleId}`,
fixName: `tsslint:${diagnostic.code}`,
description: action.title,
changes: action.getEdits(),
fixId: 'tsslint',
Expand All @@ -347,7 +333,7 @@ export function createLinter(ctx: ProjectContext, config: Config | Config[], wit
}
for (const plugin of plugins) {
if (plugin.resolveCodeFixes) {
codeFixes = plugin.resolveCodeFixes(fileName, diagnostic, result);
codeFixes = plugin.resolveCodeFixes(fileName, diagnostic, codeFixes);
}
}
result.push(...codeFixes);
Expand All @@ -358,37 +344,34 @@ export function createLinter(ctx: ProjectContext, config: Config | Config[], wit
},
getRefactors(fileName: string, start: number, end: number) {

const refactorsMap = getFileRefactors(fileName);
const refactors = getFileRefactors(fileName);
const result: ts.RefactorActionInfo[] = [];

let result: ts.RefactorActionInfo[] = [];

for (const [ruleId, { diagnostic, actions }] of refactorsMap) {
const diagStart = diagnostic.start;
const diagEnd = diagStart + diagnostic.length;
for (let i = 0; i < refactors.length; i++) {
const refactor = refactors[i];
const diagStart = refactor.diagnostic.start;
const diagEnd = diagStart + refactor.diagnostic.length;
if (
(diagStart >= start && diagStart <= end) ||
(diagEnd >= start && diagEnd <= end) ||
(start >= diagStart && start <= diagEnd) ||
(end >= diagStart && end <= diagEnd)
) {
for (let i = 0; i < actions.length; i++) {
const action = actions[i];
result.push({
name: `tsslint:${ruleId}:${i}`,
description: action.title + ' (' + ruleId + ')',
kind: 'quickfix',
});
}
result.push({
name: `tsslint:${i}`,
description: refactor.title,
kind: 'quickfix',
});
}
}

return result;
},
getRefactorEdits(fileName: string, name: string) {
if (name.startsWith('tsslint:')) {
const [ruleId, index] = name.slice('tsslint:'.length).split(':');
const refactorsMap = getFileRefactors(fileName);
const refactor = refactorsMap.get(ruleId)?.actions[Number(index)];
getRefactorEdits(fileName: string, actionName: string) {
if (actionName.startsWith('tsslint:')) {
const index = actionName.slice('tsslint:'.length);
const actions = getFileRefactors(fileName);
const refactor = actions[Number(index)];
if (refactor) {
return refactor.getEdits();
}
Expand Down Expand Up @@ -431,7 +414,7 @@ export function createLinter(ctx: ProjectContext, config: Config | Config[], wit

function getFileRefactors(fileName: string) {
if (!fileRefactors.has(fileName)) {
fileRefactors.set(fileName, new Map());
fileRefactors.set(fileName, []);
}
return fileRefactors.get(fileName)!;
}
Expand Down

0 comments on commit 19233b5

Please sign in to comment.