Skip to content

Commit

Permalink
chore(eslint): add rules for async-await (#932)
Browse files Browse the repository at this point in the history
<!-- For Coveo Employees only. Fill this section.

CDX-515

-->

## Proposed changes

Add two eslint rules:
- https://eslint.org/docs/rules/require-await - async method without any
await will be flagged.
-
https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/await-thenable.md
- await on awaitable (aka thenable) will be flagged

Also fix eslint for atomic final project (eslint config of the template
was not 'standalone')

## Breaking changes

<!--
    Remove this section if the PR does not include any breaking change

If your changes includes some breaking changes in the code, thoroughly
explains:
        - What are the breaking changes programmatically speaking.
- What is the impact on the end-user (e.g. user cannot do X anymore).
        - What motivates those changes.
-->

## Testing

- [ ] Unit Tests:
<!-- Did you write unit tests for your feature? If not, explains why?
-->
- [ ] Functional Tests:
<!-- Did you write functional tests for your feature? If not, explains
why? -->
- [ ] Manual Tests:
<!-- How did you test your changeset?  -->
  • Loading branch information
louis-bompart authored Sep 16, 2022
1 parent 3dbf3fe commit 1097f58
Show file tree
Hide file tree
Showing 55 changed files with 354 additions and 286 deletions.
6 changes: 5 additions & 1 deletion base.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
{"ignoreRestSiblings": true, "argsIgnorePattern": "^_"}
],
"@typescript-eslint/explicit-member-accessibility": "error",
"@typescript-eslint/prefer-for-of": "error"
"@typescript-eslint/prefer-for-of": "error",
"@typescript-eslint/await-thenable": "error",
"require-await": [
"error"
]
}
},
{
Expand Down
3 changes: 3 additions & 0 deletions packages/angular/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
},
"rules": {
"node/no-unpublished-import": ["off"]
},
"parserOptions": {
"project": "tsconfig.json"
}
}
3 changes: 3 additions & 0 deletions packages/atomic/create-atomic/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"extends": "../../../base.eslintrc",
"env": {
"browser": true
},
"parserOptions": {
"project": "tsconfig.json"
}
}
11 changes: 11 additions & 0 deletions packages/atomic/create-atomic/scripts/!.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./node_modules/gts",
"overrides": [
{
"files": "stencil.config.ts",
"rules": {
"node/no-unpublished-import": "off"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs-extra';
const {readFileSync, writeFileSync, copySync} = fs;
const {readFileSync, writeFileSync, copySync, copyFileSync} = fs;
import {join, resolve, dirname} from 'path';
import detectIndent from 'detect-indent';
import {fileURLToPath} from 'url';
Expand All @@ -9,6 +9,10 @@ const atomicTemplatePath = resolve(__dirname, '..', '..', 'template');
const bundledTemplatePath = resolve(__dirname, '..', 'template');

copySync(atomicTemplatePath, bundledTemplatePath, {recursive: true});
copyFileSync(
join(__dirname, '!.eslintrc'),
join(bundledTemplatePath, '.eslintrc')
);

const packageJson = readFileSync(
join(bundledTemplatePath, 'package.json'),
Expand Down
2 changes: 1 addition & 1 deletion packages/atomic/create-atomic/src/plopfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface PlopData {
page: IManifest;
}

export default async function (plop: NodePlopAPI) {
export default function (plop: NodePlopAPI) {
const currentPath = process.cwd();
let platformClientInstance: PlatformClient;

Expand Down
5 changes: 4 additions & 1 deletion packages/atomic/template/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"node/no-unpublished-import": "off"
}
}
]
],
"parserOptions": {
"project": "tsconfig.donotcopy.json"
}
}
21 changes: 21 additions & 0 deletions packages/atomic/template/tsconfig.donotcopy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": "../../../node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": false,
"experimentalDecorators": true,
"lib": ["dom", "es2018"],
"moduleResolution": "node",
"module": "esnext",
"target": "es2018",
"noUnusedLocals": true,
"noUnusedParameters": true,
"jsx": "react",
"jsxFactory": "h",
"paths": {
"@coveo/headless": ["../../../node_modules/@coveo/atomic/headless"]
}
},
"include": ["stencil.config.ts", "src"],
"exclude": ["node_modules", "lambda"]
}
3 changes: 3 additions & 0 deletions packages/cli-e2e/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"extends": "../../base.eslintrc",
"env": {
"es6": true
},
"parserOptions": {
"project": "tsconfig.json"
}
}
30 changes: 15 additions & 15 deletions packages/cli-e2e/__tests__/angular.specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('ui:create:angular', () => {
.until(buildTerminalExitPromise);
};

const startApplication = async (
const startApplication = (
processManager: ProcessManager,
debugName = 'angular-server'
) => {
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('ui:create:angular', () => {
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
'angular-server-valid'
);
Expand Down Expand Up @@ -285,7 +285,7 @@ describe('ui:create:angular', () => {
await page.keyboard.type('my query');
await page.keyboard.press('Enter');

await retry(async () => {
await retry(() => {
expect(
interceptedRequests.some(isSearchRequestOrResponse)
).toBeTruthy();
Expand All @@ -309,7 +309,7 @@ describe('ui:create:angular', () => {
describe('when the .env file is missing', () => {
let serverProcessManager: ProcessManager;

beforeAll(async () => {
beforeAll(() => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
deactivateEnvironmentFile(join(projectPath, 'server'));
Expand All @@ -325,7 +325,7 @@ describe('ui:create:angular', () => {
async () => {
const missingEnvErrorSpy = jest.fn();

const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
'angular-server-missing-env'
);
Expand All @@ -350,7 +350,7 @@ describe('ui:create:angular', () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
envFileContent = flushEnvFile(join(projectPath, 'server'));
const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
'angular-server-invalid'
);
Expand Down Expand Up @@ -379,7 +379,7 @@ describe('ui:create:angular', () => {
processManagers.push(serverProcessManager);
setCustomTokenEndpoint(customTokenEndpoint);

const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
'angular-server-port-test'
);
Expand All @@ -392,21 +392,21 @@ describe('ui:create:angular', () => {
resetCustomTokenEndpoint();
}, 30e3);

beforeEach(async () => {
beforeEach(() => {
page.on('request', (request: HTTPRequest) => {
interceptedRequests.push(request);
});
});

afterEach(async () => {
afterEach(() => {
page.removeAllListeners('request');
interceptedRequests = [];
});

it('should use the custom token endpoint', async () => {
page.goto(searchPageEndpoint());

await retry(async () => {
await retry(() => {
expect(
interceptedRequests.some(
(request: HTTPRequest) => request.url() === customTokenEndpoint
Expand Down Expand Up @@ -436,7 +436,7 @@ describe('ui:create:angular', () => {
);
await Promise.all(dummyServers.map((server) => server.start()));

const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
'angular-server-port-test'
);
Expand All @@ -449,19 +449,19 @@ describe('ui:create:angular', () => {
await serverProcessManager.killAllProcesses();
}, 30e3);

it('should allocate a new port for the application', async () => {
it('should allocate a new port for the application', () => {
expect(clientPort).not.toEqual(usedClientPort);
});

it('should not use an undefined port for application', async () => {
it('should not use an undefined port for application', () => {
expect(clientPort).not.toBeUndefined();
});

it('should allocate a new port for the token server', async () => {
it('should allocate a new port for the token server', () => {
expect(serverPort).not.toEqual(usedServerPort);
});

it('should not use an undefined port for token server', async () => {
it('should not use an undefined port for token server', () => {
expect(serverPort).not.toBeUndefined();
});

Expand Down
8 changes: 4 additions & 4 deletions packages/cli-e2e/__tests__/atomic.specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('ui:create:atomic', () => {
const getFlagsFromOptions = (options: BuildAppOptions) =>
options.pageId ? ['--pageId', options.pageId] : undefined;

const selectDefaultWithCli = async (buildTerminal: Terminal) => {
const selectDefaultWithCli = (buildTerminal: Terminal) => {
buildTerminal
.when(/Use an existing hosted search page/)
.on('stdout')
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('ui:create:atomic', () => {
return {output};
};

const startApplication = async (
const startApplication = (
processManager: ProcessManager,
options: BuildAppOptions,
debugName = 'atomic-server'
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('ui:create:atomic', () => {
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
buildAppOptions,
'atomic-server-valid'
Expand Down Expand Up @@ -370,7 +370,7 @@ describe('ui:create:atomic', () => {
dummyServer = new DummyServer(3333);
await dummyServer.start();

const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
buildAppOptions,
'stencil-port-test'
Expand Down
28 changes: 14 additions & 14 deletions packages/cli-e2e/__tests__/react.specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('ui:create:react', () => {
await buildTerminal.when('exit').on('process').do().once();
};

const startApplication = async (
const startApplication = (
processManager: ProcessManager,
debugName = 'react-server'
) => {
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('ui:create:react', () => {
beforeAll(async () => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
'react-server-valid'
);
Expand Down Expand Up @@ -230,7 +230,7 @@ describe('ui:create:react', () => {
await page.keyboard.type('my query');
await page.keyboard.press('Enter');

await retry(async () => {
await retry(() => {
expect(
interceptedRequests.some(isSearchRequestOrResponse)
).toBeTruthy();
Expand All @@ -253,7 +253,7 @@ describe('ui:create:react', () => {
describe('when the .env file is missing', () => {
let serverProcessManager: ProcessManager;

beforeAll(async () => {
beforeAll(() => {
serverProcessManager = new ProcessManager();
processManagers.push(serverProcessManager);
deactivateEnvironmentFile(projectPath);
Expand All @@ -269,7 +269,7 @@ describe('ui:create:react', () => {
async () => {
const missingEnvErrorSpy = jest.fn();

const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
'react-server-missing-env'
);
Expand All @@ -296,7 +296,7 @@ describe('ui:create:react', () => {
processManagers.push(serverProcessManager);
envFileContent = flushEnvFile(projectPath);
overwriteEnvFile(projectPath, 'GENERATE_SOURCEMAP=false'); // TODO: CDX-737: fix exponential-backoff compilation warnings
const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
'react-server-invalid'
);
Expand Down Expand Up @@ -333,7 +333,7 @@ describe('ui:create:react', () => {
processManagers.push(serverProcessManager);
forceApplicationPorts(hardCodedClientPort, hardCodedServerPort);

const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
'react-server-port-test'
);
Expand All @@ -345,11 +345,11 @@ describe('ui:create:react', () => {
await serverProcessManager.killAllProcesses();
}, 30e3);

it('should run the application on the specified port', async () => {
it('should run the application on the specified port', () => {
expect(clientPort).toEqual(hardCodedClientPort);
});

it('should run the token server on the specified port', async () => {
it('should run the token server on the specified port', () => {
expect(serverPort).toEqual(hardCodedServerPort);
});
});
Expand All @@ -373,7 +373,7 @@ describe('ui:create:react', () => {
);
await Promise.all(dummyServers.map((server) => server.start()));

const appTerminal = await startApplication(
const appTerminal = startApplication(
serverProcessManager,
'react-server-port-test'
);
Expand All @@ -386,19 +386,19 @@ describe('ui:create:react', () => {
await serverProcessManager.killAllProcesses();
}, 30e3);

it('should allocate a new port for the application', async () => {
it('should allocate a new port for the application', () => {
expect(clientPort).not.toEqual(usedClientPort);
});

it('should not use an undefined port for application', async () => {
it('should not use an undefined port for application', () => {
expect(clientPort).not.toBeUndefined();
});

it('should allocate a new port for the token server', async () => {
it('should allocate a new port for the token server', () => {
expect(serverPort).not.toEqual(usedServerPort);
});

it('should not use an undefined port for token server', async () => {
it('should not use an undefined port for token server', () => {
expect(serverPort).not.toBeUndefined();
});

Expand Down
Loading

0 comments on commit 1097f58

Please sign in to comment.