Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from nbeach/better-errors
Better errors
- Loading branch information
Showing
8 changed files
with
185 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import isNil = require("lodash/fp/isNil") | ||
import includes = require("lodash/fp/includes") | ||
|
||
export function throwIfNil(value: any, error: string): void { | ||
if (isNil(value)) { | ||
throw new Error(error) | ||
} | ||
} | ||
|
||
export function throwIfNotIn(value: any, list: any[], message: string) { | ||
if (!includes(value, list)) { | ||
throw new Error(message) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {Component, Injectable, Input, Output} from "@angular/core" | ||
import {getAnnotation, propertyMetadata} from "./reflection" | ||
import {expect} from "chai" | ||
|
||
describe("getAnnotation()", () => { | ||
|
||
it("returns the annotation object for the class", () => { | ||
@Component({ selector: "foo" }) | ||
class SomeComponent {} | ||
|
||
expect(getAnnotation(SomeComponent, Component).selector).to.equal("foo") | ||
}) | ||
|
||
|
||
it("returns undefined if the class is annotated but not with the provided decorator", () => { | ||
@Injectable() | ||
class SomeComponent {} | ||
|
||
expect(getAnnotation(SomeComponent, Component)).to.to.be.undefined | ||
}) | ||
|
||
it("returns undefined if the class has no decorators", () => { | ||
class SomeComponent {} | ||
|
||
expect(getAnnotation(SomeComponent, Component)).to.to.be.undefined | ||
}) | ||
}) | ||
|
||
|
||
describe("propertyMetadata()", () => { | ||
|
||
it("returns metadata for decorated properties", () => { | ||
|
||
class SomeComponent { | ||
@Input("foo") public one: string = "" | ||
@Output("bar") public two: string = "" | ||
} | ||
|
||
const actual = propertyMetadata(SomeComponent) | ||
|
||
expect(actual.one[0].bindingPropertyName).to.equal("foo") | ||
expect(actual.two[0].bindingPropertyName).to.equal("bar") | ||
}) | ||
|
||
it("returns an empty object when no properties are decorated", () => { | ||
class SomeComponent { | ||
public one: string = "" | ||
public two: string = "" | ||
} | ||
|
||
const actual = propertyMetadata(SomeComponent) | ||
|
||
expect(actual).to.deep.equal({}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import {Type} from "@angular/core" | ||
import "reflect-metadata" | ||
|
||
export function getAnnotation(object: Type<any>, annotation: any): any { | ||
const annotations = (object as any).__annotations__ || Reflect.getMetadata("annotations", object) | ||
return annotations.filter((attachedAnnotation: any) => attachedAnnotation instanceof annotation)[0] | ||
export function getAnnotation(object: Type<any>, annotationType: any): any | undefined { | ||
const annotations = (object as any).__annotations__ || Reflect.getMetadata("annotations", object) || [] | ||
const [annotation] = annotations | ||
.filter((attachedAnnotation: any) => attachedAnnotation instanceof annotationType) | ||
|
||
return annotation | ||
} | ||
|
||
export function propertyMetadata(object: Type<any>): any { | ||
return (object as any).__prop__metadata__ || Reflect.getMetadata("propMetadata", object) | ||
return (object as any).__prop__metadata__ || Reflect.getMetadata("propMetadata", object) || {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import {getAnnotation} from "./reflection" | ||
import {Component, Type} from "@angular/core" | ||
import {throwIfNil} from "./exception" | ||
|
||
export function selectorOf(component: Type<any>) { | ||
return getAnnotation(component, Component).selector | ||
const annotation = getAnnotation(component, Component) | ||
throwIfNil(annotation, "Provided value is not a Component") | ||
|
||
const selector = annotation.selector | ||
throwIfNil(selector, "Component does not have a selector set") | ||
|
||
return selector | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters