From 8b1b195d2d2104aec7948338fbe6590c87faf220 Mon Sep 17 00:00:00 2001 From: Philippe Elsass Date: Fri, 21 May 2021 16:24:39 +0100 Subject: [PATCH] Fix case sensitivity --- README.md | 1 + src/plugins/trackCodeFlow/index.spec.ts | 45 +++++++++++++++++-- src/plugins/trackCodeFlow/index.ts | 7 ++- src/plugins/trackCodeFlow/trackFixes.ts | 22 +++++++++ src/plugins/trackCodeFlow/varTracking.ts | 8 +++- .../source/case-sensitivity-fixed.brs | 28 ++++++++++++ 6 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 src/plugins/trackCodeFlow/trackFixes.ts create mode 100644 test/project1/source/case-sensitivity-fixed.brs diff --git a/README.md b/README.md index a1ecad9..123991b 100644 --- a/README.md +++ b/README.md @@ -211,3 +211,4 @@ Running `bslint` with `--fix` parameter will attempt to fix common code-style is - Using wrong `sub` or `function` keyword, - Using/missing the optional `then` keyword, - Using/missing parenthesis around `if/while` conditions. +- Case sensitivity (align with first occurence) diff --git a/src/plugins/trackCodeFlow/index.spec.ts b/src/plugins/trackCodeFlow/index.spec.ts index dbf2ec2..13d69c6 100644 --- a/src/plugins/trackCodeFlow/index.spec.ts +++ b/src/plugins/trackCodeFlow/index.spec.ts @@ -1,8 +1,9 @@ +import * as fs from 'fs'; import { expect } from 'chai'; import { BsDiagnostic, Program } from 'brighterscript'; import Linter from '../../Linter'; import TrackCodeFlow from './index'; -import { createContext } from '../../util'; +import { createContext, PluginWrapperContext } from '../../util'; function pad(n: number) { return n > 9 ? `${n}` : `0${n}`; @@ -17,6 +18,7 @@ function fmtDiagnostics(diagnostics: BsDiagnostic[]) { describe('trackCodeFlow', () => { let linter: Linter; + let lintContext: PluginWrapperContext; const project1 = { rootDir: 'test/project1' }; @@ -26,8 +28,8 @@ describe('trackCodeFlow', () => { linter.builder.plugins.add({ name: 'test', afterProgramCreate: (program: Program) => { - const context = createContext(program); - const trackCodeFlow = new TrackCodeFlow(context); + lintContext = createContext(program); + const trackCodeFlow = new TrackCodeFlow(lintContext); program.plugins.add(trackCodeFlow); } }); @@ -229,4 +231,41 @@ describe('trackCodeFlow', () => { ]; expect(actual).deep.equal(expected); }); + + describe('fix', () => { + beforeEach(() => { + fs.copyFileSync( + `${project1.rootDir}/source/case-sensitivity.brs`, + `${project1.rootDir}/source/case-sensitivity-temp.brs` + ); + }); + + afterEach(() => { + fs.unlinkSync(`${project1.rootDir}/source/case-sensitivity-temp.brs`); + }); + + it('fixes inconsistent case', async () => { + const diagnostics = await linter.run({ + ...project1, + files: ['source/case-sensitivity-temp.brs'], + rules: { + 'case-sensitivity': 'error' + }, + fix: true + }); + const actual = fmtDiagnostics(diagnostics); + const expected = [ + `11:LINT1005:Variable 'A' is set but value is never used` + ]; + expect(actual).deep.equal(expected); + + expect(lintContext.pendingFixes.size).equals(1); + await lintContext.applyFixes(); + expect(lintContext.pendingFixes.size).equals(0); + + const actualSrc = fs.readFileSync(`${project1.rootDir}/source/case-sensitivity-temp.brs`).toString(); + const expectedSrc = fs.readFileSync(`${project1.rootDir}/source/case-sensitivity-fixed.brs`).toString(); + expect(actualSrc).to.equal(expectedSrc); + }); + }); }); diff --git a/src/plugins/trackCodeFlow/index.ts b/src/plugins/trackCodeFlow/index.ts index 4b92518..ef2846f 100644 --- a/src/plugins/trackCodeFlow/index.ts +++ b/src/plugins/trackCodeFlow/index.ts @@ -4,6 +4,7 @@ import { isForEachStatement, isForStatement, isIfStatement, isWhileStatement, Ra import { PluginContext } from '../../util'; import { createReturnLinter } from './returnTracking'; import { createVarLinter, resetVarContext, runDeferredValidation } from './varTracking'; +import { extractFixes } from './trackFixes'; export interface NarrowingInfo { text: string; @@ -60,7 +61,7 @@ export default class TrackCodeFlow { if (!isBrsFile(file) || this.lintContext.ignores(file)) { return; } - const diagnostics: BsDiagnostic[] = []; + let diagnostics: BsDiagnostic[] = []; resetVarContext(file); @@ -152,6 +153,10 @@ export default class TrackCodeFlow { } }); + if (this.lintContext.fix) { + diagnostics = extractFixes(this.lintContext, file, diagnostics); + } + file.addDiagnostics(diagnostics); } } diff --git a/src/plugins/trackCodeFlow/trackFixes.ts b/src/plugins/trackCodeFlow/trackFixes.ts new file mode 100644 index 0000000..fab1b05 --- /dev/null +++ b/src/plugins/trackCodeFlow/trackFixes.ts @@ -0,0 +1,22 @@ +import { BrsFile, BsDiagnostic, Range } from 'brighterscript'; +import { replaceText } from '../../textEdit'; +import { PluginContext } from '../../util'; +import { VarLintError } from './varTracking'; + +export function extractFixes(lintContext: PluginContext, file: BrsFile, diagnostics: BsDiagnostic[]): BsDiagnostic[] { + return diagnostics.filter(diagnostic => { + switch (diagnostic.code) { + case VarLintError.CaseMismatch: + lintContext.addFixes(file, fixCasing(diagnostic.data)); + return false; + default: + return true; + } + }); +} + +function fixCasing(data: { name: string; range: Range }) { + return [ + replaceText(data.range, data.name) + ]; +} diff --git a/src/plugins/trackCodeFlow/varTracking.ts b/src/plugins/trackCodeFlow/varTracking.ts index fdd2e4a..3464377 100644 --- a/src/plugins/trackCodeFlow/varTracking.ts +++ b/src/plugins/trackCodeFlow/varTracking.ts @@ -2,7 +2,7 @@ import { BscFile, FunctionExpression, BsDiagnostic, Range, isForStatement, isFor import { LintState, StatementInfo, NarrowingInfo, VarInfo } from '.'; import { PluginContext } from '../../util'; -enum VarLintError { +export enum VarLintError { UninitializedVar = 'LINT1001', UnsafeIteratorVar = 'LINT1002', UnsafeInitialization = 'LINT1003', @@ -62,7 +62,11 @@ export function createVarLinter( code: VarLintError.CaseMismatch, message: `Variable '${name.text}' was previously set with a different casing as '${curr.name}'`, range: name.range, - file: file + file: file, + data: { + name: curr.name, + range: name.range + } }); } } diff --git a/test/project1/source/case-sensitivity-fixed.brs b/test/project1/source/case-sensitivity-fixed.brs new file mode 100644 index 0000000..07deb5d --- /dev/null +++ b/test/project1/source/case-sensitivity-fixed.brs @@ -0,0 +1,28 @@ +sub error1() + a = 1 + print a ' error + print "a="; a ' error + some(a) ' error + some(1 + a) ' error +end sub + +sub error2() + a = 1 + a = 2 ' error +end sub + +sub error3(A) + A = 2 ' error +end sub + +sub ok1() + A = 1 + print A + print "A="; A + some(A) + some(1 + A) +end sub + +sub some(p) + print p +end sub