Skip to content

Commit

Permalink
chore: add prettier formatter (#1597)
Browse files Browse the repository at this point in the history
add prettier formatter
add rimraf
  • Loading branch information
notaphplover authored Oct 30, 2024
1 parent 6802b6b commit 98cff6e
Show file tree
Hide file tree
Showing 50 changed files with 2,026 additions and 834 deletions.
510 changes: 505 additions & 5 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
"chai": "4.3.8",
"mocha": "10.7.3",
"nyc": "17.1.0",
"prettier": "3.3.3",
"publish-please": "5.5.2",
"reflect-metadata": "0.2.2",
"rimraf": "6.0.1",
"sinon": "19.0.2",
"ts-node": "10.9.2",
"tslint": "6.1.3",
Expand Down Expand Up @@ -46,7 +48,8 @@
"build:es": "tsc -p src/tsconfig-es.json",
"build:es6": "tsc -p src/tsconfig-es6.json",
"build:lib": "tsc -p src/tsconfig.json",
"clean": "rm -r amd es lib",
"clean": "rimraf amd es es6 lib",
"format": "prettier --write ./src/*.ts ./src/**/*.ts",
"postupdate": "git diff-files --quiet package-lock.json || npm test",
"prepublish": "npm run build && publish-please guard",
"pretest": "tslint --project .",
Expand Down
11 changes: 11 additions & 0 deletions prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default {
printWidth: 80,
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: true,
bracketSpacing: true,
arrowParens: "always",
endOfLine: "lf",
trailingComma: "all",
};
110 changes: 73 additions & 37 deletions src/annotation/decorator_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,77 @@ import * as METADATA_KEY from '../constants/metadata_keys';
import { interfaces } from '../interfaces/interfaces';
import { getFirstArrayDuplicate } from '../utils/js';

function targetIsConstructorFunction<T = Object>(target: DecoratorTarget<T>): target is ConstructorFunction<T> {
function targetIsConstructorFunction<T = Object>(
target: DecoratorTarget<T>,
): target is ConstructorFunction<T> {
return (target as ConstructorFunction<T>).prototype !== undefined;
}

type Prototype<T> = {
[Property in keyof T]:
T[Property] extends NewableFunction ?
T[Property] :
T[Property] | undefined
} & { constructor: NewableFunction }
[Property in keyof T]: T[Property] extends NewableFunction
? T[Property]
: T[Property] | undefined;
} & { constructor: NewableFunction };

interface ConstructorFunction<T = Record<string, unknown>> {
new(...args: unknown[]): T,
prototype: Prototype<T>
new (...args: unknown[]): T;
prototype: Prototype<T>;
}

export type DecoratorTarget<T = unknown> = ConstructorFunction<T> | Prototype<T>
export type DecoratorTarget<T = unknown> =
| ConstructorFunction<T>
| Prototype<T>;

function _throwIfMethodParameter(parameterName: string | symbol | undefined): void {
function _throwIfMethodParameter(
parameterName: string | symbol | undefined,
): void {
if (parameterName !== undefined) {
throw new Error(ERROR_MSGS.INVALID_DECORATOR_OPERATION);
}
}


function tagParameter(
annotationTarget: DecoratorTarget,
parameterName: string | symbol | undefined,
parameterIndex: number,
metadata: interfaces.MetadataOrMetadataArray
metadata: interfaces.MetadataOrMetadataArray,
) {
_throwIfMethodParameter(parameterName);
_tagParameterOrProperty(METADATA_KEY.TAGGED, annotationTarget as ConstructorFunction, parameterIndex.toString(), metadata);
_tagParameterOrProperty(
METADATA_KEY.TAGGED,
annotationTarget as ConstructorFunction,
parameterIndex.toString(),
metadata,
);
}

function tagProperty(
annotationTarget: DecoratorTarget,
propertyName: string | symbol,
metadata: interfaces.MetadataOrMetadataArray
metadata: interfaces.MetadataOrMetadataArray,
) {
if (targetIsConstructorFunction(annotationTarget)) {
throw new Error(ERROR_MSGS.INVALID_DECORATOR_OPERATION);
}
_tagParameterOrProperty(METADATA_KEY.TAGGED_PROP, annotationTarget.constructor, propertyName, metadata);
_tagParameterOrProperty(
METADATA_KEY.TAGGED_PROP,
annotationTarget.constructor,
propertyName,
metadata,
);
}

function _ensureNoMetadataKeyDuplicates(metadata: interfaces.MetadataOrMetadataArray): interfaces.Metadata[] {
function _ensureNoMetadataKeyDuplicates(
metadata: interfaces.MetadataOrMetadataArray,
): interfaces.Metadata[] {
let metadatas: interfaces.Metadata[] = [];
if (Array.isArray(metadata)) {
metadatas = metadata;
const duplicate = getFirstArrayDuplicate(metadatas.map(md => md.key));
const duplicate = getFirstArrayDuplicate(metadatas.map((md) => md.key));
if (duplicate !== undefined) {
throw new Error(`${ERROR_MSGS.DUPLICATED_METADATA} ${duplicate.toString()}`);
throw new Error(
`${ERROR_MSGS.DUPLICATED_METADATA} ${duplicate.toString()}`,
);
}
} else {
metadatas = [metadata];
Expand All @@ -69,38 +87,47 @@ function _tagParameterOrProperty(
key: string | symbol,
metadata: interfaces.MetadataOrMetadataArray,
) {
const metadatas: interfaces.Metadata[] = _ensureNoMetadataKeyDuplicates(metadata);
const metadatas: interfaces.Metadata[] =
_ensureNoMetadataKeyDuplicates(metadata);

let paramsOrPropertiesMetadata: Record<string | symbol, interfaces.Metadata[] | undefined> = {};
let paramsOrPropertiesMetadata: Record<
string | symbol,
interfaces.Metadata[] | undefined
> = {};
// read metadata if available
if (Reflect.hasOwnMetadata(metadataKey, annotationTarget)) {
paramsOrPropertiesMetadata = Reflect.getMetadata(metadataKey, annotationTarget);
paramsOrPropertiesMetadata = Reflect.getMetadata(
metadataKey,
annotationTarget,
);
}

let paramOrPropertyMetadata: interfaces.Metadata[] | undefined = paramsOrPropertiesMetadata[key as string];
let paramOrPropertyMetadata: interfaces.Metadata[] | undefined =
paramsOrPropertiesMetadata[key as string];

if (paramOrPropertyMetadata === undefined) {
paramOrPropertyMetadata = [];
} else {
for (const m of paramOrPropertyMetadata) {
if (metadatas.some(md => md.key === m.key)) {
throw new Error(`${ERROR_MSGS.DUPLICATED_METADATA} ${m.key.toString()}`);
if (metadatas.some((md) => md.key === m.key)) {
throw new Error(
`${ERROR_MSGS.DUPLICATED_METADATA} ${m.key.toString()}`,
);
}
}
}

// set metadata
paramOrPropertyMetadata.push(...metadatas);
paramsOrPropertiesMetadata[key] = paramOrPropertyMetadata;
Reflect.defineMetadata(metadataKey, paramsOrPropertiesMetadata, annotationTarget);

Reflect.defineMetadata(
metadataKey,
paramsOrPropertiesMetadata,
annotationTarget,
);
}



function createTaggedDecorator(
metadata: interfaces.MetadataOrMetadataArray,
) {
function createTaggedDecorator(metadata: interfaces.MetadataOrMetadataArray) {
return <T>(
target: DecoratorTarget,
targetKey?: string | symbol,
Expand All @@ -122,7 +149,9 @@ function _decorate(
}

function _param(paramIndex: number, decorator: ParameterDecorator) {
return function (target: string, key: string) { decorator(target, key, paramIndex); };
return function (target: string, key: string) {
decorator(target, key, paramIndex);
};
}

// Allows VanillaJS developers to use decorators:
Expand All @@ -131,14 +160,21 @@ function _param(paramIndex: number, decorator: ParameterDecorator) {
// decorate(named('foo'), FooBar, 0);
// decorate(tagged('bar'), FooBar, 1);
function decorate(
decorator: (DecoratorTarget | ParameterDecorator | MethodDecorator),
decorator: DecoratorTarget | ParameterDecorator | MethodDecorator,
target: object,
parameterIndexOrProperty?: number | string): void {

parameterIndexOrProperty?: number | string,
): void {
if (typeof parameterIndexOrProperty === 'number') {
_decorate([_param(parameterIndexOrProperty, decorator as ParameterDecorator)], target);
_decorate(
[_param(parameterIndexOrProperty, decorator as ParameterDecorator)],
target,
);
} else if (typeof parameterIndexOrProperty === 'string') {
Reflect.decorate([decorator as MethodDecorator], target, parameterIndexOrProperty);
Reflect.decorate(
[decorator as MethodDecorator],
target,
parameterIndexOrProperty,
);
} else {
_decorate([decorator], target);
}
Expand Down
12 changes: 6 additions & 6 deletions src/annotation/inject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as METADATA_KEY from '../constants/metadata_keys';
import { injectBase } from './inject_base';

const inject = injectBase(METADATA_KEY.INJECT_TAG);

export { inject };
import * as METADATA_KEY from '../constants/metadata_keys';
import { injectBase } from './inject_base';

const inject = injectBase(METADATA_KEY.INJECT_TAG);

export { inject };
7 changes: 4 additions & 3 deletions src/annotation/inject_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ export function injectBase(metadataKey: string) {
indexOrPropertyDescriptor?: number | TypedPropertyDescriptor<T>,
) => {
if (serviceIdentifier === undefined) {
const className = typeof target === 'function' ? target.name : target.constructor.name;
const className =
typeof target === 'function' ? target.name : target.constructor.name;

throw new Error(UNDEFINED_INJECT_ANNOTATION(className));
}
return createTaggedDecorator(
new Metadata(metadataKey, serviceIdentifier)
new Metadata(metadataKey, serviceIdentifier),
)(target, targetKey, indexOrPropertyDescriptor);
};
}
};
}
8 changes: 5 additions & 3 deletions src/annotation/injectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import * as ERRORS_MSGS from '../constants/error_msgs';
import * as METADATA_KEY from '../constants/metadata_keys';

function injectable() {
return function <T extends abstract new (...args: any) => unknown>(target: T) {

return function <T extends abstract new (...args: any) => unknown>(
target: T,
) {
if (Reflect.hasOwnMetadata(METADATA_KEY.PARAM_TYPES, target)) {
throw new Error(ERRORS_MSGS.DUPLICATED_INJECTABLE_DECORATOR);
}

const types = Reflect.getMetadata(METADATA_KEY.DESIGN_PARAM_TYPES, target) || [];
const types =
Reflect.getMetadata(METADATA_KEY.DESIGN_PARAM_TYPES, target) || [];
Reflect.defineMetadata(METADATA_KEY.PARAM_TYPES, types, target);

return target;
Expand Down
4 changes: 3 additions & 1 deletion src/annotation/lazy_service_identifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { interfaces } from '../interfaces/interfaces';

export type ServiceIdentifierOrFunc<T> = interfaces.ServiceIdentifier<T> | LazyServiceIdentifier<T>;
export type ServiceIdentifierOrFunc<T> =
| interfaces.ServiceIdentifier<T>
| LazyServiceIdentifier<T>;

export class LazyServiceIdentifier<T = unknown> {
private _cb: () => interfaces.ServiceIdentifier<T>;
Expand Down
20 changes: 10 additions & 10 deletions src/annotation/named.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as METADATA_KEY from '../constants/metadata_keys';
import { Metadata } from '../planning/metadata';
import { createTaggedDecorator } from './decorator_utils';

// Used to add named metadata which is used to resolve name-based contextual bindings.
function named(name: string | number | symbol) {
return createTaggedDecorator(new Metadata(METADATA_KEY.NAMED_TAG, name));
}

export { named };
import * as METADATA_KEY from '../constants/metadata_keys';
import { Metadata } from '../planning/metadata';
import { createTaggedDecorator } from './decorator_utils';

// Used to add named metadata which is used to resolve name-based contextual bindings.
function named(name: string | number | symbol) {
return createTaggedDecorator(new Metadata(METADATA_KEY.NAMED_TAG, name));
}

export { named };
8 changes: 4 additions & 4 deletions src/annotation/property_event_decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Metadata } from '../planning/metadata';

function propertyEventDecorator(eventKey: string, errorMessage: string) {
return () => {
return (target: { constructor: NewableFunction; }, propertyKey: string) => {
return (target: { constructor: NewableFunction }, propertyKey: string) => {
const metadata = new Metadata(eventKey, propertyKey);

if (Reflect.hasOwnMetadata(eventKey, target.constructor)) {
throw new Error(errorMessage);
}
Reflect.defineMetadata(eventKey, metadata, target.constructor);
}
}
};
};
}

export { propertyEventDecorator }
export { propertyEventDecorator };
5 changes: 4 additions & 1 deletion src/annotation/tagged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { Metadata } from '../planning/metadata';
import { createTaggedDecorator } from './decorator_utils';

// Used to add custom metadata which is used to resolve metadata-based contextual bindings.
function tagged<T>(metadataKey: string | number | symbol, metadataValue: unknown) {
function tagged<T>(
metadataKey: string | number | symbol,
metadataValue: unknown,
) {
return createTaggedDecorator(new Metadata(metadataKey, metadataValue));
}

Expand Down
8 changes: 4 additions & 4 deletions src/annotation/target_name.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as METADATA_KEY from "../constants/metadata_keys";
import { Metadata } from "../planning/metadata";
import { tagParameter, DecoratorTarget } from "./decorator_utils";
import * as METADATA_KEY from '../constants/metadata_keys';
import { Metadata } from '../planning/metadata';
import { tagParameter, DecoratorTarget } from './decorator_utils';

function targetName(name: string) {
return function (
target: DecoratorTarget,
targetKey: string | undefined,
index: number
index: number,
) {
const metadata = new Metadata(METADATA_KEY.NAME_TAG, name);
tagParameter(target, targetKey, index, metadata);
Expand Down
6 changes: 5 additions & 1 deletion src/annotation/unmanaged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { Metadata } from '../planning/metadata';
import { tagParameter, DecoratorTarget } from './decorator_utils';

function unmanaged() {
return function (target: DecoratorTarget, targetKey: string | undefined, index: number) {
return function (
target: DecoratorTarget,
targetKey: string | undefined,
index: number,
) {
const metadata = new Metadata(METADATA_KEY.UNMANAGED_TAG, true);
tagParameter(target, targetKey, index, metadata);
};
Expand Down
Loading

0 comments on commit 98cff6e

Please sign in to comment.