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

Add tslint #14

Merged
merged 2 commits into from
Jul 26, 2018
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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
"scripts": {
"build": "tsc && rollup -c",
"clean": "rimraf dist",
"lint": "tslint --project tsconfig.json",
"prebuild": "npm run clean",
"prepare": "npm run build",
"prepublishOnly": "pkg-ok",
"start": "jest --watch",
"test": "jest"
"test": "jest && npm run lint"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -40,6 +41,7 @@
"rimraf": "2.6.2",
"rollup": "0.63.4",
"ts-jest": "23.0.1",
"tslint": "5.11.0",
"typescript": "2.9.2"
},
"jest": {
Expand Down
44 changes: 32 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,32 @@ export const Reflection = Object.assign(Reflect, {
});

export type Decorator = ClassDecorator | MemberDecorator;
export type MemberDecorator = <T>(target: Target, propertyKey: PropertyKey, descriptor?: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
export type MemberDecorator = <T>(target: Target,
propertyKey: PropertyKey,
descriptor?: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
export type MetadataKey = string;
export type MetadataValue = Function;
export type PropertyKey = string | symbol;
export type Target = object | Function;

const Metadata = new WeakMap();

export function defineMetadata(metadataKey: MetadataKey, metadataValue: MetadataValue, target: Target, propertyKey?: PropertyKey) {
export function defineMetadata(metadataKey: MetadataKey,
metadataValue: MetadataValue,
target: Target, propertyKey?: PropertyKey) {
return ordinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey);
}

export function decorate(decorators: ClassDecorator[], target: Function): Function
export function decorate(decorators: MemberDecorator[], target: object, propertyKey?: PropertyKey, attributes?: PropertyDescriptor): PropertyDescriptor | undefined
export function decorate(decorators: Decorator[], target: Target, propertyKey?: PropertyKey, attributes?: PropertyDescriptor): Function | PropertyDescriptor | undefined {
if (decorators.length === 0) throw new TypeError();
export function decorate(decorators: ClassDecorator[], target: Function): Function;
export function decorate(decorators: MemberDecorator[],
target: object,
propertyKey?: PropertyKey,
attributes?: PropertyDescriptor): PropertyDescriptor | undefined;
export function decorate(decorators: Decorator[],
target: Target,
propertyKey?: PropertyKey,
attributes?: PropertyDescriptor): Function | PropertyDescriptor | undefined {
if (decorators.length === 0) { throw new TypeError(); }

if (typeof target === 'function') {
return decorateConstructor(decorators as ClassDecorator[], target);
Expand Down Expand Up @@ -61,30 +71,40 @@ function decorateConstructor(decorators: ClassDecorator[], target: Function): Fu
return target;
}

function decorateProperty(decorators: MemberDecorator[], target: Target, propertyKey: PropertyKey, descriptor?: PropertyDescriptor): PropertyDescriptor | undefined {
function decorateProperty(decorators: MemberDecorator[],
target: Target,
propertyKey: PropertyKey,
descriptor?: PropertyDescriptor): PropertyDescriptor | undefined {
decorators.reverse().forEach((decorator: MemberDecorator) => {
descriptor = decorator(target, propertyKey, descriptor) || descriptor;
});
return descriptor;
}

function ordinaryDefineOwnMetadata(metadataKey: MetadataKey, metadataValue: MetadataValue, target: Target, propertyKey?: PropertyKey): void {
if (propertyKey && !['string', 'symbol'].includes(typeof propertyKey)) throw new TypeError();
function ordinaryDefineOwnMetadata(metadataKey: MetadataKey,
metadataValue: MetadataValue,
target: Target,
propertyKey?: PropertyKey): void {
if (propertyKey && !['string', 'symbol'].includes(typeof propertyKey)) { throw new TypeError(); }

(getMetadataMap(target, propertyKey) || createMetadataMap(target, propertyKey))
.set(metadataKey, metadataValue);
}

function ordinaryGetMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey): Function | undefined {
function ordinaryGetMetadata(metadataKey: MetadataKey,
target: Target,
propertyKey?: PropertyKey): Function | undefined {
return !!ordinaryGetOwnMetadata(metadataKey, target, propertyKey)
? ordinaryGetOwnMetadata(metadataKey, target, propertyKey)
: Object.getPrototypeOf(target)
? ordinaryGetMetadata(metadataKey, Object.getPrototypeOf(target), propertyKey)
: undefined;
}

function ordinaryGetOwnMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey): Function | undefined {
if (target === undefined) throw new TypeError();
function ordinaryGetOwnMetadata(metadataKey: MetadataKey,
target: Target,
propertyKey?: PropertyKey): Function | undefined {
if (target === undefined) { throw new TypeError(); }
const metadataMap = getMetadataMap(target, propertyKey);
return metadataMap && metadataMap.get(metadataKey);
}
Expand Down
19 changes: 19 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {
"quotemark": [true, "single"],
"ban-types": [
true,
["Object", "Avoid using the `Object` type. Did you mean `object`?"],
["Boolean", "Avoid using the `Boolean` type. Did you mean `boolean`?"],
["Number", "Avoid using the `Number` type. Did you mean `number`?"],
["String", "Avoid using the `String` type. Did you mean `string`?"],
["Symbol", "Avoid using the `Symbol` type. Did you mean `symbol`?"]
]
},
"rulesDirectory": []
}