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,85 +6,85 @@ import { FunctionBodyType } from '@/application/Parser/Script/Compiler/Function/
import { FunctionCallStub } from '@tests/unit/stubs/FunctionCallStub';
describe('SharedFunctionCollection', () => {
describe('addFunction', () => {
it('throws if function is undefined', () => {
// arrange
const expectedError = 'undefined function';
const func = undefined;
const sut = new SharedFunctionCollection();
// act
const act = () => sut.addFunction(func);
// assert
expect(act).to.throw(expectedError);
});
it('throws if function with same name already exists', () => {
// arrange
const functionName = 'duplicate-function';
const expectedError = `function with name ${functionName} already exists`;
const func = new SharedFunctionStub(FunctionBodyType.Code)
.withName('duplicate-function');
const sut = new SharedFunctionCollection();
sut.addFunction(func);
// act
const act = () => sut.addFunction(func);
// assert
expect(act).to.throw(expectedError);
});
describe('addFunction', () => {
it('throws if function is undefined', () => {
// arrange
const expectedError = 'undefined function';
const func = undefined;
const sut = new SharedFunctionCollection();
// act
const act = () => sut.addFunction(func);
// assert
expect(act).to.throw(expectedError);
});
describe('getFunctionByName', () => {
it('throws if name is undefined', () => {
// arrange
const expectedError = 'undefined function name';
const invalidValues = [ undefined, '' ];
const sut = new SharedFunctionCollection();
for (const invalidValue of invalidValues) {
const name = invalidValue;
// act
const act = () => sut.getFunctionByName(name);
// assert
expect(act).to.throw(expectedError);
}
});
it('throws if function does not exist', () => {
// arrange
const name = 'unique-name';
const expectedError = `called function is not defined "${name}"`;
const func = new SharedFunctionStub(FunctionBodyType.Code)
.withName('unexpected-name');
const sut = new SharedFunctionCollection();
sut.addFunction(func);
// act
const act = () => sut.getFunctionByName(name);
// assert
expect(act).to.throw(expectedError);
});
describe('returns existing function', () => {
it('when function with inline code is added', () => {
// arrange
const expected = new SharedFunctionStub(FunctionBodyType.Code)
.withName('expected-function-name');
const sut = new SharedFunctionCollection();
// act
sut.addFunction(expected);
const actual = sut.getFunctionByName(expected.name);
// assert
expect(actual).to.equal(expected);
});
it('when calling function is added', () => {
// arrange
const callee = new SharedFunctionStub(FunctionBodyType.Code)
.withName('calleeFunction');
const caller = new SharedFunctionStub(FunctionBodyType.Calls)
.withName('callerFunction')
.withCalls(new FunctionCallStub().withFunctionName(callee.name));
const sut = new SharedFunctionCollection();
// act
sut.addFunction(callee);
sut.addFunction(caller);
const actual = sut.getFunctionByName(caller.name);
// assert
expect(actual).to.equal(caller);
});
});
it('throws if function with same name already exists', () => {
// arrange
const functionName = 'duplicate-function';
const expectedError = `function with name ${functionName} already exists`;
const func = new SharedFunctionStub(FunctionBodyType.Code)
.withName('duplicate-function');
const sut = new SharedFunctionCollection();
sut.addFunction(func);
// act
const act = () => sut.addFunction(func);
// assert
expect(act).to.throw(expectedError);
});
});
describe('getFunctionByName', () => {
it('throws if name is undefined', () => {
// arrange
const expectedError = 'undefined function name';
const invalidValues = [undefined, ''];
const sut = new SharedFunctionCollection();
for (const invalidValue of invalidValues) {
const name = invalidValue;
// act
const act = () => sut.getFunctionByName(name);
// assert
expect(act).to.throw(expectedError);
}
});
it('throws if function does not exist', () => {
// arrange
const name = 'unique-name';
const expectedError = `called function is not defined "${name}"`;
const func = new SharedFunctionStub(FunctionBodyType.Code)
.withName('unexpected-name');
const sut = new SharedFunctionCollection();
sut.addFunction(func);
// act
const act = () => sut.getFunctionByName(name);
// assert
expect(act).to.throw(expectedError);
});
describe('returns existing function', () => {
it('when function with inline code is added', () => {
// arrange
const expected = new SharedFunctionStub(FunctionBodyType.Code)
.withName('expected-function-name');
const sut = new SharedFunctionCollection();
// act
sut.addFunction(expected);
const actual = sut.getFunctionByName(expected.name);
// assert
expect(actual).to.equal(expected);
});
it('when calling function is added', () => {
// arrange
const callee = new SharedFunctionStub(FunctionBodyType.Code)
.withName('calleeFunction');
const caller = new SharedFunctionStub(FunctionBodyType.Calls)
.withName('callerFunction')
.withCalls(new FunctionCallStub().withFunctionName(callee.name));
const sut = new SharedFunctionCollection();
// act
sut.addFunction(callee);
sut.addFunction(caller);
const actual = sut.getFunctionByName(caller.name);
// assert
expect(actual).to.equal(caller);
});
});
});
});