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

@@ -5,129 +5,129 @@ import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { getEnumValues } from '@/application/Common/Enum';
describe('ScriptingDefinition', () => {
describe('language', () => {
describe('sets as expected', () => {
// arrange
const expectedValues = getEnumValues(ScriptingLanguage);
expectedValues.forEach((expected) => {
it(ScriptingLanguage[expected], () => {
// act
const sut = new ScriptingDefinitionBuilder()
.withLanguage(expected)
.build();
// assert
expect(sut.language).to.equal(expected);
});
});
});
it('throws if unknown', () => {
// arrange
const unknownValue: ScriptingLanguage = 666;
const errorMessage = `unsupported language: ${unknownValue}`;
// act
const act = () => new ScriptingDefinitionBuilder()
.withLanguage(unknownValue)
.build();
// assert
expect(act).to.throw(errorMessage);
describe('language', () => {
describe('sets as expected', () => {
// arrange
const expectedValues = getEnumValues(ScriptingLanguage);
expectedValues.forEach((expected) => {
it(ScriptingLanguage[expected], () => {
// act
const sut = new ScriptingDefinitionBuilder()
.withLanguage(expected)
.build();
// assert
expect(sut.language).to.equal(expected);
});
});
});
describe('fileExtension', () => {
describe('returns expected for each language', () => {
// arrange
const testCases = new Map<ScriptingLanguage, string>([
[ScriptingLanguage.batchfile, 'bat'],
[ScriptingLanguage.shellscript, 'sh'],
]);
Array.from(testCases.entries()).forEach((test) => {
const language = test[0];
const expectedExtension = test[1];
it(`${ScriptingLanguage[language]} has ${expectedExtension}`, () => {
// act
const sut = new ScriptingDefinitionBuilder()
.withLanguage(language)
.build();
// assert
expect(sut.fileExtension, expectedExtension);
});
});
});
it('throws if unknown', () => {
// arrange
const unknownValue: ScriptingLanguage = 666;
const errorMessage = `unsupported language: ${unknownValue}`;
// act
const act = () => new ScriptingDefinitionBuilder()
.withLanguage(unknownValue)
.build();
// assert
expect(act).to.throw(errorMessage);
});
describe('startCode', () => {
it('sets as expected', () => {
// arrange
const expected = 'REM start-code';
// act
const sut = new ScriptingDefinitionBuilder()
.withStartCode(expected)
.build();
// assert
expect(sut.startCode).to.equal(expected);
});
it('throws when undefined', () => {
// arrange
const expectedError = 'undefined start code';
const undefinedValues = [ '', undefined ];
for (const undefinedValue of undefinedValues) {
// act
const act = () => new ScriptingDefinitionBuilder()
.withStartCode(undefinedValue)
.build();
// assert
expect(act).to.throw(expectedError);
}
});
describe('fileExtension', () => {
describe('returns expected for each language', () => {
// arrange
const testCases = new Map<ScriptingLanguage, string>([
[ScriptingLanguage.batchfile, 'bat'],
[ScriptingLanguage.shellscript, 'sh'],
]);
Array.from(testCases.entries()).forEach((test) => {
const language = test[0];
const expectedExtension = test[1];
it(`${ScriptingLanguage[language]} has ${expectedExtension}`, () => {
// act
const sut = new ScriptingDefinitionBuilder()
.withLanguage(language)
.build();
// assert
expect(sut.fileExtension, expectedExtension);
});
});
});
describe('endCode', () => {
it('sets as expected', () => {
// arrange
const expected = 'REM end-code';
// act
const sut = new ScriptingDefinitionBuilder()
.withEndCode(expected)
.build();
// assert
expect(sut.endCode).to.equal(expected);
});
it('throws when undefined', () => {
// arrange
const expectedError = 'undefined end code';
const undefinedValues = [ '', undefined ];
for (const undefinedValue of undefinedValues) {
// act
const act = () => new ScriptingDefinitionBuilder()
.withEndCode(undefinedValue)
.build();
// assert
expect(act).to.throw(expectedError);
}
});
});
describe('startCode', () => {
it('sets as expected', () => {
// arrange
const expected = 'REM start-code';
// act
const sut = new ScriptingDefinitionBuilder()
.withStartCode(expected)
.build();
// assert
expect(sut.startCode).to.equal(expected);
});
it('throws when undefined', () => {
// arrange
const expectedError = 'undefined start code';
const undefinedValues = ['', undefined];
for (const undefinedValue of undefinedValues) {
// act
const act = () => new ScriptingDefinitionBuilder()
.withStartCode(undefinedValue)
.build();
// assert
expect(act).to.throw(expectedError);
}
});
});
describe('endCode', () => {
it('sets as expected', () => {
// arrange
const expected = 'REM end-code';
// act
const sut = new ScriptingDefinitionBuilder()
.withEndCode(expected)
.build();
// assert
expect(sut.endCode).to.equal(expected);
});
it('throws when undefined', () => {
// arrange
const expectedError = 'undefined end code';
const undefinedValues = ['', undefined];
for (const undefinedValue of undefinedValues) {
// act
const act = () => new ScriptingDefinitionBuilder()
.withEndCode(undefinedValue)
.build();
// assert
expect(act).to.throw(expectedError);
}
});
});
});
class ScriptingDefinitionBuilder {
private language = ScriptingLanguage.shellscript;
private startCode = 'REM start-code';
private endCode = 'REM end-code';
private language = ScriptingLanguage.shellscript;
public withLanguage(language: ScriptingLanguage): ScriptingDefinitionBuilder {
this.language = language;
return this;
}
private startCode = 'REM start-code';
public withStartCode(startCode: string): ScriptingDefinitionBuilder {
this.startCode = startCode;
return this;
}
private endCode = 'REM end-code';
public withEndCode(endCode: string): ScriptingDefinitionBuilder {
this.endCode = endCode;
return this;
}
public withLanguage(language: ScriptingLanguage): ScriptingDefinitionBuilder {
this.language = language;
return this;
}
public build(): ScriptingDefinition {
return new ScriptingDefinition(
this.language, this.startCode, this.endCode);
}
public withStartCode(startCode: string): ScriptingDefinitionBuilder {
this.startCode = startCode;
return this;
}
public withEndCode(endCode: string): ScriptingDefinitionBuilder {
this.endCode = endCode;
return this;
}
public build(): ScriptingDefinition {
return new ScriptingDefinition(this.language, this.startCode, this.endCode);
}
}