Skip to content

Commit 2edb2a0

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/core): assert catch clause variable type before usage
Prepares the `@angular-devkit/core` package for the eventual change of enabling the TypeScript `useUnknownInCatchVariables` option. This option provides additional code safety by ensuring that the catch clause variable is the proper type before attempting to access its properties. Similar changes will be needed in the other packages in the repository prior to enabling `useUnknownInCatchVariables`.
1 parent 7431d1c commit 2edb2a0

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

packages/angular_devkit/core/node/experimental/jobs/job-registry.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@ import { JsonValue, experimental as core_experimental, schema } from '../../../s
1212
export class NodeModuleJobRegistry<
1313
MinimumArgumentValueT extends JsonValue = JsonValue,
1414
MinimumInputValueT extends JsonValue = JsonValue,
15-
MinimumOutputValueT extends JsonValue = JsonValue
15+
MinimumOutputValueT extends JsonValue = JsonValue,
1616
> implements
17-
core_experimental.jobs.Registry<
18-
MinimumArgumentValueT,
19-
MinimumInputValueT,
20-
MinimumOutputValueT
21-
> {
17+
core_experimental.jobs.Registry<MinimumArgumentValueT, MinimumInputValueT, MinimumOutputValueT>
18+
{
2219
protected _resolve(name: string): string | null {
2320
try {
2421
return require.resolve(name);
2522
} catch (e) {
26-
if (e.code === 'MODULE_NOT_FOUND') {
23+
if ((e as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {
2724
return null;
2825
}
2926
throw e;

packages/angular_devkit/core/node/host.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function loadFSWatcher() {
4545
// eslint-disable-next-line import/no-extraneous-dependencies
4646
FSWatcher = require('chokidar').FSWatcher;
4747
} catch (e) {
48-
if (e.code !== 'MODULE_NOT_FOUND') {
48+
if ((e as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {
4949
throw new Error(
5050
'As of angular-devkit version 8.0, the "chokidar" package ' +
5151
'must be installed in order to use watch() features.',

packages/angular_devkit/core/src/json/schema/registry.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,20 +528,21 @@ export class CoreSchemaRegistry implements SchemaRegistry {
528528
!Array.isArray((parentSchema as JsonObject).default)
529529
? undefined
530530
: ((parentSchema as JsonObject).default as string[]),
531-
async validator(data: JsonValue) {
531+
async validator(data: JsonValue): Promise<boolean | string> {
532532
try {
533533
const result = await it.self.validate(parentSchema, data);
534534
// If the schema is sync then false will be returned on validation failure
535535
if (result) {
536-
return result;
536+
return result as boolean | string;
537537
} else if (it.self.errors?.length) {
538538
// Validation errors will be present on the Ajv instance when sync
539-
return it.self.errors[0].message;
539+
return it.self.errors[0].message as string;
540540
}
541541
} catch (e) {
542+
const validationError = e as { errors?: Error[] };
542543
// If the schema is async then an error will be thrown on validation failure
543-
if (Array.isArray(e.errors) && e.errors.length) {
544-
return e.errors[0].message;
544+
if (Array.isArray(validationError.errors) && validationError.errors.length) {
545+
return validationError.errors[0].message;
545546
}
546547
}
547548

0 commit comments

Comments
 (0)