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

@@ -6,94 +6,99 @@ import { ProjectInformationStub } from '@tests/unit/stubs/ProjectInformationStub
import { ExpressionsCompilerStub } from '@tests/unit/stubs/ExpressionsCompilerStub';
describe('CodeSubstituter', () => {
describe('throws with invalid parameters', () => {
// arrange
const testCases = [{
expectedError: 'undefined code',
parameters: {
code: undefined,
info: new ProjectInformationStub(),
}},
{
expectedError: 'undefined info',
parameters: {
code: 'non empty code',
info: undefined,
},
}];
for (const testCase of testCases) {
it(`throws "${testCase.expectedError}" as expected`, () => {
const sut = new CodeSubstituterBuilder().build();
// act
const act = () => sut.substitute(testCase.parameters.code, testCase.parameters.info);
// assert
expect(act).to.throw(testCase.expectedError);
});
}
});
describe('substitutes parameters as expected values', () => {
// arrange
const info = new ProjectInformationStub();
const date = new Date();
const testCases = [
{
parameter: 'homepage',
argument: info.homepage,
},
{
parameter: 'version',
argument: info.version,
},
{
parameter: 'date',
argument: date.toUTCString(),
},
];
for (const testCase of testCases) {
it(`substitutes ${testCase.parameter} as expected`, () => {
const compilerStub = new ExpressionsCompilerStub();
const sut = new CodeSubstituterBuilder()
.withCompiler(compilerStub)
.withDate(date)
.build();
// act
sut.substitute('non empty code', info);
// assert
expect(compilerStub.callHistory).to.have.lengthOf(1);
const parameters = compilerStub.callHistory[0].parameters;
expect(parameters.hasArgument(testCase.parameter));
const argumentValue = parameters.getArgument(testCase.parameter).argumentValue;
expect(argumentValue).to.equal(testCase.argument);
});
}
});
it('returns code as it is', () => {
// arrange
const expected = 'expected-code';
describe('throws with invalid parameters', () => {
// arrange
const testCases = [{
expectedError: 'undefined code',
parameters: {
code: undefined,
info: new ProjectInformationStub(),
},
},
{
expectedError: 'undefined info',
parameters: {
code: 'non empty code',
info: undefined,
},
}];
for (const testCase of testCases) {
it(`throws "${testCase.expectedError}" as expected`, () => {
const sut = new CodeSubstituterBuilder().build();
// act
const act = () => sut.substitute(testCase.parameters.code, testCase.parameters.info);
// assert
expect(act).to.throw(testCase.expectedError);
});
}
});
describe('substitutes parameters as expected values', () => {
// arrange
const info = new ProjectInformationStub();
const date = new Date();
const testCases = [
{
parameter: 'homepage',
argument: info.homepage,
},
{
parameter: 'version',
argument: info.version,
},
{
parameter: 'date',
argument: date.toUTCString(),
},
];
for (const testCase of testCases) {
it(`substitutes ${testCase.parameter} as expected`, () => {
const compilerStub = new ExpressionsCompilerStub();
const sut = new CodeSubstituterBuilder()
.withCompiler(compilerStub)
.build();
.withCompiler(compilerStub)
.withDate(date)
.build();
// act
sut.substitute(expected, new ProjectInformationStub());
sut.substitute('non empty code', info);
// assert
expect(compilerStub.callHistory).to.have.lengthOf(1);
expect(compilerStub.callHistory[0].code).to.equal(expected);
});
const { parameters } = compilerStub.callHistory[0];
expect(parameters.hasArgument(testCase.parameter));
const { argumentValue } = parameters.getArgument(testCase.parameter);
expect(argumentValue).to.equal(testCase.argument);
});
}
});
it('returns code as it is', () => {
// arrange
const expected = 'expected-code';
const compilerStub = new ExpressionsCompilerStub();
const sut = new CodeSubstituterBuilder()
.withCompiler(compilerStub)
.build();
// act
sut.substitute(expected, new ProjectInformationStub());
// assert
expect(compilerStub.callHistory).to.have.lengthOf(1);
expect(compilerStub.callHistory[0].code).to.equal(expected);
});
});
class CodeSubstituterBuilder {
private compiler: IExpressionsCompiler = new ExpressionsCompilerStub();
private date = new Date();
public withCompiler(compiler: IExpressionsCompiler) {
this.compiler = compiler;
return this;
}
public withDate(date: Date) {
this.date = date;
return this;
}
public build() {
return new CodeSubstituter(this.compiler, this.date);
}
private compiler: IExpressionsCompiler = new ExpressionsCompilerStub();
private date = new Date();
public withCompiler(compiler: IExpressionsCompiler) {
this.compiler = compiler;
return this;
}
public withDate(date: Date) {
this.date = date;
return this;
}
public build() {
return new CodeSubstituter(this.compiler, this.date);
}
}

View File

