Refactor code to comply with ESLint rules

Major refactoring using ESLint with rules from AirBnb and Vue.

Enable most of the ESLint rules and do necessary linting in the code.
Also add more information for rules that are disabled to describe what
they are and why they are disabled.

Allow logging (`console.log`) in test files, and in development mode
(e.g. when working with `npm run serve`), but disable it when
environment is production (as pre-configured by Vue). Also add flag
(`--mode production`) in `lint:eslint` command so production linting is
executed earlier in lifecycle.

Disable rules that requires a separate work. Such as ESLint rules that
are broken in TypeScript: no-useless-constructor (eslint/eslint#14118)
and no-shadow (eslint/eslint#13014).
This commit is contained in:
undergroundwires
2022-01-02 18:20:14 +01:00
parent 96265b75de
commit 5b1fbe1e2f
341 changed files with 16126 additions and 15101 deletions

View File

@@ -8,140 +8,204 @@ import { SelectedScriptStub } from '@tests/unit/stubs/SelectedScriptStub';
import { ScriptStub } from '@tests/unit/stubs/ScriptStub';
describe('CodeChangedEvent', () => {
describe('ctor', () => {
describe('position validation', () => {
it('throws when code position is out of range', () => {
const act = () => new CodeChangedEvent(
'singleline code', [], new Map<SelectedScript, ICodePosition>([
[ new SelectedScriptStub('1'), new CodePosition(0, 2) ],
]),
);
expect(act).to.throw();
});
it('does not throw with valid code position', () => {
const act = () => new CodeChangedEvent(
'singleline code', [], new Map<SelectedScript, ICodePosition>([
[ new SelectedScriptStub('1'), new CodePosition(0, 1) ],
]),
);
expect(act).to.not.throw();
});
});
});
it('code returns expected', () => {
describe('ctor', () => {
describe('position validation', () => {
it('throws when code position is out of range', () => {
// arrange
const expected = 'code';
const code = 'singleline code';
const newScripts = new Map<SelectedScript, ICodePosition>([
[new SelectedScriptStub('1'), new CodePosition(0, 2 /* nonexisting line */)],
]);
// act
const sut = new CodeChangedEvent(
expected, [], new Map<SelectedScript, ICodePosition>(),
);
const actual = sut.code;
const act = () => new CodeChangedEventBuilder()
.withCode(code)
.withNewScripts(newScripts)
.build();
// assert
expect(actual).to.equal(expected);
});
describe('addedScripts', () => {
it('returns new scripts when scripts are added', () => {
// arrange
const expected = [ new ScriptStub('3'), new ScriptStub('4') ];
const initialScripts = [ new SelectedScriptStub('1'), new SelectedScriptStub('2') ];
expect(act).to.throw();
});
describe('does not throw with valid code position', () => {
// arrange
const testCases = [
{
name: 'singleline',
code: 'singleline code',
position: new CodePosition(0, 1),
},
{
name: 'multiline',
code: 'multiline code\nsecond line',
position: new CodePosition(0, 2),
},
];
for (const testCase of testCases) {
it(testCase.name, () => {
const newScripts = new Map<SelectedScript, ICodePosition>([
[initialScripts[0], new CodePosition(0, 1) ],
[initialScripts[1], new CodePosition(0, 1) ],
[new SelectedScript(expected[0], false), new CodePosition(0, 1) ],
[new SelectedScript(expected[1], false), new CodePosition(0, 1) ],
[new SelectedScriptStub('1'), testCase.position],
]);
// act
const sut = new CodeChangedEvent(
'code', initialScripts, newScripts,
);
const actual = sut.addedScripts;
const act = () => new CodeChangedEventBuilder()
.withCode(testCase.code)
.withNewScripts(newScripts)
.build();
// assert
expect(actual).to.have.lengthOf(2);
expect(actual[0]).to.deep.equal(expected[0]);
expect(actual[1]).to.deep.equal(expected[1]);
});
expect(act).to.not.throw();
});
}
});
});
describe('removedScripts', () => {
it('returns removed scripts when script are removed', () => {
// arrange
const existingScripts = [ new SelectedScriptStub('0'), new SelectedScriptStub('1') ];
const removedScripts = [ new SelectedScriptStub('2') ];
const initialScripts = [ ...existingScripts, ...removedScripts ];
const newScripts = new Map<SelectedScript, ICodePosition>([
[initialScripts[0], new CodePosition(0, 1) ],
[initialScripts[1], new CodePosition(0, 1) ],
]);
// act
const sut = new CodeChangedEvent(
'code', initialScripts, newScripts,
);
const actual = sut.removedScripts;
// assert
expect(actual).to.have.lengthOf(removedScripts.length);
expect(actual[0]).to.deep.equal(removedScripts[0].script);
});
});
it('code returns expected', () => {
// arrange
const expected = 'code';
const sut = new CodeChangedEventBuilder()
.withCode(expected)
.build();
// act
const actual = sut.code;
// assert
expect(actual).to.equal(expected);
});
describe('addedScripts', () => {
it('returns new scripts when scripts are added', () => {
// arrange
const expected = [new ScriptStub('3'), new ScriptStub('4')];
const initialScripts = [new SelectedScriptStub('1'), new SelectedScriptStub('2')];
const newScripts = new Map<SelectedScript, ICodePosition>([
[initialScripts[0], new CodePosition(0, 1)],
[initialScripts[1], new CodePosition(0, 1)],
[new SelectedScript(expected[0], false), new CodePosition(0, 1)],
[new SelectedScript(expected[1], false), new CodePosition(0, 1)],
]);
const sut = new CodeChangedEventBuilder()
.withOldScripts(initialScripts)
.withNewScripts(newScripts)
.build();
// act
const actual = sut.addedScripts;
// assert
expect(actual).to.have.lengthOf(2);
expect(actual[0]).to.deep.equal(expected[0]);
expect(actual[1]).to.deep.equal(expected[1]);
});
describe('changedScripts', () => {
it('returns changed scripts when scripts are changed', () => {
// arrange
const initialScripts = [ new SelectedScriptStub('1', false), new SelectedScriptStub('2', false) ];
const newScripts = new Map<SelectedScript, ICodePosition>([
[new SelectedScriptStub('1', true), new CodePosition(0, 1) ],
[new SelectedScriptStub('2', false), new CodePosition(0, 1) ],
]);
// act
const sut = new CodeChangedEvent(
'code', initialScripts, newScripts,
);
const actual = sut.changedScripts;
// assert
expect(actual).to.have.lengthOf(1);
expect(actual[0]).to.deep.equal(initialScripts[0].script);
});
});
describe('removedScripts', () => {
it('returns removed scripts when script are removed', () => {
// arrange
const existingScripts = [new SelectedScriptStub('0'), new SelectedScriptStub('1')];
const removedScripts = [new SelectedScriptStub('2')];
const initialScripts = [...existingScripts, ...removedScripts];
const newScripts = new Map<SelectedScript, ICodePosition>([
[initialScripts[0], new CodePosition(0, 1)],
[initialScripts[1], new CodePosition(0, 1)],
]);
const sut = new CodeChangedEventBuilder()
.withOldScripts(initialScripts)
.withNewScripts(newScripts)
.build();
// act
const actual = sut.removedScripts;
// assert
expect(actual).to.have.lengthOf(removedScripts.length);
expect(actual[0]).to.deep.equal(removedScripts[0].script);
});
describe('isEmpty', () => {
it('returns true when empty', () => {
// arrange
const newScripts = new Map<SelectedScript, ICodePosition>();
const oldScripts = [ new SelectedScriptStub('1', false) ];
const sut = new CodeChangedEvent(
'code', oldScripts, newScripts,
);
// act
const actual = sut.isEmpty();
// assert
expect(actual).to.equal(true);
});
it('returns false when not empty', () => {
// arrange
const oldScripts = [ new SelectedScriptStub('1') ];
const newScripts = new Map<SelectedScript, ICodePosition>( [
[oldScripts[0], new CodePosition(0, 1) ],
]);
const sut = new CodeChangedEvent(
'code', oldScripts, newScripts,
);
// act
const actual = sut.isEmpty();
// assert
expect(actual).to.equal(false);
});
});
describe('changedScripts', () => {
it('returns changed scripts when scripts are changed', () => {
// arrange
const initialScripts = [new SelectedScriptStub('1', false), new SelectedScriptStub('2', false)];
const newScripts = new Map<SelectedScript, ICodePosition>([
[new SelectedScriptStub('1', true), new CodePosition(0, 1)],
[new SelectedScriptStub('2', false), new CodePosition(0, 1)],
]);
const sut = new CodeChangedEventBuilder()
.withOldScripts(initialScripts)
.withNewScripts(newScripts)
.build();
// act
const actual = sut.changedScripts;
// assert
expect(actual).to.have.lengthOf(1);
expect(actual[0]).to.deep.equal(initialScripts[0].script);
});
describe('getScriptPositionInCode', () => {
it('returns expected position for existing script', () => {
// arrange
const script = new ScriptStub('1');
const expected = new CodePosition(0, 1);
const newScripts = new Map<SelectedScript, ICodePosition>( [
[new SelectedScript(script, false), expected ],
]);
const sut = new CodeChangedEvent(
'code', [], newScripts,
);
// act
const actual = sut.getScriptPositionInCode(script);
// assert
expect(actual).to.deep.equal(expected);
});
});
describe('isEmpty', () => {
it('returns true when empty', () => {
// arrange
const newScripts = new Map<SelectedScript, ICodePosition>();
const oldScripts = [new SelectedScriptStub('1', false)];
const sut = new CodeChangedEventBuilder()
.withOldScripts(oldScripts)
.withNewScripts(newScripts)
.build();
// act
const actual = sut.isEmpty();
// assert
expect(actual).to.equal(true);
});
it('returns false when not empty', () => {
// arrange
const oldScripts = [new SelectedScriptStub('1')];
const newScripts = new Map<SelectedScript, ICodePosition>([
[oldScripts[0], new CodePosition(0, 1)],
]);
const sut = new CodeChangedEventBuilder()
.withOldScripts(oldScripts)
.withNewScripts(newScripts)
.build();
// act
const actual = sut.isEmpty();
// assert
expect(actual).to.equal(false);
});
});
describe('getScriptPositionInCode', () => {
it('returns expected position for existing script', () => {
// arrange
const script = new ScriptStub('1');
const expected = new CodePosition(0, 1);
const newScripts = new Map<SelectedScript, ICodePosition>([
[new SelectedScript(script, false), expected],
]);
const sut = new CodeChangedEventBuilder()
.withNewScripts(newScripts)
.build();
// act
const actual = sut.getScriptPositionInCode(script);
// assert
expect(actual).to.deep.equal(expected);
});
});
});
// Prefer over original ctor in tests for easier future ctor refactorings
class CodeChangedEventBuilder {
private code = '[CodeChangedEventBuilder] default code';
private oldScripts: ReadonlyArray<SelectedScript> = [];
private newScripts = new Map<SelectedScript, ICodePosition>();
public withCode(code: string) {
this.code = code;
return this;
}
public withOldScripts(oldScripts: ReadonlyArray<SelectedScript>) {
this.oldScripts = oldScripts;
return this;
}
public withNewScripts(newScripts: Map<SelectedScript, ICodePosition>) {
this.newScripts = newScripts;
return this;
}
public build(): CodeChangedEvent {
return new CodeChangedEvent(
this.code,
this.oldScripts,
this.newScripts,
);
}
}