Skip to content

Commit 0217e2d

Browse files
authored
feat: introduce support for Angular version 5 (#439)
* feat: support angular version 5 * fix: broken tests * chore: drop obsolete rule * refactor: drop useless log statement * refactor: drop useless log statement * chore: remove unused task * chore: update dependencies * chore: fix build
1 parent 68932b4 commit 0217e2d

File tree

12 files changed

+273
-567
lines changed

12 files changed

+273
-567
lines changed

build/links.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

package.json

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
{
22
"name": "codelyzer",
3-
"version": "3.2.1",
3+
"version": "4.0.0",
44
"description": "Linting for Angular applications, following angular.io/styleguide.",
55
"main": "index.js",
66
"scripts": {
77
"docs": "ts-node build/buildDocs.ts",
88
"lint": "tslint -c tslint.json \"src/**/*.ts\" \"test/**/*.ts\"",
99
"lint:fix": "npm run lint -- --fix",
10-
"release": "npm run build && rimraf dist && tsc -p tsconfig-release.json && npm run copy:common && npm run prepare:package && BUILD_TYPE=prod npm run set:vars && npm run build:links",
10+
"release":
11+
"npm run build && rimraf dist && tsc -p tsconfig-release.json && npm run copy:common && npm run prepare:package && BUILD_TYPE=prod npm run set:vars",
1112
"build": "rimraf dist && tsc && npm run lint && npm t",
1213
"copy:common": "cp README.md dist",
13-
"build:links": "ts-node build/links.ts --src ./dist",
1414
"prepare:package": "cat package.json | ts-node build/package.ts > dist/package.json",
1515
"test": "rimraf dist && tsc && cp -r test/fixtures dist/test && mocha dist/test --recursive",
16-
"test:watch": "rimraf dist && tsc && cp -r test/fixtures dist/test && BUILD_TYPE=dev npm run set:vars && mocha dist/test --watch --recursive",
16+
"test:watch":
17+
"rimraf dist && tsc && cp -r test/fixtures dist/test && BUILD_TYPE=dev npm run set:vars && mocha dist/test --watch --recursive",
1718
"set:vars": "ts-node build/vars.ts --src ./dist",
1819
"tscv": "tsc --version",
1920
"tsc": "tsc",
@@ -28,15 +29,7 @@
2829
"type": "git",
2930
"url": "git+https://github.com/mgechev/codelyzer.git"
3031
},
31-
"keywords": [
32-
"Angular",
33-
"style guide",
34-
"styleguide",
35-
"nglint",
36-
"codelyzer",
37-
"lint",
38-
"tslint"
39-
],
32+
"keywords": ["Angular", "style guide", "styleguide", "nglint", "codelyzer", "lint", "tslint"],
4033
"author": {
4134
"name": "Minko Gechev",
4235
"email": "mgechev@gmail.com"
@@ -47,8 +40,11 @@
4740
},
4841
"homepage": "https://github.com/mgechev/codelyzer#readme",
4942
"devDependencies": {
50-
"@angular/compiler": "^4.4.0",
51-
"@angular/core": "^4.4.0",
43+
"@angular/compiler": "^5.0.0-rc.7",
44+
"@angular/core": "^5.0.0-rc.7",
45+
"@angular/platform-browser-dynamic": "^5.0.0-rc.7",
46+
"@angular/common": "^5.0.0-rc.7",
47+
"@angular/platform-browser": "^5.0.0-rc.7",
5248
"@types/chai": "^3.4.33",
5349
"@types/less": "0.0.31",
5450
"@types/mocha": "^2.2.32",
@@ -62,7 +58,7 @@
6258
"mocha": "3.0.2",
6359
"node-sass": "^3.13.0",
6460
"rimraf": "^2.5.2",
65-
"rxjs": "5.4.1",
61+
"rxjs": "^5.5.0",
6662
"ts-node": "1.2.2",
6763
"tslint": "^5.0.0",
6864
"typescript": "2.4.0",
@@ -72,8 +68,11 @@
7268
"@types/js-yaml": "^3.5.31"
7369
},
7470
"peerDependencies": {
75-
"@angular/compiler": "^2.3.1 || >=4.0.0-beta <5.0.0",
76-
"@angular/core": "^2.3.1 || >=4.0.0-beta <5.0.0",
71+
"@angular/compiler": "^2.3.1 || >=4.0.0-beta <6.0.0",
72+
"@angular/core": "^2.3.1 || >=4.0.0-beta <6.0.0",
73+
"@angular/platform-browser-dynamic": "^2.3.1 || >=4.0.0-beta <6.0.0",
74+
"@angular/platform-browser": "^2.3.1 || >=4.0.0-beta <6.0.0",
75+
"@angular/common": "^2.3.1 || >=4.0.0-beta <6.0.0",
7776
"tslint": "^5.0.0"
7877
},
7978
"dependencies": {

src/angular/templates/jitReflector.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { CompileReflector, ExternalReference, Identifiers, getUrlScheme, syntaxError } from '@angular/compiler';
2+
import {
3+
ANALYZE_FOR_ENTRY_COMPONENTS,
4+
ChangeDetectionStrategy,
5+
ChangeDetectorRef,
6+
Component,
7+
ComponentFactory,
8+
ComponentFactoryResolver,
9+
ComponentRef,
10+
ElementRef,
11+
Injector,
12+
LOCALE_ID,
13+
NgModuleFactory,
14+
NgModuleRef,
15+
QueryList,
16+
Renderer,
17+
SecurityContext,
18+
TRANSLATIONS_FORMAT,
19+
TemplateRef,
20+
ViewContainerRef,
21+
ViewEncapsulation,
22+
ɵCodegenComponentFactoryResolver,
23+
ɵEMPTY_ARRAY,
24+
ɵEMPTY_MAP,
25+
ɵReflectionCapabilities as ReflectionCapabilities,
26+
ɵand,
27+
ɵccf,
28+
ɵcmf,
29+
ɵcrt,
30+
ɵdid,
31+
ɵeld,
32+
ɵinlineInterpolate,
33+
ɵinterpolate,
34+
ɵmod,
35+
ɵmpd,
36+
ɵncd,
37+
ɵnov,
38+
ɵpad,
39+
ɵpid,
40+
ɵpod,
41+
ɵppd,
42+
ɵprd,
43+
ɵqud,
44+
ɵregisterModuleFactory,
45+
ɵstringify as stringify,
46+
ɵted,
47+
ɵunv,
48+
ɵvid
49+
} from '@angular/core';
50+
51+
export const MODULE_SUFFIX = '';
52+
const builtinExternalReferences = createBuiltinExternalReferencesMap();
53+
54+
export class JitReflector implements CompileReflector {
55+
private reflectionCapabilities: ReflectionCapabilities;
56+
private builtinExternalReferences = new Map<ExternalReference, any>();
57+
constructor() {
58+
this.reflectionCapabilities = new ReflectionCapabilities();
59+
}
60+
componentModuleUrl(type: any, cmpMetadata: Component): string {
61+
const moduleId = cmpMetadata.moduleId;
62+
63+
if (typeof moduleId === 'string') {
64+
const scheme = getUrlScheme(moduleId);
65+
return scheme ? moduleId : `package:${moduleId}${MODULE_SUFFIX}`;
66+
} else if (moduleId !== null && moduleId !== void 0) {
67+
throw syntaxError(
68+
`moduleId should be a string in "${stringify(type)}". See https://goo.gl/wIDDiL for more information.\n` +
69+
`If you're using Webpack you should inline the template and the styles, see https://goo.gl/X2J8zc.`
70+
);
71+
}
72+
73+
return `./${stringify(type)}`;
74+
}
75+
parameters(typeOrFunc: /*Type*/ any): any[][] {
76+
return this.reflectionCapabilities.parameters(typeOrFunc);
77+
}
78+
annotations(typeOrFunc: /*Type*/ any): any[] {
79+
return this.reflectionCapabilities.annotations(typeOrFunc);
80+
}
81+
propMetadata(typeOrFunc: /*Type*/ any): { [key: string]: any[] } {
82+
return this.reflectionCapabilities.propMetadata(typeOrFunc);
83+
}
84+
hasLifecycleHook(type: any, lcProperty: string): boolean {
85+
return this.reflectionCapabilities.hasLifecycleHook(type, lcProperty);
86+
}
87+
resolveExternalReference(ref: ExternalReference): any {
88+
return builtinExternalReferences.get(ref) || ref.runtime;
89+
}
90+
}
91+
92+
function createBuiltinExternalReferencesMap() {
93+
const map = new Map<ExternalReference, any>();
94+
map.set(Identifiers.ANALYZE_FOR_ENTRY_COMPONENTS, ANALYZE_FOR_ENTRY_COMPONENTS);
95+
map.set(Identifiers.ElementRef, ElementRef);
96+
map.set(Identifiers.NgModuleRef, NgModuleRef);
97+
map.set(Identifiers.ViewContainerRef, ViewContainerRef);
98+
map.set(Identifiers.ChangeDetectorRef, ChangeDetectorRef);
99+
map.set(Identifiers.QueryList, QueryList);
100+
map.set(Identifiers.TemplateRef, TemplateRef);
101+
map.set(Identifiers.CodegenComponentFactoryResolver, ɵCodegenComponentFactoryResolver);
102+
map.set(Identifiers.ComponentFactoryResolver, ComponentFactoryResolver);
103+
map.set(Identifiers.ComponentFactory, ComponentFactory);
104+
map.set(Identifiers.ComponentRef, ComponentRef);
105+
map.set(Identifiers.NgModuleFactory, NgModuleFactory);
106+
map.set(Identifiers.createModuleFactory, ɵcmf);
107+
map.set(Identifiers.moduleDef, ɵmod);
108+
map.set(Identifiers.moduleProviderDef, ɵmpd);
109+
map.set(Identifiers.RegisterModuleFactoryFn, ɵregisterModuleFactory);
110+
map.set(Identifiers.Injector, Injector);
111+
map.set(Identifiers.ViewEncapsulation, ViewEncapsulation);
112+
map.set(Identifiers.ChangeDetectionStrategy, ChangeDetectionStrategy);
113+
map.set(Identifiers.SecurityContext, SecurityContext);
114+
map.set(Identifiers.LOCALE_ID, LOCALE_ID);
115+
map.set(Identifiers.TRANSLATIONS_FORMAT, TRANSLATIONS_FORMAT);
116+
map.set(Identifiers.inlineInterpolate, ɵinlineInterpolate);
117+
map.set(Identifiers.interpolate, ɵinterpolate);
118+
map.set(Identifiers.EMPTY_ARRAY, ɵEMPTY_ARRAY);
119+
map.set(Identifiers.EMPTY_MAP, ɵEMPTY_MAP);
120+
map.set(Identifiers.Renderer, Renderer);
121+
map.set(Identifiers.viewDef, ɵvid);
122+
map.set(Identifiers.elementDef, ɵeld);
123+
map.set(Identifiers.anchorDef, ɵand);
124+
map.set(Identifiers.textDef, ɵted);
125+
map.set(Identifiers.directiveDef, ɵdid);
126+
map.set(Identifiers.providerDef, ɵprd);
127+
map.set(Identifiers.queryDef, ɵqud);
128+
map.set(Identifiers.pureArrayDef, ɵpad);
129+
map.set(Identifiers.pureObjectDef, ɵpod);
130+
map.set(Identifiers.purePipeDef, ɵppd);
131+
map.set(Identifiers.pipeDef, ɵpid);
132+
map.set(Identifiers.nodeValue, ɵnov);
133+
map.set(Identifiers.ngContentDef, ɵncd);
134+
map.set(Identifiers.unwrapValue, ɵunv);
135+
map.set(Identifiers.createRendererType2, ɵcrt);
136+
map.set(Identifiers.createComponentFactory, ɵccf);
137+
return map;
138+
}

src/angular/templates/templateParser.ts

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ class Console {
4747

4848
let defaultDirectives = [];
4949

50-
export const parseTemplate = (
51-
template: string,
52-
directives: DirectiveDeclaration[] = []
53-
) => {
50+
export const parseTemplate = (template: string, directives: DirectiveDeclaration[] = []) => {
5451
defaultDirectives = directives.map(d => dummyMetadataFactory(d));
5552

5653
const TemplateParser = <any>compiler.TemplateParser;
@@ -63,17 +60,16 @@ export const parseTemplate = (
6360

6461
SemVerDSL.gte('4.0.0-beta.8', () => {
6562
const config = new compiler.CompilerConfig({});
66-
tmplParser = new TemplateParser(
67-
config,
68-
expressionParser,
69-
elementSchemaRegistry,
70-
htmlParser,
71-
ngConsole,
72-
[]
73-
);
63+
tmplParser = new TemplateParser(config, expressionParser, elementSchemaRegistry, htmlParser, ngConsole, []);
7464
})
7565
.elseIf.lt('4.1.0', () => {
66+
tmplParser = new TemplateParser(expressionParser, elementSchemaRegistry, htmlParser, ngConsole, []);
67+
})
68+
.elseIf.lt('5.0.0-rc.0', () => {
69+
const config = new compiler.CompilerConfig({});
7670
tmplParser = new TemplateParser(
71+
config,
72+
new (compiler as any).JitReflector(),
7773
expressionParser,
7874
elementSchemaRegistry,
7975
htmlParser,
@@ -82,13 +78,14 @@ export const parseTemplate = (
8278
);
8379
})
8480
.else(() => {
81+
const JitReflector = require('./jitReflector').JitReflector;
8582
const config = new compiler.CompilerConfig({});
86-
tmplParser = new TemplateParser(
83+
tmplParser = new compiler.TemplateParser(
8784
config,
88-
new compiler.JitReflector(),
85+
new JitReflector(),
8986
expressionParser,
9087
elementSchemaRegistry,
91-
htmlParser,
88+
htmlParser as any,
9289
ngConsole,
9390
[]
9491
);
@@ -178,35 +175,35 @@ export const parseTemplate = (
178175
''
179176
).templateAst;
180177
})
181-
.elseIf.lt('4.4.0', () => {
182-
result = tmplParser.tryParse(
183-
compiler.CompileDirectiveMetadata.create({
184-
type,
185-
template: templateMetadata,
186-
isHost: true,
187-
isComponent: true,
188-
selector: '',
189-
exportAs: '',
190-
changeDetection: ChangeDetectionStrategy.Default,
191-
inputs: [],
192-
outputs: [],
193-
host: {},
194-
providers: [],
195-
viewProviders: [],
196-
queries: [],
197-
viewQueries: [],
198-
entryComponents: [],
199-
componentViewType: null,
200-
rendererType: null,
201-
componentFactory: null
202-
}),
203-
template,
204-
defaultDirectives,
205-
[],
206-
[NO_ERRORS_SCHEMA],
207-
''
208-
).templateAst;
209-
})
178+
.elseIf.lt('5.0.0-rc.0', () => {
179+
result = tmplParser.tryParse(
180+
compiler.CompileDirectiveMetadata.create({
181+
type,
182+
template: templateMetadata,
183+
isHost: true,
184+
isComponent: true,
185+
selector: '',
186+
exportAs: '',
187+
changeDetection: ChangeDetectionStrategy.Default,
188+
inputs: [],
189+
outputs: [],
190+
host: {},
191+
providers: [],
192+
viewProviders: [],
193+
queries: [],
194+
viewQueries: [],
195+
entryComponents: [],
196+
componentViewType: null,
197+
rendererType: null,
198+
componentFactory: null
199+
}),
200+
template,
201+
defaultDirectives,
202+
[],
203+
[NO_ERRORS_SCHEMA],
204+
''
205+
).templateAst;
206+
})
210207
.else(() => {
211208
result = tmplParser.tryParse(
212209
compiler.CompileDirectiveMetadata.create({
@@ -233,7 +230,8 @@ export const parseTemplate = (
233230
defaultDirectives,
234231
[],
235232
[NO_ERRORS_SCHEMA],
236-
'', true
233+
'',
234+
true
237235
).templateAst;
238236
});
239237
} catch (e) {

0 commit comments

Comments
 (0)