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

@@ -12,235 +12,242 @@ import { PipelineCompilerStub } from '@tests/unit/stubs/PipelineCompilerStub';
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
describe('Expression', () => {
describe('ctor', () => {
describe('position', () => {
it('throws if undefined', () => {
// arrange
const expectedError = 'undefined position';
const position = undefined;
// act
const act = () => new ExpressionBuilder()
.withPosition(position)
.build();
// assert
expect(act).to.throw(expectedError);
});
it('sets as expected', () => {
// arrange
const expected = new ExpressionPosition(0, 5);
// act
const actual = new ExpressionBuilder()
.withPosition(expected)
.build();
// assert
expect(actual.position).to.equal(expected);
});
});
describe('parameters', () => {
it('defaults to empty array if undefined', () => {
// arrange
const parameters = undefined;
// act
const actual = new ExpressionBuilder()
.withParameters(parameters)
.build();
// assert
expect(actual.parameters);
expect(actual.parameters.all);
expect(actual.parameters.all.length).to.equal(0);
});
it('sets as expected', () => {
// arrange
const expected = new FunctionParameterCollectionStub()
.withParameterName('firstParameterName')
.withParameterName('secondParameterName');
// act
const actual = new ExpressionBuilder()
.withParameters(expected)
.build();
// assert
expect(actual.parameters).to.deep.equal(expected);
});
});
describe('evaluator', () => {
it('throws if undefined', () => {
// arrange
const expectedError = 'undefined evaluator';
const evaluator = undefined;
// act
const act = () => new ExpressionBuilder()
.withEvaluator(evaluator)
.build();
// assert
expect(act).to.throw(expectedError);
});
});
describe('ctor', () => {
describe('position', () => {
it('throws if undefined', () => {
// arrange
const expectedError = 'undefined position';
const position = undefined;
// act
const act = () => new ExpressionBuilder()
.withPosition(position)
.build();
// assert
expect(act).to.throw(expectedError);
});
it('sets as expected', () => {
// arrange
const expected = new ExpressionPosition(0, 5);
// act
const actual = new ExpressionBuilder()
.withPosition(expected)
.build();
// assert
expect(actual.position).to.equal(expected);
});
});
describe('evaluate', () => {
describe('throws with invalid arguments', () => {
const testCases = [
{
name: 'throws if arguments is undefined',
context: undefined,
expectedError: 'undefined context',
},
{
name: 'throws when some of the required args are not provided',
sut: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b', 'c'], false),
context: new ExpressionEvaluationContextStub()
.withArgs(new FunctionCallArgumentCollectionStub().withArgument('b', 'provided')),
expectedError: 'argument values are provided for required parameters: "a", "c"',
},
{
name: 'throws when none of the required args are not provided',
sut: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b'], false),
context: new ExpressionEvaluationContextStub()
.withArgs(new FunctionCallArgumentCollectionStub().withArgument('c', 'unrelated')),
expectedError: 'argument values are provided for required parameters: "a", "b"',
},
];
for (const testCase of testCases) {
it(testCase.name, () => {
// arrange
const sutBuilder = new ExpressionBuilder();
if (testCase.sut) {
testCase.sut(sutBuilder);
}
const sut = sutBuilder.build();
// act
const act = () => sut.evaluate(testCase.context);
// assert
expect(act).to.throw(testCase.expectedError);
});
}
});
it('returns result from evaluator', () => {
// arrange
const evaluatorMock: ExpressionEvaluator = (c) =>
`"${c
.args
.getAllParameterNames()
.map((name) => context.args.getArgument(name))
.map((arg) => `${arg.parameterName}': '${arg.argumentValue}'`)
.join('", "')}"`;
const givenArguments = new FunctionCallArgumentCollectionStub()
.withArgument('parameter1', 'value1')
.withArgument('parameter2', 'value2');
const expectedParameterNames = givenArguments.getAllParameterNames();
const context = new ExpressionEvaluationContextStub()
.withArgs(givenArguments);
const expected = evaluatorMock(context);
const sut = new ExpressionBuilder()
.withEvaluator(evaluatorMock)
.withParameterNames(expectedParameterNames)
.build();
// arrange
const actual = sut.evaluate(context);
// assert
expect(expected).to.equal(actual,
`\nGiven arguments: ${JSON.stringify(givenArguments)}\n` +
`\nExpected parameter names: ${JSON.stringify(expectedParameterNames)}\n`,
);
});
it('sends pipeline compiler as it is', () => {
// arrange
const expected = new PipelineCompilerStub();
const context = new ExpressionEvaluationContextStub()
.withPipelineCompiler(expected);
let actual: IPipelineCompiler;
const evaluatorMock: ExpressionEvaluator = (c) => {
actual = c.pipelineCompiler;
return '';
};
const sut = new ExpressionBuilder()
.withEvaluator(evaluatorMock)
.build();
// arrange
sut.evaluate(context);
// assert
expect(expected).to.equal(actual);
});
describe('filters unused parameters', () => {
// arrange
const testCases = [
{
name: 'with a provided argument',
expressionParameters: new FunctionParameterCollectionStub()
.withParameterName('parameterToHave', false),
arguments: new FunctionCallArgumentCollectionStub()
.withArgument('parameterToHave', 'value-to-have')
.withArgument('parameterToIgnore', 'value-to-ignore'),
expectedArguments: [
new FunctionCallArgumentStub()
.withParameterName('parameterToHave').withArgumentValue('value-to-have'),
],
},
{
name: 'without a provided argument',
expressionParameters: new FunctionParameterCollectionStub()
.withParameterName('parameterToHave', false)
.withParameterName('parameterToIgnore', true),
arguments: new FunctionCallArgumentCollectionStub()
.withArgument('parameterToHave', 'value-to-have'),
expectedArguments: [
new FunctionCallArgumentStub()
.withParameterName('parameterToHave').withArgumentValue('value-to-have'),
],
},
];
for (const testCase of testCases) {
it(testCase.name, () => {
let actual: IReadOnlyFunctionCallArgumentCollection;
const evaluatorMock: ExpressionEvaluator = (c) => {
actual = c.args;
return '';
};
const context = new ExpressionEvaluationContextStub()
.withArgs(testCase.arguments);
const sut = new ExpressionBuilder()
.withEvaluator(evaluatorMock)
.withParameters(testCase.expressionParameters)
.build();
// act
sut.evaluate(context);
// assert
const actualArguments = actual.getAllParameterNames().map((name) => actual.getArgument(name));
expect(actualArguments).to.deep.equal(testCase.expectedArguments);
});
}
});
describe('parameters', () => {
it('defaults to empty array if undefined', () => {
// arrange
const parameters = undefined;
// act
const actual = new ExpressionBuilder()
.withParameters(parameters)
.build();
// assert
expect(actual.parameters);
expect(actual.parameters.all);
expect(actual.parameters.all.length).to.equal(0);
});
it('sets as expected', () => {
// arrange
const expected = new FunctionParameterCollectionStub()
.withParameterName('firstParameterName')
.withParameterName('secondParameterName');
// act
const actual = new ExpressionBuilder()
.withParameters(expected)
.build();
// assert
expect(actual.parameters).to.deep.equal(expected);
});
});
describe('evaluator', () => {
it('throws if undefined', () => {
// arrange
const expectedError = 'undefined evaluator';
const evaluator = undefined;
// act
const act = () => new ExpressionBuilder()
.withEvaluator(evaluator)
.build();
// assert
expect(act).to.throw(expectedError);
});
});
});
describe('evaluate', () => {
describe('throws with invalid arguments', () => {
const testCases = [
{
name: 'throws if arguments is undefined',
context: undefined,
expectedError: 'undefined context',
},
{
name: 'throws when some of the required args are not provided',
sut: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b', 'c'], false),
context: new ExpressionEvaluationContextStub()
.withArgs(new FunctionCallArgumentCollectionStub().withArgument('b', 'provided')),
expectedError: 'argument values are provided for required parameters: "a", "c"',
},
{
name: 'throws when none of the required args are not provided',
sut: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b'], false),
context: new ExpressionEvaluationContextStub()
.withArgs(new FunctionCallArgumentCollectionStub().withArgument('c', 'unrelated')),
expectedError: 'argument values are provided for required parameters: "a", "b"',
},
];
for (const testCase of testCases) {
it(testCase.name, () => {
// arrange
const sutBuilder = new ExpressionBuilder();
if (testCase.sut) {
testCase.sut(sutBuilder);
}
const sut = sutBuilder.build();
// act
const act = () => sut.evaluate(testCase.context);
// assert
expect(act).to.throw(testCase.expectedError);
});
}
});
it('returns result from evaluator', () => {
// arrange
const evaluatorMock: ExpressionEvaluator = (c) => `"${c
.args
.getAllParameterNames()
.map((name) => context.args.getArgument(name))
.map((arg) => `${arg.parameterName}': '${arg.argumentValue}'`)
.join('", "')}"`;
const givenArguments = new FunctionCallArgumentCollectionStub()
.withArgument('parameter1', 'value1')
.withArgument('parameter2', 'value2');
const expectedParameterNames = givenArguments.getAllParameterNames();
const context = new ExpressionEvaluationContextStub()
.withArgs(givenArguments);
const expected = evaluatorMock(context);
const sut = new ExpressionBuilder()
.withEvaluator(evaluatorMock)
.withParameterNames(expectedParameterNames)
.build();
// arrange
const actual = sut.evaluate(context);
// assert
expect(expected).to.equal(actual, printMessage());
function printMessage(): string {
return `\nGiven arguments: ${JSON.stringify(givenArguments)}\n`
+ `\nExpected parameter names: ${JSON.stringify(expectedParameterNames)}\n`;
}
});
it('sends pipeline compiler as it is', () => {
// arrange
const expected = new PipelineCompilerStub();
const context = new ExpressionEvaluationContextStub()
.withPipelineCompiler(expected);
let actual: IPipelineCompiler;
const evaluatorMock: ExpressionEvaluator = (c) => {
actual = c.pipelineCompiler;
return '';
};
const sut = new ExpressionBuilder()
.withEvaluator(evaluatorMock)
.build();
// arrange
sut.evaluate(context);
// assert
expect(expected).to.equal(actual);
});
describe('filters unused parameters', () => {
// arrange
const testCases = [
{
name: 'with a provided argument',
expressionParameters: new FunctionParameterCollectionStub()
.withParameterName('parameterToHave', false),
arguments: new FunctionCallArgumentCollectionStub()
.withArgument('parameterToHave', 'value-to-have')
.withArgument('parameterToIgnore', 'value-to-ignore'),
expectedArguments: [
new FunctionCallArgumentStub()
.withParameterName('parameterToHave').withArgumentValue('value-to-have'),
],
},
{
name: 'without a provided argument',
expressionParameters: new FunctionParameterCollectionStub()
.withParameterName('parameterToHave', false)
.withParameterName('parameterToIgnore', true),
arguments: new FunctionCallArgumentCollectionStub()
.withArgument('parameterToHave', 'value-to-have'),
expectedArguments: [
new FunctionCallArgumentStub()
.withParameterName('parameterToHave').withArgumentValue('value-to-have'),
],
},
];
for (const testCase of testCases) {
it(testCase.name, () => {
let actual: IReadOnlyFunctionCallArgumentCollection;
const evaluatorMock: ExpressionEvaluator = (c) => {
actual = c.args;
return '';
};
const context = new ExpressionEvaluationContextStub()
.withArgs(testCase.arguments);
const sut = new ExpressionBuilder()
.withEvaluator(evaluatorMock)
.withParameters(testCase.expressionParameters)
.build();
// act
sut.evaluate(context);
// assert
const actualArguments = actual.getAllParameterNames()
.map((name) => actual.getArgument(name));
expect(actualArguments).to.deep.equal(testCase.expectedArguments);
});
}
});
});
});
class ExpressionBuilder {
private position: ExpressionPosition = new ExpressionPosition(0, 5);
private parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub();
private position: ExpressionPosition = new ExpressionPosition(0, 5);
public withPosition(position: ExpressionPosition) {
this.position = position;
return this;
}
public withEvaluator(evaluator: ExpressionEvaluator) {
this.evaluator = evaluator;
return this;
}
public withParameters(parameters: IReadOnlyFunctionParameterCollection) {
this.parameters = parameters;
return this;
}
public withParameterName(parameterName: string, isOptional: boolean = true) {
const collection = new FunctionParameterCollectionStub()
.withParameterName(parameterName, isOptional);
return this.withParameters(collection);
}
public withParameterNames(parameterNames: string[], isOptional: boolean = true) {
const collection = new FunctionParameterCollectionStub()
.withParameterNames(parameterNames, isOptional);
return this.withParameters(collection);
}
public build() {
return new Expression(this.position, this.evaluator, this.parameters);
}
private parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub();
private evaluator: ExpressionEvaluator = () => '';
public withPosition(position: ExpressionPosition) {
this.position = position;
return this;
}
public withEvaluator(evaluator: ExpressionEvaluator) {
this.evaluator = evaluator;
return this;
}
public withParameters(parameters: IReadOnlyFunctionParameterCollection) {
this.parameters = parameters;
return this;
}
public withParameterName(parameterName: string, isOptional = true) {
const collection = new FunctionParameterCollectionStub()
.withParameterName(parameterName, isOptional);
return this.withParameters(collection);
}
public withParameterNames(parameterNames: string[], isOptional = true) {
const collection = new FunctionParameterCollectionStub()
.withParameterNames(parameterNames, isOptional);
return this.withParameters(collection);
}
public build() {
return new Expression(this.position, this.evaluator, this.parameters);
}
private evaluator: ExpressionEvaluator = () => '';
}

View File

@@ -6,60 +6,63 @@ import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressi
import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub';
import { PipelineCompilerStub } from '@tests/unit/stubs/PipelineCompilerStub';
describe('ExpressionEvaluationContext', () => {
describe('ctor', () => {
describe('args', () => {
it('throws if args are undefined', () => {
// arrange
const expectedError = 'undefined args';
const builder = new ExpressionEvaluationContextBuilder()
.withArgs(undefined);
// act
const act = () => builder.build();
// assert
expect(act).throw(expectedError);
});
it('sets as expected', () => {
// arrange
const expected = new FunctionCallArgumentCollectionStub()
.withArgument('expectedParameter', 'expectedValue');
const builder = new ExpressionEvaluationContextBuilder()
.withArgs(expected);
// act
const sut = builder.build();
// assert
const actual = sut.args;
expect(actual).to.equal(expected);
});
});
describe('pipelineCompiler', () => {
it('sets as expected', () => {
// arrange
const expected = new PipelineCompilerStub();
const builder = new ExpressionEvaluationContextBuilder()
.withPipelineCompiler(expected);
// act
const sut = builder.build();
// assert
expect(sut.pipelineCompiler).to.equal(expected);
});
});
describe('ctor', () => {
describe('args', () => {
it('throws if args are undefined', () => {
// arrange
const expectedError = 'undefined args';
const builder = new ExpressionEvaluationContextBuilder()
.withArgs(undefined);
// act
const act = () => builder.build();
// assert
expect(act).throw(expectedError);
});
it('sets as expected', () => {
// arrange
const expected = new FunctionCallArgumentCollectionStub()
.withArgument('expectedParameter', 'expectedValue');
const builder = new ExpressionEvaluationContextBuilder()
.withArgs(expected);
// act
const sut = builder.build();
// assert
const actual = sut.args;
expect(actual).to.equal(expected);
});
});
describe('pipelineCompiler', () => {
it('sets as expected', () => {
// arrange
const expected = new PipelineCompilerStub();
const builder = new ExpressionEvaluationContextBuilder()
.withPipelineCompiler(expected);
// act
const sut = builder.build();
// assert
expect(sut.pipelineCompiler).to.equal(expected);
});
});
});
});
class ExpressionEvaluationContextBuilder {
private args: IReadOnlyFunctionCallArgumentCollection = new FunctionCallArgumentCollectionStub();
private pipelineCompiler: IPipelineCompiler = new PipelineCompilerStub();
public withArgs(args: IReadOnlyFunctionCallArgumentCollection) {
this.args = args;
return this;
}
public withPipelineCompiler(pipelineCompiler: IPipelineCompiler) {
this.pipelineCompiler = pipelineCompiler;
return this;
}
public build(): IExpressionEvaluationContext {
return new ExpressionEvaluationContext(this.args, this.pipelineCompiler);
}
private args: IReadOnlyFunctionCallArgumentCollection = new FunctionCallArgumentCollectionStub();
private pipelineCompiler: IPipelineCompiler = new PipelineCompilerStub();
public withArgs(args: IReadOnlyFunctionCallArgumentCollection) {
this.args = args;
return this;
}
public withPipelineCompiler(pipelineCompiler: IPipelineCompiler) {
this.pipelineCompiler = pipelineCompiler;
return this;
}
public build(): IExpressionEvaluationContext {
return new ExpressionEvaluationContext(this.args, this.pipelineCompiler);
}
}

View File

@@ -3,32 +3,32 @@ import { expect } from 'chai';
import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
describe('ExpressionPosition', () => {
describe('ctor', () => {
it('sets as expected', () => {
// arrange
const expectedStart = 0;
const expectedEnd = 5;
// act
const sut = new ExpressionPosition(expectedStart, expectedEnd);
// assert
expect(sut.start).to.equal(expectedStart);
expect(sut.end).to.equal(expectedEnd);
});
describe('throws when invalid', () => {
// arrange
const testCases = [
{ start: 5, end: 5, error: 'no length (start = end = 5)' },
{ start: 5, end: 3, error: 'start (5) after end (3)' },
{ start: -1, end: 3, error: 'negative start position: -1' },
];
for (const testCase of testCases) {
it(testCase.error, () => {
// act
const act = () => new ExpressionPosition(testCase.start, testCase.end);
// assert
expect(act).to.throw(testCase.error);
});
}
});
describe('ctor', () => {
it('sets as expected', () => {
// arrange
const expectedStart = 0;
const expectedEnd = 5;
// act
const sut = new ExpressionPosition(expectedStart, expectedEnd);
// assert
expect(sut.start).to.equal(expectedStart);
expect(sut.end).to.equal(expectedEnd);
});
describe('throws when invalid', () => {
// arrange
const testCases = [
{ start: 5, end: 5, error: 'no length (start = end = 5)' },
{ start: 5, end: 3, error: 'start (5) after end (3)' },
{ start: -1, end: 3, error: 'negative start position: -1' },
];
for (const testCase of testCases) {
it(testCase.error, () => {
// act
const act = () => new ExpressionPosition(testCase.start, testCase.end);
// assert
expect(act).to.throw(testCase.error);
});
}
});
});
});