Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 71de7a3

Browse files
committed
Indent disable rule according to current line's indentation
Fixes #73
1 parent 028091b commit 71de7a3

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

e2e/tests/codeFixes.test.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ const { openMockFile, getFirstResponseOfType } = require('./helpers');
99
const mockFileName = path.join(__dirname, '..', 'project-fixture', 'main.ts').replace(/\\/g, '/');
1010

1111
/**
12-
* @param {string} fileContents
12+
* @param {string[]} fileContents
1313
*/
14-
function createServerForFile(fileContents) {
14+
function createServerForFile(...fileContents) {
1515
const server = createServer();
16-
openMockFile(server, mockFileName, fileContents);
16+
openMockFile(server, mockFileName, fileContents.join('\n'));
1717
return server;
1818
}
1919

@@ -394,4 +394,29 @@ describe('CodeFixes', () => {
394394
assert.deepEqual(codeFixesResponse.body[0].fixName, 'spelling');
395395
assert.deepEqual(codeFixesResponse.body[1].fixName, 'tslint:disable:no-unused-expression');
396396
});
397+
398+
it('disable comment should be correctly indented', async () => {
399+
server = createServerForFile(
400+
'{',
401+
' const a = 1',
402+
'}'
403+
);
404+
await getCodeFixes(server, {
405+
startLine: 2,
406+
startOffset: 16,
407+
endLine: 2,
408+
endOffset: 16,
409+
additionalErrorCodes: [2552]
410+
});
411+
await server.close();
412+
const codeFixesResponse = await getFirstResponseOfType('getCodeFixes', server);
413+
414+
assert.isTrue(codeFixesResponse.success);
415+
assert.deepEqual(codeFixesResponse.body.length, 3);
416+
const disableFix = codeFixesResponse.body[2];
417+
const change = disableFix.changes[0].textChanges[0];
418+
assert.strictEqual(change.start.line, 2);
419+
assert.strictEqual(change.start.offset, 1);
420+
assert.strictEqual(change.newText, ' // tslint:disable-next-line: semicolon\n');
421+
});
397422
});

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export = function init({ typescript }: { typescript: typeof ts_module }) {
2424
return info.languageService;
2525
}
2626

27-
return new TSLintPlugin(typescript, logger, info.project, configManager)
27+
return new TSLintPlugin(typescript, info.languageServiceHost, logger, info.project, configManager)
2828
.decorate(info.languageService);
2929
},
3030
onConfigurationChanged(config: any) {

src/plugin.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class TSLintPlugin {
5656

5757
public constructor(
5858
private readonly ts: typeof ts_module,
59+
private readonly languageServiceHost: ts_module.LanguageServiceHost,
5960
private readonly logger: Logger,
6061
private readonly project: ts_module.server.Project,
6162
private readonly configurationManager: ConfigurationManager,
@@ -320,14 +321,28 @@ export class TSLintPlugin {
320321
}
321322

322323
private getDisableRuleQuickFix(failure: tslint.RuleFailure, fileName: string, file: ts_module.SourceFile): ts_module.CodeFixAction {
324+
const line = failure.getStartPosition().getLineAndCharacter().line;
325+
const lineStarts = file.getLineStarts();
326+
const lineStart = lineStarts[line];
327+
let prefix = '';
328+
const snapshot = this.languageServiceHost.getScriptSnapshot(fileName);
329+
if (snapshot) {
330+
const lineEnd = line < lineStarts.length - 1 ? lineStarts[line + 1] : file.end;
331+
const lineText = snapshot.getText(lineStart, lineEnd);
332+
const leadingSpace = lineText.match(/^([ \t]+)/);
333+
if (leadingSpace) {
334+
prefix = leadingSpace[0];
335+
}
336+
}
337+
323338
return {
324339
description: `Disable rule '${failure.getRuleName()}'`,
325340
fixName: `tslint:disable:${failure.getRuleName()}`,
326341
changes: [{
327342
fileName,
328343
textChanges: [{
329-
newText: `// tslint:disable-next-line: ${failure.getRuleName()}\n`,
330-
span: { start: file.getLineStarts()[failure.getStartPosition().getLineAndCharacter().line], length: 0 },
344+
newText: `${prefix}// tslint:disable-next-line: ${failure.getRuleName()}\n`,
345+
span: { start: lineStart, length: 0 },
331346
}],
332347
}],
333348
};

0 commit comments

Comments
 (0)