-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
PropertyExtractor.ts
209 lines (188 loc) · 8.44 KB
/
PropertyExtractor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { CommonModule } from '@angular/common';
import {
Component,
Directive,
importProvidersFrom,
Injectable,
InjectionToken,
Input,
NgModule,
Output,
Pipe,
Provider,
ɵReflectionCapabilities as ReflectionCapabilities,
} from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {
BrowserAnimationsModule,
NoopAnimationsModule,
provideAnimations,
provideNoopAnimations,
} from '@angular/platform-browser/animations';
import dedent from 'ts-dedent';
import { NgModuleMetadata } from '../../types';
import { isComponentAlreadyDeclared } from './NgModulesAnalyzer';
export const reflectionCapabilities = new ReflectionCapabilities();
export const REMOVED_MODULES = new InjectionToken('REMOVED_MODULES');
export const uniqueArray = (arr: any[]) => {
return arr
.flat(Number.MAX_VALUE)
.filter(Boolean)
.filter((value, index, self) => self.indexOf(value) === index);
};
export class PropertyExtractor implements NgModuleMetadata {
/* eslint-disable @typescript-eslint/lines-between-class-members */
declarations?: any[] = [];
imports?: any[];
providers?: Provider[];
applicationProviders?: Array<Provider | ReturnType<typeof importProvidersFrom>>;
/* eslint-enable @typescript-eslint/lines-between-class-members */
constructor(
private metadata: NgModuleMetadata,
private component?: any
) {
this.init();
}
// With the new way of mounting standalone components to the DOM via bootstrapApplication API,
// we should now pass ModuleWithProviders to the providers array of the bootstrapApplication function.
static warnImportsModuleWithProviders(propertyExtractor: PropertyExtractor) {
const hasModuleWithProvidersImport = propertyExtractor.imports.some(
(importedModule) => 'ngModule' in importedModule
);
if (hasModuleWithProvidersImport) {
console.warn(
dedent(
`
Storybook Warning:
moduleMetadata property 'imports' contains one or more ModuleWithProviders, likely the result of a 'Module.forRoot()'-style call.
In Storybook 7.0 we use Angular's new 'bootstrapApplication' API to mount the component to the DOM, which accepts a list of providers to set up application-wide providers.
Use the 'applicationConfig' decorator from '@storybook/angular' to pass your ModuleWithProviders to the 'providers' property in combination with the importProvidersFrom helper function from '@angular/core' to extract all the necessary providers.
Visit https://angular.io/guide/standalone-components#configuring-dependency-injection for more information
`
)
);
}
}
private init() {
const analyzed = this.analyzeMetadata(this.metadata);
this.imports = uniqueArray([CommonModule, analyzed.imports]);
this.providers = uniqueArray(analyzed.providers);
this.applicationProviders = uniqueArray(analyzed.applicationProviders);
this.declarations = uniqueArray(analyzed.declarations);
if (this.component) {
const { isDeclarable, isStandalone } = PropertyExtractor.analyzeDecorators(this.component);
const isDeclared = isComponentAlreadyDeclared(
this.component,
analyzed.declarations,
this.imports
);
if (isStandalone) {
this.imports.push(this.component);
} else if (isDeclarable && !isDeclared) {
this.declarations.push(this.component);
}
}
}
/**
* Analyze NgModule Metadata
*
* - Removes Restricted Imports
* - Extracts providers from ModuleWithProviders
* - Returns a new NgModuleMetadata object
*
*
*/
private analyzeMetadata = (metadata: NgModuleMetadata) => {
const declarations = [...(metadata?.declarations || [])];
const providers = [...(metadata?.providers || [])];
const applicationProviders: Provider[] = [];
const imports = [...(metadata?.imports || [])].reduce((acc, imported) => {
// remove ngModule and use only its providers if it is restricted
// (e.g. BrowserModule, BrowserAnimationsModule, NoopAnimationsModule, ...etc)
const [isRestricted, restrictedProviders] = PropertyExtractor.analyzeRestricted(imported);
if (isRestricted) {
applicationProviders.unshift(restrictedProviders || []);
return acc;
}
acc.push(imported);
return acc;
}, []);
return { ...metadata, imports, providers, applicationProviders, declarations };
};
static analyzeRestricted = (ngModule: NgModule): [boolean] | [boolean, Provider] => {
if (ngModule === BrowserModule) {
console.warn(
dedent`
Storybook Warning:
You have imported the "BrowserModule", which is not necessary anymore.
In Storybook v7.0 we are using Angular's new bootstrapApplication API to mount an Angular application to the DOM.
Note that the BrowserModule providers are automatically included when starting an application with bootstrapApplication()
Please remove the "BrowserModule" from the list of imports in your moduleMetadata definition to remove this warning.
`
);
return [true];
}
if (ngModule === BrowserAnimationsModule) {
console.warn(
dedent`
Storybook Warning:
You have added the "BrowserAnimationsModule" to the list of "imports" in your moduleMetadata definition of your Story.
In Storybook 7.0 we use Angular's new 'bootstrapApplication' API to mount the component to the DOM, which accepts a list of providers to set up application-wide providers.
Use the 'applicationConfig' decorator from '@storybook/angular' and add the "provideAnimations" function to the list of "providers".
If your Angular version does not support "provide-like" functions, use the helper function importProvidersFrom instead to set up animations. For this case, please add "importProvidersFrom(BrowserAnimationsModule)" to the list of providers of your applicationConfig definition.
Please visit https://angular.io/guide/standalone-components#configuring-dependency-injection for more information.
`
);
return [true, provideAnimations()];
}
if (ngModule === NoopAnimationsModule) {
console.warn(
dedent`
Storybook Warning:
You have added the "NoopAnimationsModule" to the list of "imports" in your moduleMetadata definition of your Story.
In Storybook v7.0 we are using Angular's new bootstrapApplication API to mount an Angular application to the DOM, which accepts a list of providers to set up application-wide providers.
Use the 'applicationConfig' decorator from '@storybook/angular' and add the "provideNoopAnimations" function to the list of "providers".
If your Angular version does not support "provide-like" functions, use the helper function importProvidersFrom instead to set up noop animations and to extract all necessary providers from NoopAnimationsModule. For this case, please add "importProvidersFrom(NoopAnimationsModule)" to the list of providers of your applicationConfig definition.
Please visit https://angular.io/guide/standalone-components#configuring-dependency-injection for more information.
`
);
return [true, provideNoopAnimations()];
}
return [false];
};
static analyzeDecorators = (component: any) => {
const decorators = reflectionCapabilities.annotations(component);
const isComponent = decorators.some((d) => this.isDecoratorInstanceOf(d, 'Component'));
const isDirective = decorators.some((d) => this.isDecoratorInstanceOf(d, 'Directive'));
const isPipe = decorators.some((d) => this.isDecoratorInstanceOf(d, 'Pipe'));
const isDeclarable = isComponent || isDirective || isPipe;
const isStandalone = (isComponent || isDirective) && decorators.some((d) => d.standalone);
return { isDeclarable, isStandalone };
};
static isDecoratorInstanceOf = (decorator: any, name: string) => {
let factory;
switch (name) {
case 'Component':
factory = Component;
break;
case 'Directive':
factory = Directive;
break;
case 'Pipe':
factory = Pipe;
break;
case 'Injectable':
factory = Injectable;
break;
case 'Input':
factory = Input;
break;
case 'Output':
factory = Output;
break;
default:
throw new Error(`Unknown decorator type: ${name}`);
}
return decorator instanceof factory || decorator.ngMetadataName === name;
};
}