This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #2 from gund/fix-aot
[Fix] AOT + Add decorators
- Loading branch information
Showing
34 changed files
with
725 additions
and
148 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 |
---|---|---|
|
@@ -11,4 +11,6 @@ engines: | |
ratings: | ||
paths: | ||
- "src/**/*.ts" | ||
exclude_paths: [] | ||
exclude_paths: [ | ||
"src/**/*.spec.ts" | ||
] |
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
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,16 +1,16 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { ModuleWithProviders, NgModule, Type } from '@angular/core'; | ||
|
||
import { createWithPresetMethodFor, PresetModule } from '../../preset'; | ||
import { PresetModule, providePresetFor } from '../../preset'; | ||
import { MyComponentModule } from './my-component.module'; | ||
import { MyPreset } from './my-preset'; | ||
import { MyComponent } from './my.component'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
PresetModule, | ||
MyComponentModule, | ||
], | ||
imports: [MyComponentModule, PresetModule.forComponent(MyComponent)], | ||
exports: [MyComponentModule], | ||
}) | ||
export class MyCustomModule { | ||
static withPreset = createWithPresetMethodFor<MyPreset>(MyCustomModule); | ||
static withPreset(presetType: Type<MyPreset>): ModuleWithProviders { | ||
return providePresetFor(MyCustomModule, presetType); | ||
} | ||
} |
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
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,19 +1,13 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
import { PresetService } from '../../preset'; | ||
import { Preset } from '../../preset'; | ||
import { MyPreset } from './my-preset'; | ||
|
||
@Component({ | ||
selector: 'prst-my', | ||
templateUrl: './my.component.html', | ||
styleUrls: ['./my.component.css'] | ||
styleUrls: ['./my.component.css'], | ||
}) | ||
export class MyComponent { | ||
|
||
presetComp = this.presetService.getPreset<MyPreset>(); | ||
|
||
constructor( | ||
private presetService: PresetService, | ||
) { } | ||
|
||
@Preset() presetComp: MyPreset; | ||
} |
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,5 +1,6 @@ | ||
export { PresetType } from './preset-token'; | ||
export * from './preset-method'; | ||
export * from './provide'; | ||
export * from './metadata'; | ||
export * from './preset.service'; | ||
export * from './preset.module'; | ||
export * from './preset-default.module'; |
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,2 @@ | ||
export * from './preset'; | ||
export * from './preset-decorator'; |
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,18 @@ | ||
import { PresetMetadata, setPresetMetadataOn } from './preset'; | ||
import { Preset } from './preset-decorator'; | ||
|
||
jest.mock('./preset'); | ||
|
||
describe('Preset decorator', () => { | ||
it('should return prop decorator and call `setPresetMetadataOn` with target and metadata obj', () => { | ||
const options = { __options: true }; | ||
const propName = 'propertyName'; | ||
const t = { constructor: {} } as any; | ||
Preset(options)(t, propName); | ||
|
||
expect(setPresetMetadataOn).toHaveBeenCalledWith(t, <PresetMetadata>{ | ||
propName, | ||
options, | ||
}); | ||
}); | ||
}); |
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,9 @@ | ||
import { PresetMetadataOptions, setPresetMetadataOn } from './preset'; | ||
|
||
export function Preset(options?: PresetMetadataOptions): PropertyDecorator { | ||
return (target, propName) => | ||
setPresetMetadataOn(target, { | ||
propName, | ||
options, | ||
}); | ||
} |
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,52 @@ | ||
import { | ||
getPresetMetadataFrom, | ||
PRESET_METADATA_KEY, | ||
setPresetMetadataOn, | ||
} from './preset'; | ||
|
||
describe('setPresetMetadataOn() function', () => { | ||
it('should set metadata on type`s prototype under `PRESET_METADATA_KEY`', () => { | ||
const metadata = { __medatadta: true }; | ||
const t = getFakeType(); | ||
|
||
setPresetMetadataOn(t, metadata as any); | ||
|
||
expect(t[PRESET_METADATA_KEY]).toEqual([metadata]); | ||
}); | ||
|
||
it('should set array of metadata on type`s prototype under `PRESET_METADATA_KEY`', () => { | ||
const metadata = { __medatadta: true }; | ||
const metadata2 = { __medatadta2: true }; | ||
const t = getFakeType(); | ||
|
||
setPresetMetadataOn(t, [metadata, metadata2] as any); | ||
|
||
expect(t[PRESET_METADATA_KEY]).toEqual([metadata, metadata2]); | ||
}); | ||
|
||
it('should preserve previously set metadata on type`s prototype under `PRESET_METADATA_KEY`', () => { | ||
const metadata = { __medatadta: true }; | ||
const metadata2 = { __medatadta2: true }; | ||
const t = getFakeType(); | ||
|
||
setPresetMetadataOn(t, metadata as any); | ||
expect(t[PRESET_METADATA_KEY]).toEqual([metadata]); | ||
|
||
setPresetMetadataOn(t, metadata2 as any); | ||
expect(t[PRESET_METADATA_KEY]).toEqual([metadata, metadata2]); | ||
}); | ||
}); | ||
|
||
describe('getPresetMetadataFrom() function', () => { | ||
it('should return metadata from type under `PRESET_METADATA_KEY`', () => { | ||
const metadata = { __metadata: true }; | ||
const t = getFakeType(); | ||
setPresetMetadataOn(t, metadata as any); | ||
|
||
expect(getPresetMetadataFrom({ prototype: t })).toEqual([metadata]); | ||
}); | ||
}); | ||
|
||
function getFakeType(): any { | ||
return class {}; | ||
} |
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,26 @@ | ||
export const PRESET_METADATA_KEY = '__Preset-Key__'; | ||
|
||
export interface PresetMetadata { | ||
propName: string | symbol; | ||
options?: PresetMetadataOptions; | ||
} | ||
|
||
// tslint:disable-next-line:no-empty-interface | ||
export interface PresetMetadataOptions {} | ||
|
||
export function setPresetMetadataOn( | ||
type: any, | ||
metadata: PresetMetadata | PresetMetadata[], | ||
) { | ||
if (!Array.isArray(metadata)) { | ||
metadata = [metadata]; | ||
} | ||
|
||
const existingMetadata = type[PRESET_METADATA_KEY] || []; | ||
type[PRESET_METADATA_KEY] = [...existingMetadata, ...metadata]; | ||
} | ||
|
||
export function getPresetMetadataFrom(type: any): PresetMetadata[] { | ||
const proto = type.prototype || (type.prototype = {}); | ||
return proto[PRESET_METADATA_KEY] || []; | ||
} |
Oops, something went wrong.