Navigation Menu

Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Commit

Permalink
Preparation for publish
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Jan 17, 2019
1 parent f553271 commit 4ea4bd5
Show file tree
Hide file tree
Showing 12 changed files with 4,686 additions and 86 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -2,4 +2,5 @@ node_modules
coverage
src/**/*.js
test/**/*.js
.DS_Store
.DS_Store
lib
13 changes: 13 additions & 0 deletions .npmignore
@@ -0,0 +1,13 @@
.vscode
coverage
scripts
src
test
.gitignore
.npmignore
.travis.yml
package-lock.json
tsconfig.build.json
tsconfig.json
yarn-error.log
yarn.lock
12 changes: 9 additions & 3 deletions package.json
@@ -1,10 +1,14 @@
{
"name": "tinspector",
"version": "1.3.0",
"description": "TypeScript type inspector",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"postinstall": "tsc",
"test": "jest"
"test": "jest",
"prepublish": "tsc -p tsconfig.build.json",
"publish": "np"
},
"keywords": [
"Reflection",
Expand All @@ -26,9 +30,11 @@
},
"jest": {
"collectCoverage": true,
"collectCoverageFrom": ["src/**/*.js"],
"collectCoverageFrom": [
"src/**/*.js"
],
"snapshotSerializers": [
"./scripts/function-snapshot-serializer.js"
]
}
}
}
99 changes: 53 additions & 46 deletions src/index.ts
Expand Up @@ -170,47 +170,6 @@ export function getDecorators(target: any): Decorator[] {
return Reflect.getMetadata(DECORATOR_KEY, target) || []
}

/* ---------------------------------------------------------------- */
/* ------------------------- DECORATORS --------------------------- */
/* ---------------------------------------------------------------- */

/**
* Add type information for array element
* @param type Data type of array element
*/
export function array(type: Class) {
return decorate(<ArrayDecorator>{ kind: "Array", type: type }, ["Parameter", "Method", "Property"])
}

/**
* Override type definition information. Useful to add type definition for some data type that is erased
* after transfile such as Partial<Type> or ReadOnly<Type>
*
* If applied to parameter it will override the parameter type
*
* If applied to property it will override the property type
*
* if applied to method it will overrid the method return value
* @param type The type overridden
* @param info Additional information about type (readonly, partial etc)
*/
export function type(type: Class, info?: string) {
return decorate(<TypeDecorator>{ kind: "Override", type: type, info }, ["Parameter", "Method", "Property"])
}

/**
* Mark member as private
*/
export function ignore() {
return decorate(<PrivateDecorator>{ kind: "Private" }, ["Parameter", "Method", "Property"])
}

/**
* Mark all constructor parameters as properties
*/
export function parameterProperties() {
return decorateClass({ type: "ParameterProperties" })
}

/* ---------------------------------------------------------------- */
/* ------------------------- CACHE FUNCTIONS ---------------------- */
Expand Down Expand Up @@ -392,8 +351,16 @@ function traverse(fn: any, name: string): Reflection | undefined {
return
}
}

/**
* Reflect module
* @param path module name
*/
export function reflect(path: string): ObjectReflection

/**
* Reflect class
* @param classType Class
*/
export function reflect(classType: Class): ClassReflection
export function reflect(option: string | Class) {
const cache = getCache(option)
Expand All @@ -405,8 +372,48 @@ export function reflect(option: string | Class) {
return setCache(option, reflectClassDeep(option))
}
}
reflect.private = ignore
reflect.type = type
reflect.array = array
reflect.parameterProperties = parameterProperties

/* ---------------------------------------------------------------- */
/* ------------------------- DECORATORS --------------------------- */
/* ---------------------------------------------------------------- */

/**
* Mark member as private
*/
reflect.private = function () {
return decorate(<PrivateDecorator>{ kind: "Private" }, ["Parameter", "Method", "Property"])
}

/**
* Override type definition information. Useful to add type definition for some data type that is erased
* after transfile such as Partial<Type> or ReadOnly<Type>
*
* If applied to parameter it will override the parameter type
*
* If applied to property it will override the property type
*
* if applied to method it will overrid the method return value
* @param type The type overridden
* @param info Additional information about type (readonly, partial etc)
*/
reflect.type = function (type: Class, info?: string) {
return decorate(<TypeDecorator>{ kind: "Override", type: type, info }, ["Parameter", "Method", "Property"])
}

/**
* Add type information for array element
* @param type Data type of array element
*/
reflect.array = function (type: Class) {
return decorate(<ArrayDecorator>{ kind: "Array", type: type }, ["Parameter", "Method", "Property"])
}

/**
* Mark all constructor parameters as properties
*/
reflect.parameterProperties = function () {
return decorateClass({ type: "ParameterProperties" })
}


export default reflect
2 changes: 1 addition & 1 deletion test/cache.spec.ts
@@ -1,4 +1,4 @@
import { decorateClass, reflect, type } from "../src"
import { decorateClass, reflect } from "../src"

describe("Cache Function", () => {

Expand Down
8 changes: 4 additions & 4 deletions test/class.spec.ts
@@ -1,4 +1,4 @@
import { array, decorateClass, decorateMethod, reflect, type } from "../src"
import { decorateClass, decorateMethod, reflect } from "../src"

describe("Class Introspection", () => {
it("Should inspect class properly", () => {
Expand Down Expand Up @@ -47,7 +47,7 @@ describe("Class Introspection", () => {
class EmptyClass { }
@decorateClass({})
class DummyClass {
constructor(@array(EmptyClass) empty: EmptyClass[]) { }
constructor(@reflect.array(EmptyClass) empty: EmptyClass[]) { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
Expand All @@ -57,7 +57,7 @@ describe("Class Introspection", () => {
class EmptyClass { }
class DummyClass {
@decorateMethod({})
dummyMethod(@array(EmptyClass) empty: EmptyClass[]) { }
dummyMethod(@reflect.array(EmptyClass) empty: EmptyClass[]) { }
}
const meta = reflect(DummyClass)
expect(meta.methods[0].parameters[0].type).toEqual([EmptyClass])
Expand All @@ -67,7 +67,7 @@ describe("Class Introspection", () => {
it("Should able to override type with other type", () => {
class OtherDummyClass { }
class DummyClass {
method(@type(OtherDummyClass, "Readonly") dummy: Readonly<OtherDummyClass>) { }
method(@reflect.type(OtherDummyClass, "Readonly") dummy: Readonly<OtherDummyClass>) { }
}
const meta = reflect(DummyClass)
expect(meta.methods[0].parameters[0].decorators[0].type).toEqual(OtherDummyClass)
Expand Down
14 changes: 6 additions & 8 deletions test/decorator.spec.ts
@@ -1,11 +1,9 @@
import {
parameterProperties,
decorate,
decorateClass,
decorateMethod,
decorateParameter,
decorateProperty,
ignore,
reflect,
} from "../src"

Expand Down Expand Up @@ -244,7 +242,7 @@ describe("Decorator", () => {
})

it("Should inspect constructor property", () => {
@parameterProperties()
@reflect.parameterProperties()
class DummyClass {
constructor(public data: string) { }
}
Expand All @@ -253,7 +251,7 @@ describe("Decorator", () => {
})

it("Should inspect constructor property with decorator", () => {
@parameterProperties()
@reflect.parameterProperties()
class DummyClass {
constructor(@decorateProperty({}) public data: string) { }
}
Expand All @@ -263,7 +261,7 @@ describe("Decorator", () => {
})

it("Should inspect constructor property with decorator multiple", () => {
@parameterProperties()
@reflect.parameterProperties()
class DummyClass {
constructor(
@decorateProperty({ value: 1 })
Expand All @@ -276,7 +274,7 @@ describe("Decorator", () => {
})

it("Should inspect constructor property with decorator callback", () => {
@parameterProperties()
@reflect.parameterProperties()
class DummyClass {
constructor(
@decorateProperty((target, name) => ({ target, name }))
Expand All @@ -288,9 +286,9 @@ describe("Decorator", () => {
})

it("Should not inspect private constructor property", () => {
@parameterProperties()
@reflect.parameterProperties()
class DummyClass {
constructor(public data: string, @ignore() myPrivateField: string) { }
constructor(public data: string, @reflect.private() myPrivateField: string) { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
Expand Down
10 changes: 5 additions & 5 deletions test/inheritance.spec.ts
@@ -1,4 +1,4 @@
import { reflect, getDeepMembers, decorateProperty, DECORATOR_KEY, decorateMethod, parameterProperties } from "../src";
import { reflect, getDeepMembers, decorateProperty, DECORATOR_KEY, decorateMethod } from "../src";

describe("getDeepMember", () => {

Expand Down Expand Up @@ -79,14 +79,14 @@ describe("Inheritance", () => {
})

it("Should inspect domain with inheritance using constructor property", () => {
@parameterProperties()
@reflect.parameterProperties()
class DomainBase {
constructor(
public id = 0,
public createdAt = new Date()
) { }
}
@parameterProperties()
@reflect.parameterProperties()
class Item extends DomainBase {
constructor(
public name: string,
Expand All @@ -100,14 +100,14 @@ describe("Inheritance", () => {
})

it("Should inspect domain with inheritance using property", () => {
@parameterProperties()
@reflect.parameterProperties()
class DomainBase {
@decorateProperty({})
id = 0
@decorateProperty({})
createdAt = new Date()
}
@parameterProperties()
@reflect.parameterProperties()
class Item extends DomainBase {
constructor(
public name: string,
Expand Down
2 changes: 1 addition & 1 deletion test/module.spec.ts
@@ -1,6 +1,6 @@
import { join } from "path"

import { reflect, type } from "../src"
import { reflect } from "../src"
import { MyClass } from "./mocks/mock.class"
import { myNameSpace } from "./mocks/mock.class-in-namespace"

Expand Down
11 changes: 11 additions & 0 deletions tsconfig.build.json
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "lib",
"declaration": true
},
"exclude": [
"test",
"lib"
]
}

0 comments on commit 4ea4bd5

Please sign in to comment.