@@ -11,100 +11,103 @@ import { ScriptingDefinitionDataStub } from '@tests/unit/stubs/ScriptingDefiniti
import { CodeSubstituterStub } from '@tests/unit/stubs/CodeSubstituterStub';
describe('ScriptingDefinitionParser', () => {
describe('parseScriptingDefinition', () => {
it('throws when info is undefined', () => {
// arrange
const info = undefined;
const definition = new ScriptingDefinitionDataStub();
const sut = new ScriptingDefinitionParserBuilder()
.build();
// act
const act = () => sut.parse(definition, info);
// assert
expect(act).to.throw('undefined info');
});
it('throws when definition is undefined', () => {
// arrange
const info = new ProjectInformationStub();
const definition = undefined;
const sut = new ScriptingDefinitionParserBuilder()
.build();
// act
const act = () => sut.parse(definition, info);
// assert
expect(act).to.throw('undefined definition');
});
describe('language', () => {
it('parses as expected', () => {
// arrange
const expectedLanguage = ScriptingLanguage.batchfile;
const languageText = 'batchfile';
const expectedName = 'language';
const info = new ProjectInformationStub();
const definition = new ScriptingDefinitionDataStub()
.withLanguage(languageText);
const parserMock = new EnumParserStub<ScriptingLanguage>()
.setup(expectedName, languageText, expectedLanguage);
const sut = new ScriptingDefinitionParserBuilder()
.withParser(parserMock)
.build();
// act
const actual = sut.parse(definition, info);
// assert
expect(actual.language).to.equal(expectedLanguage);
});
});
describe('substitutes code as expected', () => {
// arrange
const code = 'hello';
const expected = 'substituted';
const testCases = [
{
name: 'startCode',
getActualValue: (result: IScriptingDefinition) => result.startCode,
data: new ScriptingDefinitionDataStub()
.withStartCode(code),
},
{
name: 'endCode',
getActualValue: (result: IScriptingDefinition) => result.endCode,
data: new ScriptingDefinitionDataStub()
.withEndCode(code),
},
];
for (const testCase of testCases) {
it(testCase.name, () => {
const info = new ProjectInformationStub();
const substituterMock = new CodeSubstituterStub()
.setup(code, info, expected);
const sut = new ScriptingDefinitionParserBuilder()
.withSubstituter(substituterMock)
.build();
// act
const definition = sut.parse(testCase.data, info);
// assert
const actual = testCase.getActualValue(definition);
expect(actual).to.equal(expected);
});
}
});
describe('parseScriptingDefinition', () => {
it('throws when info is undefined', () => {
// arrange
const info = undefined;
const definition = new ScriptingDefinitionDataStub();
const sut = new ScriptingDefinitionParserBuilder()
.build();
// act
const act = () => sut.parse(definition, info);
// assert
expect(act).to.throw('undefined info');
});
it('throws when definition is undefined', () => {
// arrange
const info = new ProjectInformationStub();
const definition = undefined;
const sut = new ScriptingDefinitionParserBuilder()
.build();
// act
const act = () => sut.parse(definition, info);
// assert
expect(act).to.throw('undefined definition');
});
describe('language', () => {
it('parses as expected', () => {
// arrange
const expectedLanguage = ScriptingLanguage.batchfile;
const languageText = 'batchfile';
const expectedName = 'language';
const info = new ProjectInformationStub();
const definition = new ScriptingDefinitionDataStub()
.withLanguage(languageText);
const parserMock = new EnumParserStub<ScriptingLanguage>()
.setup(expectedName, languageText, expectedLanguage);
const sut = new ScriptingDefinitionParserBuilder()
.withParser(parserMock)
.build();
// act
const actual = sut.parse(definition, info);
// assert
expect(actual.language).to.equal(expectedLanguage);
});
});
describe('substitutes code as expected', () => {
// arrange
const code = 'hello';
const expected = 'substituted';
const testCases = [
{
name: 'startCode',
getActualValue: (result: IScriptingDefinition) => result.startCode,
data: new ScriptingDefinitionDataStub()
.withStartCode(code),
},
{
name: 'endCode',
getActualValue: (result: IScriptingDefinition) => result.endCode,
data: new ScriptingDefinitionDataStub()
.withEndCode(code),
},
];
for (const testCase of testCases) {
it(testCase.name, () => {
const info = new ProjectInformationStub();
const substituterMock = new CodeSubstituterStub()
.setup(code, info, expected);
const sut = new ScriptingDefinitionParserBuilder()
.withSubstituter(substituterMock)
.build();
// act
const definition = sut.parse(testCase.data, info);
// assert
const actual = testCase.getActualValue(definition);
expect(actual).to.equal(expected);
});
}
});
});
});
class ScriptingDefinitionParserBuilder {
private languageParser: IEnumParser<ScriptingLanguage> = new EnumParserStub<ScriptingLanguage>()
.setupDefaultValue(ScriptingLanguage.shellscript);
private codeSubstituter: ICodeSubstituter = new CodeSubstituterStub();
private languageParser: IEnumParser<ScriptingLanguage> = new EnumParserStub<ScriptingLanguage>()
.setupDefaultValue(ScriptingLanguage.shellscript);
public withParser(parser: IEnumParser<ScriptingLanguage>) {
this.languageParser = parser;
return this;
}
public withSubstituter(substituter: ICodeSubstituter) {
this.codeSubstituter = substituter;
return this;
}
public build() {
return new ScriptingDefinitionParser(this.languageParser, this.codeSubstituter);
}
private codeSubstituter: ICodeSubstituter = new CodeSubstituterStub();
public withParser(parser: IEnumParser<ScriptingLanguage>) {
this.languageParser = parser;
return this;
}
public withSubstituter(substituter: ICodeSubstituter) {
this.codeSubstituter = substituter;
return this;
}
public build() {
return new ScriptingDefinitionParser(this.languageParser, this.codeSubstituter);
}
}