Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-config): enable "no-floating-promises" rule #3219

Merged
merged 2 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bin/run-lerna.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ async function run(argv, options) {
}

module.exports = run;
if (require.main === module) run(process.argv);
if (require.main === module) {
run(process.argv).catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion bin/sync-dev-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ function updatePackageJson(pkgFile, masterDeps) {
return true;
}

if (require.main === module) syncDevDeps();
if (require.main === module) {
syncDevDeps().catch(err => {
console.error(err);
process.exit(1);
});
}

function readPackageJson(filePath) {
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
Expand Down
7 changes: 6 additions & 1 deletion bin/update-template-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ async function updateTemplateDeps() {
}
}

if (require.main === module) updateTemplateDeps();
if (require.main === module) {
updateTemplateDeps().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/binding-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,9 @@ export async function main() {
console.log(greeter.hello());
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/configuration-injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ export async function main() {
console.log(greeter.greet('Ray'));
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/context-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ export async function main() {
console.log(greeter.greet('John'));
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/context-observation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,9 @@ export async function main() {
await requestCtx.waitUntilObserversNotified();
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/custom-configuration-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ export async function main() {
console.log(barConfig);
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/custom-inject-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ export async function main() {
console.log(greeter.hello());
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/custom-inject-resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ export async function main() {
console.log(greeter.hello());
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/dependency-injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,9 @@ export async function main() {
console.log(greeting);
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/find-bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ export async function main() {
console.log(view.bindings.map(b => b.key));
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
5 changes: 4 additions & 1 deletion examples/context/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ export async function main() {
if (require.main === module) {
process.env.FOO = JSON.stringify({bar: 'xyz'});

main();
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/injection-without-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ export async function main() {
await sayHello(ctx);
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/interceptor-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,9 @@ export async function main() {
console.log(await greeter!.greet('John'));
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/parameterized-decoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ export async function main() {
console.log('2: %s', greeting2.hello());
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/sync-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ export async function main() {
await greetWithAsyncUser(ctx);
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
7 changes: 6 additions & 1 deletion examples/context/src/value-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ async function greetFromAll(greetersView: ContextView<Greeter>) {
await transformValueOrPromise(greetingsByLanguage, console.log);
}

if (require.main === module) main();
if (require.main === module) {
main().catch(err => {
console.error(err);
process.exit(1);
});
}
5 changes: 4 additions & 1 deletion examples/greeter-extension/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ module.exports = require('./dist');

if (require.main === module) {
const app = new module.exports.GreetingApplication();
app.main();
app.main().catch(err => {
console.error(err);
process.exit(1);
});
}
5 changes: 4 additions & 1 deletion packages/cli/bin/download-connector-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ async function download() {
await writeFileAsync(DEST, JSON.stringify(out, null, 2));
}

download();
download().catch(err => {
console.error(err);
process.exit(1);
});
2 changes: 2 additions & 0 deletions packages/context/src/__tests__/unit/resolver.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('constructor injection', () => {
}

expect(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
instantiateClass(TestClass, ctx);
}).to.throw(/Cannot resolve injected arguments/);
});
Expand Down Expand Up @@ -383,6 +384,7 @@ describe('property injection', () => {
}

expect(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
instantiateClass(TestClass, ctx);
}).to.throw(/Cannot resolve injected property/);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('tryWithFinally', () => {
let finalActionInvoked = false;
const action = () => 1;
const finalAction = () => (finalActionInvoked = true);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
tryWithFinally(action, finalAction);
expect(finalActionInvoked).to.be.true();
});
Expand Down
4 changes: 1 addition & 3 deletions packages/eslint-config/eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ module.exports = {

// Rules mapped from `@loopback/tslint-config/tslint.build.json
'@typescript-eslint/await-thenable': 'error', // tslint:await-promise: [true, 'PromiseLike', 'RequestPromise'],

// See https://github.com/typescript-eslint/typescript-eslint/issues/464
// tslint:no-floating-promises: [true, 'PromiseLike', 'RequestPromise'],
'@typescript-eslint/no-floating-promises': 'error',

'no-void': 'error', // tslint:no-void-expression: [true, 'ignore-arrow-function-shorthand'],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('getFilterJsonSchemaFor', () => {

it('describes "where" as an object', () => {
const filter = {where: 'invalid-where'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
Expand All @@ -63,6 +64,7 @@ describe('getFilterJsonSchemaFor', () => {

it('describes "fields" as an object', () => {
const filter = {fields: 'invalid-fields'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
Expand All @@ -75,6 +77,7 @@ describe('getFilterJsonSchemaFor', () => {

it('describes "include" as an array for models with relations', () => {
const filter = {include: 'invalid-include'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
Expand All @@ -92,6 +95,7 @@ describe('getFilterJsonSchemaFor', () => {

it('describes "offset" as an integer', () => {
const filter = {offset: 'invalid-offset'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
Expand All @@ -104,6 +108,7 @@ describe('getFilterJsonSchemaFor', () => {

it('describes "limit" as an integer', () => {
const filter = {limit: 'invalid-limit'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
Expand All @@ -116,6 +121,7 @@ describe('getFilterJsonSchemaFor', () => {

it('describes "skip" as an integer', () => {
const filter = {skip: 'invalid-skip'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
Expand All @@ -128,6 +134,7 @@ describe('getFilterJsonSchemaFor', () => {

it('describes "order" as an array', () => {
const filter = {order: 'invalid-order'};
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ajv.validate(customerFilterSchema, filter);
expect(ajv.errors || []).to.containDeep([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Sequence', () => {
let server: RestServer;
beforeEach(givenAppWithController);
it('provides a default sequence', async () => {
whenIRequest()
await whenIRequest()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is an example of a problem caught by the new rule.

.get('/name')
.expect('SequenceApp');
});
Expand All @@ -52,7 +52,7 @@ describe('Sequence', () => {
.expect('hello world');
});

it('allows users to define a custom sequence as a class', () => {
it('allows users to define a custom sequence as a class', async () => {
class MySequence implements SequenceHandler {
constructor(@inject(SequenceActions.SEND) private send: Send) {}

Expand All @@ -63,7 +63,7 @@ describe('Sequence', () => {
// bind user defined sequence
server.sequence(MySequence);

whenIRequest()
await whenIRequest()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another example.

.get('/')
.expect('hello world');
});
Expand Down
5 changes: 4 additions & 1 deletion packages/tsdocs/bin/extract-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ async function main() {
await runExtractorForMonorepo({silent, dryRun, apiReportEnabled});
}

main();
main().catch(err => {
console.error(err);
process.exit(1);
});
5 changes: 4 additions & 1 deletion packages/tsdocs/bin/update-apidocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ async function main() {
await updateApiDocs({silent, dryRun});
}

main();
main().catch(err => {
console.error(err);
process.exit(1);
});