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

fix: [#4684] ESLint issues in botbuilder-dialogs-adaptive-runtime-core #4809

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
}
},
"dependencies": {
"dependency-graph": "^1.0.0",
"eslint-plugin-only-warn": "^1.1.0"
"dependency-graph": "^1.0.0"
},
"devDependencies": {
"mocha": "^10.7.3",
Expand All @@ -39,7 +38,7 @@
"build": "tsc -b",
"clean": "rimraf _ts3.4 lib tsconfig.tsbuildinfo",
"depcheck": "depcheck --config ../../.depcheckrc --ignores dependency-graph",
"lint": "eslint .",
"lint": "eslint . --config ../../eslint.config.cjs",
"postbuild": "downlevel-dts lib _ts3.4/lib --checksum",
"test": "nyc mocha",
"test:min": "nyc --silent mocha --reporter dot"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import assert from 'assert';
import assert, { ok } from 'assert';
import { DepGraph } from 'dependency-graph';
import { ok } from 'assert';
import { stringify } from './util';

/**
Expand All @@ -13,7 +12,7 @@ import { stringify } from './util';
* @template Initial true if the `initialValue` passed to the factory must be defined
*/
export type Factory<Type, Initial extends boolean> = (
initialValue: Initial extends true ? Type : Type | undefined
initialValue: Initial extends true ? Type : Type | undefined,
) => Type;

/**
Expand All @@ -26,7 +25,7 @@ export type Factory<Type, Initial extends boolean> = (
*/
export type DependencyFactory<Type, Dependencies, Initial extends boolean> = (
dependencies: Dependencies,
initialValue: Initial extends true ? Type : Type | undefined
initialValue: Initial extends true ? Type : Type | undefined,
) => Type;

/**
Expand Down Expand Up @@ -97,7 +96,7 @@ export class ServiceCollection {
addFactory<InstanceType, Dependencies>(
key: string,
dependencies: string[],
factory: DependencyFactory<InstanceType, Dependencies, false>
factory: DependencyFactory<InstanceType, Dependencies, false>,
): this;

/**
Expand All @@ -106,7 +105,7 @@ export class ServiceCollection {
addFactory<InstanceType, Dependencies>(
key: string,
depsOrFactory: string[] | Factory<InstanceType, false>,
maybeFactory?: DependencyFactory<InstanceType, Dependencies, false>
maybeFactory?: DependencyFactory<InstanceType, Dependencies, false>,
): this {
const dependencies = Array.isArray(depsOrFactory) ? depsOrFactory : undefined;

Expand All @@ -129,8 +128,6 @@ export class ServiceCollection {
this.graph.removeNode(key);
}

// Note: we have done the type checking above, so disabling no-explicit-any is okay.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.graph.addNode(key, factories.concat(factory) as any);

return this;
Expand All @@ -157,7 +154,7 @@ export class ServiceCollection {
composeFactory<InstanceType, Dependencies>(
key: string,
dependencies: string[],
factory: DependencyFactory<InstanceType, Dependencies, true>
factory: DependencyFactory<InstanceType, Dependencies, true>,
): this;

/**
Expand All @@ -166,7 +163,7 @@ export class ServiceCollection {
composeFactory<InstanceType, Dependencies>(
key: string,
depsOrFactory: string[] | Factory<InstanceType, true>,
maybeFactory?: DependencyFactory<InstanceType, Dependencies, true>
maybeFactory?: DependencyFactory<InstanceType, Dependencies, true>,
): this {
if (maybeFactory) {
return this.addFactory<InstanceType, Dependencies>(
Expand All @@ -176,7 +173,7 @@ export class ServiceCollection {
ok(value, `unable to create ${key}, initial value undefined`);

return maybeFactory(dependencies, value);
}
},
);
} else {
ok(typeof depsOrFactory === 'function', 'illegal invocation with undefined factory');
Expand All @@ -193,43 +190,49 @@ export class ServiceCollection {
// depend on results of dependency registration
private buildNodes<ReturnType = Record<string, unknown>>(
generateNodes: () => string[],
reuseServices: Record<string, unknown> = {}
reuseServices: Record<string, unknown> = {},
): ReturnType {
// Consume all dependencies and then reset so updating registrations without re-registering
// dependencies works
this.dependencies.forEach((dependencies, node) =>
dependencies.forEach((dependency) => this.graph.addDependency(node, stringify(dependency)))
dependencies.forEach((dependency) => this.graph.addDependency(node, stringify(dependency))),
);

// Generate nodes after registering dependencies so ordering is correct
const nodes = generateNodes();

const services = nodes.reduce((services, service) => {
// Extra precaution
if (!this.graph.hasNode(service)) {
return services;
}
const services = nodes.reduce(
(services, service) => {
// Extra precaution
if (!this.graph.hasNode(service)) {
return services;
}

// Helper to generate return value
const assignValue = (value: unknown) => ({
...services,
[service]: value,
});
// Helper to generate return value
const assignValue = (value: unknown) => ({
...services,
[service]: value,
});

// Optionally reuse existing service
const reusedService = reuseServices[service];
if (reusedService !== undefined) {
return assignValue(reusedService);
}
// Optionally reuse existing service
const reusedService = reuseServices[service];
if (reusedService !== undefined) {
return assignValue(reusedService);
}

// Each node stores a list of factory methods.
const factories = this.graph.getNodeData(service);
// Each node stores a list of factory methods.
const factories = this.graph.getNodeData(service);

// Produce the instance by reducing those factories, passing the instance along for composition.
const instance = factories.reduce((value, factory) => factory(services, value), <unknown>services[service]);
// Produce the instance by reducing those factories, passing the instance along for composition.
const instance = factories.reduce(
(value, factory) => factory(services, value),
<unknown>services[service],
);

return assignValue(instance);
}, <Record<string, unknown>>{});
return assignValue(instance);
},
<Record<string, unknown>>{},
);

// Cache results for subsequent invocations that may desire pre-constructed instances
Object.assign(this.cache, services);
Expand All @@ -254,7 +257,7 @@ export class ServiceCollection {

const services = this.buildNodes<Record<string, InstanceType | undefined>>(
() => this.graph.dependenciesOf(key).concat(key),
initialServices
initialServices,
);

return services[key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class Bar {
}

class Baz {
constructor(public foo: Foo, public bar: Bar) {}
constructor(
public foo: Foo,
public bar: Bar,
) {}
}

interface TestServices {
Expand Down Expand Up @@ -129,7 +132,7 @@ describe('ServiceCollection', function () {
services.composeFactory<Record<string, unknown>, Record<string, Record<string, unknown>>>(
'b',
['a'],
({ a }, b) => ({ ...a, ...b })
({ a }, b) => ({ ...a, ...b }),
);

const a = services.mustMakeInstance('a');
Expand All @@ -144,7 +147,7 @@ describe('ServiceCollection', function () {
services.composeFactory<Record<string, unknown>, Record<string, Record<string, unknown>>>(
'b',
['a'],
({ a }, b) => ({ ...a, ...b })
({ a }, b) => ({ ...a, ...b }),
);
assert.throws(() => services.makeInstance('b'));
});
Expand Down
Loading