Skip to content

Commit

Permalink
feat: introduce support for Angular version 5 (#439)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mgechev authored Oct 29, 2017
1 parent 68932b4 commit 0217e2d
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 567 deletions.
20 changes: 0 additions & 20 deletions build/links.ts

This file was deleted.

35 changes: 17 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"name": "codelyzer",
"version": "3.2.1",
"version": "4.0.0",
"description": "Linting for Angular applications, following angular.io/styleguide.",
"main": "index.js",
"scripts": {
"docs": "ts-node build/buildDocs.ts",
"lint": "tslint -c tslint.json \"src/**/*.ts\" \"test/**/*.ts\"",
"lint:fix": "npm run lint -- --fix",
"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",
"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",
"build": "rimraf dist && tsc && npm run lint && npm t",
"copy:common": "cp README.md dist",
"build:links": "ts-node build/links.ts --src ./dist",
"prepare:package": "cat package.json | ts-node build/package.ts > dist/package.json",
"test": "rimraf dist && tsc && cp -r test/fixtures dist/test && mocha dist/test --recursive",
"test:watch": "rimraf dist && tsc && cp -r test/fixtures dist/test && BUILD_TYPE=dev npm run set:vars && mocha dist/test --watch --recursive",
"test:watch":
"rimraf dist && tsc && cp -r test/fixtures dist/test && BUILD_TYPE=dev npm run set:vars && mocha dist/test --watch --recursive",
"set:vars": "ts-node build/vars.ts --src ./dist",
"tscv": "tsc --version",
"tsc": "tsc",
Expand All @@ -28,15 +29,7 @@
"type": "git",
"url": "git+https://github.com/mgechev/codelyzer.git"
},
"keywords": [
"Angular",
"style guide",
"styleguide",
"nglint",
"codelyzer",
"lint",
"tslint"
],
"keywords": ["Angular", "style guide", "styleguide", "nglint", "codelyzer", "lint", "tslint"],
"author": {
"name": "Minko Gechev",
"email": "mgechev@gmail.com"
Expand All @@ -47,8 +40,11 @@
},
"homepage": "https://github.com/mgechev/codelyzer#readme",
"devDependencies": {
"@angular/compiler": "^4.4.0",
"@angular/core": "^4.4.0",
"@angular/compiler": "^5.0.0-rc.7",
"@angular/core": "^5.0.0-rc.7",
"@angular/platform-browser-dynamic": "^5.0.0-rc.7",
"@angular/common": "^5.0.0-rc.7",
"@angular/platform-browser": "^5.0.0-rc.7",
"@types/chai": "^3.4.33",
"@types/less": "0.0.31",
"@types/mocha": "^2.2.32",
Expand All @@ -62,7 +58,7 @@
"mocha": "3.0.2",
"node-sass": "^3.13.0",
"rimraf": "^2.5.2",
"rxjs": "5.4.1",
"rxjs": "^5.5.0",
"ts-node": "1.2.2",
"tslint": "^5.0.0",
"typescript": "2.4.0",
Expand All @@ -72,8 +68,11 @@
"@types/js-yaml": "^3.5.31"
},
"peerDependencies": {
"@angular/compiler": "^2.3.1 || >=4.0.0-beta <5.0.0",
"@angular/core": "^2.3.1 || >=4.0.0-beta <5.0.0",
"@angular/compiler": "^2.3.1 || >=4.0.0-beta <6.0.0",
"@angular/core": "^2.3.1 || >=4.0.0-beta <6.0.0",
"@angular/platform-browser-dynamic": "^2.3.1 || >=4.0.0-beta <6.0.0",
"@angular/platform-browser": "^2.3.1 || >=4.0.0-beta <6.0.0",
"@angular/common": "^2.3.1 || >=4.0.0-beta <6.0.0",
"tslint": "^5.0.0"
},
"dependencies": {
Expand Down
138 changes: 138 additions & 0 deletions src/angular/templates/jitReflector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { CompileReflector, ExternalReference, Identifiers, getUrlScheme, syntaxError } from '@angular/compiler';
import {
ANALYZE_FOR_ENTRY_COMPONENTS,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ComponentFactory,
ComponentFactoryResolver,
ComponentRef,
ElementRef,
Injector,
LOCALE_ID,
NgModuleFactory,
NgModuleRef,
QueryList,
Renderer,
SecurityContext,
TRANSLATIONS_FORMAT,
TemplateRef,
ViewContainerRef,
ViewEncapsulation,
ɵCodegenComponentFactoryResolver,
ɵEMPTY_ARRAY,
ɵEMPTY_MAP,
ɵReflectionCapabilities as ReflectionCapabilities,
ɵand,
ɵccf,
ɵcmf,
ɵcrt,
ɵdid,
ɵeld,
ɵinlineInterpolate,
ɵinterpolate,
ɵmod,
ɵmpd,
ɵncd,
ɵnov,
ɵpad,
ɵpid,
ɵpod,
ɵppd,
ɵprd,
ɵqud,
ɵregisterModuleFactory,
ɵstringify as stringify,
ɵted,
ɵunv,
ɵvid
} from '@angular/core';

export const MODULE_SUFFIX = '';
const builtinExternalReferences = createBuiltinExternalReferencesMap();

export class JitReflector implements CompileReflector {
private reflectionCapabilities: ReflectionCapabilities;
private builtinExternalReferences = new Map<ExternalReference, any>();
constructor() {
this.reflectionCapabilities = new ReflectionCapabilities();
}
componentModuleUrl(type: any, cmpMetadata: Component): string {
const moduleId = cmpMetadata.moduleId;

if (typeof moduleId === 'string') {
const scheme = getUrlScheme(moduleId);
return scheme ? moduleId : `package:${moduleId}${MODULE_SUFFIX}`;
} else if (moduleId !== null && moduleId !== void 0) {
throw syntaxError(
`moduleId should be a string in "${stringify(type)}". See https://goo.gl/wIDDiL for more information.\n` +
`If you're using Webpack you should inline the template and the styles, see https://goo.gl/X2J8zc.`
);
}

return `./${stringify(type)}`;
}
parameters(typeOrFunc: /*Type*/ any): any[][] {
return this.reflectionCapabilities.parameters(typeOrFunc);
}
annotations(typeOrFunc: /*Type*/ any): any[] {
return this.reflectionCapabilities.annotations(typeOrFunc);
}
propMetadata(typeOrFunc: /*Type*/ any): { [key: string]: any[] } {
return this.reflectionCapabilities.propMetadata(typeOrFunc);
}
hasLifecycleHook(type: any, lcProperty: string): boolean {
return this.reflectionCapabilities.hasLifecycleHook(type, lcProperty);
}
resolveExternalReference(ref: ExternalReference): any {
return builtinExternalReferences.get(ref) || ref.runtime;
}
}

function createBuiltinExternalReferencesMap() {
const map = new Map<ExternalReference, any>();
map.set(Identifiers.ANALYZE_FOR_ENTRY_COMPONENTS, ANALYZE_FOR_ENTRY_COMPONENTS);
map.set(Identifiers.ElementRef, ElementRef);
map.set(Identifiers.NgModuleRef, NgModuleRef);
map.set(Identifiers.ViewContainerRef, ViewContainerRef);
map.set(Identifiers.ChangeDetectorRef, ChangeDetectorRef);
map.set(Identifiers.QueryList, QueryList);
map.set(Identifiers.TemplateRef, TemplateRef);
map.set(Identifiers.CodegenComponentFactoryResolver, ɵCodegenComponentFactoryResolver);
map.set(Identifiers.ComponentFactoryResolver, ComponentFactoryResolver);
map.set(Identifiers.ComponentFactory, ComponentFactory);
map.set(Identifiers.ComponentRef, ComponentRef);
map.set(Identifiers.NgModuleFactory, NgModuleFactory);
map.set(Identifiers.createModuleFactory, ɵcmf);
map.set(Identifiers.moduleDef, ɵmod);
map.set(Identifiers.moduleProviderDef, ɵmpd);
map.set(Identifiers.RegisterModuleFactoryFn, ɵregisterModuleFactory);
map.set(Identifiers.Injector, Injector);
map.set(Identifiers.ViewEncapsulation, ViewEncapsulation);
map.set(Identifiers.ChangeDetectionStrategy, ChangeDetectionStrategy);
map.set(Identifiers.SecurityContext, SecurityContext);
map.set(Identifiers.LOCALE_ID, LOCALE_ID);
map.set(Identifiers.TRANSLATIONS_FORMAT, TRANSLATIONS_FORMAT);
map.set(Identifiers.inlineInterpolate, ɵinlineInterpolate);
map.set(Identifiers.interpolate, ɵinterpolate);
map.set(Identifiers.EMPTY_ARRAY, ɵEMPTY_ARRAY);
map.set(Identifiers.EMPTY_MAP, ɵEMPTY_MAP);
map.set(Identifiers.Renderer, Renderer);
map.set(Identifiers.viewDef, ɵvid);
map.set(Identifiers.elementDef, ɵeld);
map.set(Identifiers.anchorDef, ɵand);
map.set(Identifiers.textDef, ɵted);
map.set(Identifiers.directiveDef, ɵdid);
map.set(Identifiers.providerDef, ɵprd);
map.set(Identifiers.queryDef, ɵqud);
map.set(Identifiers.pureArrayDef, ɵpad);
map.set(Identifiers.pureObjectDef, ɵpod);
map.set(Identifiers.purePipeDef, ɵppd);
map.set(Identifiers.pipeDef, ɵpid);
map.set(Identifiers.nodeValue, ɵnov);
map.set(Identifiers.ngContentDef, ɵncd);
map.set(Identifiers.unwrapValue, ɵunv);
map.set(Identifiers.createRendererType2, ɵcrt);
map.set(Identifiers.createComponentFactory, ɵccf);
return map;
}
88 changes: 43 additions & 45 deletions src/angular/templates/templateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ class Console {

let defaultDirectives = [];

export const parseTemplate = (
template: string,
directives: DirectiveDeclaration[] = []
) => {
export const parseTemplate = (template: string, directives: DirectiveDeclaration[] = []) => {
defaultDirectives = directives.map(d => dummyMetadataFactory(d));

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

SemVerDSL.gte('4.0.0-beta.8', () => {
const config = new compiler.CompilerConfig({});
tmplParser = new TemplateParser(
config,
expressionParser,
elementSchemaRegistry,
htmlParser,
ngConsole,
[]
);
tmplParser = new TemplateParser(config, expressionParser, elementSchemaRegistry, htmlParser, ngConsole, []);
})
.elseIf.lt('4.1.0', () => {
tmplParser = new TemplateParser(expressionParser, elementSchemaRegistry, htmlParser, ngConsole, []);
})
.elseIf.lt('5.0.0-rc.0', () => {
const config = new compiler.CompilerConfig({});
tmplParser = new TemplateParser(
config,
new (compiler as any).JitReflector(),
expressionParser,
elementSchemaRegistry,
htmlParser,
Expand All @@ -82,13 +78,14 @@ export const parseTemplate = (
);
})
.else(() => {
const JitReflector = require('./jitReflector').JitReflector;
const config = new compiler.CompilerConfig({});
tmplParser = new TemplateParser(
tmplParser = new compiler.TemplateParser(
config,
new compiler.JitReflector(),
new JitReflector(),
expressionParser,
elementSchemaRegistry,
htmlParser,
htmlParser as any,
ngConsole,
[]
);
Expand Down Expand Up @@ -178,35 +175,35 @@ export const parseTemplate = (
''
).templateAst;
})
.elseIf.lt('4.4.0', () => {
result = tmplParser.tryParse(
compiler.CompileDirectiveMetadata.create({
type,
template: templateMetadata,
isHost: true,
isComponent: true,
selector: '',
exportAs: '',
changeDetection: ChangeDetectionStrategy.Default,
inputs: [],
outputs: [],
host: {},
providers: [],
viewProviders: [],
queries: [],
viewQueries: [],
entryComponents: [],
componentViewType: null,
rendererType: null,
componentFactory: null
}),
template,
defaultDirectives,
[],
[NO_ERRORS_SCHEMA],
''
).templateAst;
})
.elseIf.lt('5.0.0-rc.0', () => {
result = tmplParser.tryParse(
compiler.CompileDirectiveMetadata.create({
type,
template: templateMetadata,
isHost: true,
isComponent: true,
selector: '',
exportAs: '',
changeDetection: ChangeDetectionStrategy.Default,
inputs: [],
outputs: [],
host: {},
providers: [],
viewProviders: [],
queries: [],
viewQueries: [],
entryComponents: [],
componentViewType: null,
rendererType: null,
componentFactory: null
}),
template,
defaultDirectives,
[],
[NO_ERRORS_SCHEMA],
''
).templateAst;
})
.else(() => {
result = tmplParser.tryParse(
compiler.CompileDirectiveMetadata.create({
Expand All @@ -233,7 +230,8 @@ export const parseTemplate = (
defaultDirectives,
[],
[NO_ERRORS_SCHEMA],
'', true
'',
true
).templateAst;
});
} catch (e) {
Expand Down
Loading

0 comments on commit 0217e2d

Please sign in to comment.