Skip to content

Commit

Permalink
Fix case sensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
elsassph committed May 21, 2021
1 parent 677fdba commit 8b1b195
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
45 changes: 42 additions & 3 deletions src/plugins/trackCodeFlow/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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}`;
Expand All @@ -17,6 +18,7 @@ function fmtDiagnostics(diagnostics: BsDiagnostic[]) {

describe('trackCodeFlow', () => {
let linter: Linter;
let lintContext: PluginWrapperContext;
const project1 = {
rootDir: 'test/project1'
};
Expand All @@ -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);
}
});
Expand Down Expand Up @@ -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);
});
});
});
7 changes: 6 additions & 1 deletion src/plugins/trackCodeFlow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,7 +61,7 @@ export default class TrackCodeFlow {
if (!isBrsFile(file) || this.lintContext.ignores(file)) {
return;
}
const diagnostics: BsDiagnostic[] = [];
let diagnostics: BsDiagnostic[] = [];

resetVarContext(file);

Expand Down Expand Up @@ -152,6 +153,10 @@ export default class TrackCodeFlow {
}
});

if (this.lintContext.fix) {
diagnostics = extractFixes(this.lintContext, file, diagnostics);
}

file.addDiagnostics(diagnostics);
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/plugins/trackCodeFlow/trackFixes.ts
Original file line number Diff line number Diff line change
@@ -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)
];
}
8 changes: 6 additions & 2 deletions src/plugins/trackCodeFlow/varTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
}
});
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/project1/source/case-sensitivity-fixed.brs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8b1b195

Please sign in to comment.