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

@@ -16,198 +16,217 @@ import { parseFunctionCalls } from '@/application/Parser/Script/Compiler/Functio
import { FunctionCallDataStub } from '@tests/unit/stubs/FunctionCallDataStub';
describe('ScriptCompiler', () => {
describe('ctor', () => {
it('throws if syntax is undefined', () => {
// arrange
const expectedError = `undefined syntax`;
// act
const act = () => new ScriptCompilerBuilder()
.withSomeFunctions()
.withSyntax(undefined)
.build();
// assert
expect(act).to.throw(expectedError);
});
describe('ctor', () => {
it('throws if syntax is undefined', () => {
// arrange
const expectedError = 'undefined syntax';
// act
const act = () => new ScriptCompilerBuilder()
.withSomeFunctions()
.withSyntax(undefined)
.build();
// assert
expect(act).to.throw(expectedError);
});
describe('canCompile', () => {
it('throws if script is undefined', () => {
// arrange
const expectedError = 'undefined script';
const argument = undefined;
const builder = new ScriptCompilerBuilder()
.withEmptyFunctions()
.build();
// act
const act = () => builder.canCompile(argument);
// assert
expect(act).to.throw(expectedError);
});
it('returns true if "call" is defined', () => {
// arrange
const sut = new ScriptCompilerBuilder()
.withEmptyFunctions()
.build();
const script = ScriptDataStub.createWithCall();
// act
const actual = sut.canCompile(script);
// assert
expect(actual).to.equal(true);
});
it('returns false if "call" is undefined', () => {
// arrange
const sut = new ScriptCompilerBuilder()
.withEmptyFunctions()
.build();
const script = ScriptDataStub.createWithCode();
// act
const actual = sut.canCompile(script);
// assert
expect(actual).to.equal(false);
});
});
describe('canCompile', () => {
it('throws if script is undefined', () => {
// arrange
const expectedError = 'undefined script';
const argument = undefined;
const builder = new ScriptCompilerBuilder()
.withEmptyFunctions()
.build();
// act
const act = () => builder.canCompile(argument);
// assert
expect(act).to.throw(expectedError);
});
describe('compile', () => {
it('throws if script is undefined', () => {
// arrange
const expectedError = 'undefined script';
const argument = undefined;
const builder = new ScriptCompilerBuilder()
.withEmptyFunctions()
.build();
// act
const act = () => builder.compile(argument);
// assert
expect(act).to.throw(expectedError);
});
it('returns code as expected', () => {
// arrange
const expected: ICompiledCode = {
code: 'expected-code',
revertCode: 'expected-revert-code',
};
const call = new FunctionCallDataStub();
const script = ScriptDataStub.createWithCall(call);
const functions = [ FunctionDataStub.createWithCode().withName('existing-func') ];
const compiledFunctions = new SharedFunctionCollectionStub();
const functionParserMock = new SharedFunctionsParserStub();
functionParserMock.setup(functions, compiledFunctions);
const callCompilerMock = new FunctionCallCompilerStub();
callCompilerMock.setup(parseFunctionCalls(call), compiledFunctions, expected);
const sut = new ScriptCompilerBuilder()
.withFunctions(...functions)
.withSharedFunctionsParser(functionParserMock)
.withFunctionCallCompiler(callCompilerMock)
.build();
// act
const code = sut.compile(script);
// assert
expect(code.execute).to.equal(expected.code);
expect(code.revert).to.equal(expected.revertCode);
});
it('creates with expected syntax', () => {
// arrange
let isUsed = false;
const syntax: ILanguageSyntax = {
get commentDelimiters() {
isUsed = true;
return [];
},
get commonCodeParts() {
isUsed = true;
return [];
},
};
const sut = new ScriptCompilerBuilder()
.withSomeFunctions()
.withSyntax(syntax)
.build();
const scriptData = ScriptDataStub.createWithCall();
// act
sut.compile(scriptData);
// assert
expect(isUsed).to.equal(true);
});
it('rethrows error with script name', () => {
// arrange
const scriptName = 'scriptName';
const innerError = 'innerError';
const expectedError = `Script "${scriptName}" ${innerError}`;
const callCompiler: IFunctionCallCompiler = {
compileCall: () => { throw new Error(innerError); },
};
const scriptData = ScriptDataStub.createWithCall()
.withName(scriptName);
const sut = new ScriptCompilerBuilder()
.withSomeFunctions()
.withFunctionCallCompiler(callCompiler)
.build();
// act
const act = () => sut.compile(scriptData);
// assert
expect(act).to.throw(expectedError);
});
it('rethrows error from ScriptCode with script name', () => {
// arrange
const scriptName = 'scriptName';
const expectedError = `Script "${scriptName}" code is empty or undefined`;
const callCompiler: IFunctionCallCompiler = {
compileCall: () => ({ code: undefined, revertCode: undefined }),
};
const scriptData = ScriptDataStub.createWithCall()
.withName(scriptName);
const sut = new ScriptCompilerBuilder()
.withSomeFunctions()
.withFunctionCallCompiler(callCompiler)
.build();
// act
const act = () => sut.compile(scriptData);
// assert
expect(act).to.throw(expectedError);
});
it('returns true if "call" is defined', () => {
// arrange
const sut = new ScriptCompilerBuilder()
.withEmptyFunctions()
.build();
const script = ScriptDataStub.createWithCall();
// act
const actual = sut.canCompile(script);
// assert
expect(actual).to.equal(true);
});
it('returns false if "call" is undefined', () => {
// arrange
const sut = new ScriptCompilerBuilder()
.withEmptyFunctions()
.build();
const script = ScriptDataStub.createWithCode();
// act
const actual = sut.canCompile(script);
// assert
expect(actual).to.equal(false);
});
});
describe('compile', () => {
it('throws if script is undefined', () => {
// arrange
const expectedError = 'undefined script';
const argument = undefined;
const builder = new ScriptCompilerBuilder()
.withEmptyFunctions()
.build();
// act
const act = () => builder.compile(argument);
// assert
expect(act).to.throw(expectedError);
});
it('returns code as expected', () => {
// arrange
const expected: ICompiledCode = {
code: 'expected-code',
revertCode: 'expected-revert-code',
};
const call = new FunctionCallDataStub();
const script = ScriptDataStub.createWithCall(call);
const functions = [FunctionDataStub.createWithCode().withName('existing-func')];
const compiledFunctions = new SharedFunctionCollectionStub();
const functionParserMock = new SharedFunctionsParserStub();
functionParserMock.setup(functions, compiledFunctions);
const callCompilerMock = new FunctionCallCompilerStub();
callCompilerMock.setup(parseFunctionCalls(call), compiledFunctions, expected);
const sut = new ScriptCompilerBuilder()
.withFunctions(...functions)
.withSharedFunctionsParser(functionParserMock)
.withFunctionCallCompiler(callCompilerMock)
.build();
// act
const code = sut.compile(script);
// assert
expect(code.execute).to.equal(expected.code);
expect(code.revert).to.equal(expected.revertCode);
});
it('creates with expected syntax', () => {
// arrange
let isUsed = false;
const syntax: ILanguageSyntax = {
get commentDelimiters() {
isUsed = true;
return [];
},
get commonCodeParts() {
isUsed = true;
return [];
},
};
const sut = new ScriptCompilerBuilder()
.withSomeFunctions()
.withSyntax(syntax)
.build();
const scriptData = ScriptDataStub.createWithCall();
// act
sut.compile(scriptData);
// assert
expect(isUsed).to.equal(true);
});
it('rethrows error with script name', () => {
// arrange
const scriptName = 'scriptName';
const innerError = 'innerError';
const expectedError = `Script "${scriptName}" ${innerError}`;
const callCompiler: IFunctionCallCompiler = {
compileCall: () => { throw new Error(innerError); },
};
const scriptData = ScriptDataStub.createWithCall()
.withName(scriptName);
const sut = new ScriptCompilerBuilder()
.withSomeFunctions()
.withFunctionCallCompiler(callCompiler)
.build();
// act
const act = () => sut.compile(scriptData);
// assert
expect(act).to.throw(expectedError);
});
it('rethrows error from ScriptCode with script name', () => {
// arrange
const scriptName = 'scriptName';
const expectedError = `Script "${scriptName}" code is empty or undefined`;
const callCompiler: IFunctionCallCompiler = {
compileCall: () => ({ code: undefined, revertCode: undefined }),
};
const scriptData = ScriptDataStub.createWithCall()
.withName(scriptName);
const sut = new ScriptCompilerBuilder()
.withSomeFunctions()
.withFunctionCallCompiler(callCompiler)
.build();
// act
const act = () => sut.compile(scriptData);
// assert
expect(act).to.throw(expectedError);
});
});
});
class ScriptCompilerBuilder {
private static createFunctions(...names: string[]): FunctionData[] {
return names.map((functionName) => {
return FunctionDataStub.createWithCode().withName(functionName);
});
}
private functions: FunctionData[];
private syntax: ILanguageSyntax = new LanguageSyntaxStub();
private sharedFunctionsParser: ISharedFunctionsParser = new SharedFunctionsParserStub();
private callCompiler: IFunctionCallCompiler = new FunctionCallCompilerStub();
public withFunctions(...functions: FunctionData[]): ScriptCompilerBuilder {
this.functions = functions;
return this;
}
public withSomeFunctions(): ScriptCompilerBuilder {
this.functions = ScriptCompilerBuilder.createFunctions('test-function');
return this;
}
public withFunctionNames(...functionNames: string[]): ScriptCompilerBuilder {
this.functions = ScriptCompilerBuilder.createFunctions(...functionNames);
return this;
}
public withEmptyFunctions(): ScriptCompilerBuilder {
this.functions = [];
return this;
}
public withSyntax(syntax: ILanguageSyntax): ScriptCompilerBuilder {
this.syntax = syntax;
return this;
}
public withSharedFunctionsParser(SharedFunctionsParser: ISharedFunctionsParser): ScriptCompilerBuilder {
this.sharedFunctionsParser = SharedFunctionsParser;
return this;
}
public withFunctionCallCompiler(callCompiler: IFunctionCallCompiler): ScriptCompilerBuilder {
this.callCompiler = callCompiler;
return this;
}
public build(): ScriptCompiler {
if (!this.functions) {
throw new Error('Function behavior not defined');
}
return new ScriptCompiler(this.functions, this.syntax, this.sharedFunctionsParser, this.callCompiler);
private static createFunctions(...names: string[]): FunctionData[] {
return names.map((functionName) => {
return FunctionDataStub.createWithCode().withName(functionName);
});
}
private functions: FunctionData[];
private syntax: ILanguageSyntax = new LanguageSyntaxStub();
private sharedFunctionsParser: ISharedFunctionsParser = new SharedFunctionsParserStub();
private callCompiler: IFunctionCallCompiler = new FunctionCallCompilerStub();
public withFunctions(...functions: FunctionData[]): ScriptCompilerBuilder {
this.functions = functions;
return this;
}
public withSomeFunctions(): ScriptCompilerBuilder {
this.functions = ScriptCompilerBuilder.createFunctions('test-function');
return this;
}
public withFunctionNames(...functionNames: string[]): ScriptCompilerBuilder {
this.functions = ScriptCompilerBuilder.createFunctions(...functionNames);
return this;
}
public withEmptyFunctions(): ScriptCompilerBuilder {
this.functions = [];
return this;
}
public withSyntax(syntax: ILanguageSyntax): ScriptCompilerBuilder {
this.syntax = syntax;
return this;
}
public withSharedFunctionsParser(
sharedFunctionsParser: ISharedFunctionsParser,
): ScriptCompilerBuilder {
this.sharedFunctionsParser = sharedFunctionsParser;
return this;
}
public withFunctionCallCompiler(callCompiler: IFunctionCallCompiler): ScriptCompilerBuilder {
this.callCompiler = callCompiler;
return this;
}
public build(): ScriptCompiler {
if (!this.functions) {
throw new Error('Function behavior not defined');
}
return new ScriptCompiler(
this.functions,
this.syntax,
this.callCompiler,
this.sharedFunctionsParser,
);
}
}