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:
@@ -4,66 +4,67 @@ import WindowsData from 'raw-loader!@/application/collections/windows.yaml';
|
||||
import MacOsData from 'raw-loader!@/application/collections/macos.yaml';
|
||||
|
||||
/*
|
||||
A common mistake when working with yaml files to forget mentioning that a value should
|
||||
be interpreted as multi-line string using "|".
|
||||
E.g.
|
||||
```
|
||||
code: |-
|
||||
echo Hello
|
||||
echo World
|
||||
```
|
||||
If "|" is missing then the code is inlined like `echo Hello echo World``, which can be
|
||||
unintended. This test checks for similar issues in collection yaml files.
|
||||
These tests can be considered as "linter" more than "unit-test" and therefore can lead
|
||||
to false-positives.
|
||||
A common mistake when working with yaml files to forget mentioning that a value should
|
||||
be interpreted as multi-line string using "|".
|
||||
E.g.
|
||||
```
|
||||
code: |-
|
||||
echo Hello
|
||||
echo World
|
||||
```
|
||||
If "|" is missing then the code is inlined like `echo Hello echo World``, which can be
|
||||
unintended. This test checks for similar issues in collection yaml files.
|
||||
These tests can be considered as "linter" more than "unit-test" and therefore can lead
|
||||
to false-positives.
|
||||
*/
|
||||
describe('collection files to have no unintended inlining', async () => {
|
||||
// arrange
|
||||
const testCases = [ {
|
||||
name: 'macos',
|
||||
fileContent: MacOsData,
|
||||
}, {
|
||||
name: 'windows',
|
||||
fileContent: WindowsData,
|
||||
},
|
||||
];
|
||||
for (const testCase of testCases) {
|
||||
it(`${testCase.name}`, async () => {
|
||||
const lines = await findBadLineNumbers(testCase.fileContent);
|
||||
expect(lines).to.be.have.lengthOf(0,
|
||||
`Did you intend to have multi-lined string in lines: `
|
||||
+ lines.map(((line) => line.toString())).join(', '),
|
||||
);
|
||||
});
|
||||
}
|
||||
// arrange
|
||||
const testCases = [{
|
||||
name: 'macos',
|
||||
fileContent: MacOsData,
|
||||
}, {
|
||||
name: 'windows',
|
||||
fileContent: WindowsData,
|
||||
},
|
||||
];
|
||||
for (const testCase of testCases) {
|
||||
it(`${testCase.name}`, async () => {
|
||||
const lines = await findBadLineNumbers(testCase.fileContent);
|
||||
// assert
|
||||
expect(lines).to.be.have.lengthOf(0, printMessage());
|
||||
function printMessage(): string {
|
||||
return 'Did you intend to have multi-lined string in lines: ' // eslint-disable-line prefer-template
|
||||
+ lines.map(((line) => line.toString())).join(', ');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
async function findBadLineNumbers(fileContent: string): Promise<number[]> {
|
||||
return [
|
||||
...findLineNumbersEndingWith(fileContent, 'revertCode:'),
|
||||
...findLineNumbersEndingWith(fileContent, 'code:'),
|
||||
];
|
||||
return [
|
||||
...findLineNumbersEndingWith(fileContent, 'revertCode:'),
|
||||
...findLineNumbersEndingWith(fileContent, 'code:'),
|
||||
];
|
||||
}
|
||||
|
||||
function findLineNumbersEndingWith(content: string, ending: string): number[] {
|
||||
sanityCheck(content, ending);
|
||||
const lines = content.split(/\r\n|\r|\n/);
|
||||
const results = new Array<number>();
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (line.trim().endsWith(ending)) {
|
||||
results.push((i + 1 /* first line is 1 not 0 */));
|
||||
}
|
||||
sanityCheck(content, ending);
|
||||
const lines = content.split(/\r\n|\r|\n/);
|
||||
const results = new Array<number>();
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (line.trim().endsWith(ending)) {
|
||||
results.push((i + 1 /* first line is 1 not 0 */));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
function sanityCheck(content: string, ending: string): void {
|
||||
if (!content.includes(ending)) {
|
||||
throw new Error(
|
||||
`File does not contain string "${ending}" string at all.`
|
||||
+ `Did the word "${ending}" change? Or is this sanity check wrong?`,
|
||||
);
|
||||
}
|
||||
if (!content.includes(ending)) {
|
||||
throw new Error(
|
||||
`File does not contain string "${ending}" string at all.`
|
||||
+ `Did the word "${ending}" change? Or is this sanity check wrong?`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
declare module 'raw-loader!@/*' {
|
||||
const contents: string;
|
||||
export default contents;
|
||||
const contents: string;
|
||||
export default contents;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user