2016-11-15 11:49:23 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-12-16 18:33:47 -05:00
|
|
|
import {ModuleMetadata} from '@angular/tsc-wrapped';
|
2016-11-15 11:49:23 -05:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
2016-11-17 15:24:33 -05:00
|
|
|
import {CompilerHost} from '../src/compiler_host';
|
2016-11-15 11:49:23 -05:00
|
|
|
|
2016-11-17 15:24:33 -05:00
|
|
|
import {Directory, Entry, MockAotContext, MockCompilerHost} from './mocks';
|
2016-11-15 11:49:23 -05:00
|
|
|
|
2016-11-17 15:24:33 -05:00
|
|
|
describe('CompilerHost', () => {
|
|
|
|
let context: MockAotContext;
|
2016-11-15 11:49:23 -05:00
|
|
|
let program: ts.Program;
|
2016-11-17 15:24:33 -05:00
|
|
|
let hostNestedGenDir: CompilerHost;
|
|
|
|
let hostSiblingGenDir: CompilerHost;
|
2016-11-15 11:49:23 -05:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-11-17 15:24:33 -05:00
|
|
|
context = new MockAotContext('/tmp/src', clone(FILES));
|
2016-12-01 16:24:51 -05:00
|
|
|
const host = new MockCompilerHost(context);
|
2016-11-15 11:49:23 -05:00
|
|
|
program = ts.createProgram(
|
|
|
|
['main.ts'], {
|
|
|
|
module: ts.ModuleKind.CommonJS,
|
|
|
|
},
|
|
|
|
host);
|
|
|
|
// Force a typecheck
|
|
|
|
const errors = program.getSemanticDiagnostics();
|
|
|
|
if (errors && errors.length) {
|
|
|
|
throw new Error('Expected no errors');
|
|
|
|
}
|
2016-11-17 15:24:33 -05:00
|
|
|
hostNestedGenDir = new CompilerHost(
|
2016-12-01 16:24:51 -05:00
|
|
|
program, {
|
2016-11-15 11:49:23 -05:00
|
|
|
genDir: '/tmp/project/src/gen/',
|
|
|
|
basePath: '/tmp/project/src',
|
|
|
|
skipMetadataEmit: false,
|
|
|
|
strictMetadataEmit: false,
|
|
|
|
skipTemplateCodegen: false,
|
|
|
|
trace: false
|
|
|
|
},
|
|
|
|
context);
|
2016-11-17 15:24:33 -05:00
|
|
|
hostSiblingGenDir = new CompilerHost(
|
2016-12-01 16:24:51 -05:00
|
|
|
program, {
|
2016-11-15 11:49:23 -05:00
|
|
|
genDir: '/tmp/project/gen',
|
|
|
|
basePath: '/tmp/project/src/',
|
|
|
|
skipMetadataEmit: false,
|
|
|
|
strictMetadataEmit: false,
|
|
|
|
skipTemplateCodegen: false,
|
|
|
|
trace: false
|
|
|
|
},
|
|
|
|
context);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('nestedGenDir', () => {
|
|
|
|
it('should import node_module from factory', () => {
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/node_modules/@angular/core.d.ts',
|
|
|
|
'/tmp/project/src/gen/my.ngfactory.ts', ))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('@angular/core');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should import factory from factory', () => {
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/src/my.other.ngfactory.ts', '/tmp/project/src/my.ngfactory.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('./my.other.ngfactory');
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
2016-12-13 20:34:46 -05:00
|
|
|
'/tmp/project/src/my.other.css.ngstyle.ts', '/tmp/project/src/a/my.ngfactory.ts'))
|
|
|
|
.toEqual('../my.other.css.ngstyle');
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
2016-12-13 20:34:46 -05:00
|
|
|
'/tmp/project/src/a/my.other.shim.ngstyle.ts', '/tmp/project/src/my.ngfactory.ts'))
|
|
|
|
.toEqual('./a/my.other.shim.ngstyle');
|
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
|
|
|
'/tmp/project/src/my.other.sass.ngstyle.ts', '/tmp/project/src/a/my.ngfactory.ts'))
|
|
|
|
.toEqual('../my.other.sass.ngstyle');
|
2016-11-15 11:49:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should import application from factory', () => {
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/src/my.other.ts', '/tmp/project/src/my.ngfactory.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('../my.other');
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/src/my.other.ts', '/tmp/project/src/a/my.ngfactory.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('../../my.other');
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/src/a/my.other.ts', '/tmp/project/src/my.ngfactory.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('../a/my.other');
|
2016-12-13 20:34:46 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
|
|
|
'/tmp/project/src/a/my.other.css.ts', '/tmp/project/src/my.ngfactory.ts'))
|
|
|
|
.toEqual('../a/my.other.css');
|
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
|
|
|
'/tmp/project/src/a/my.other.css.shim.ts', '/tmp/project/src/my.ngfactory.ts'))
|
|
|
|
.toEqual('../a/my.other.css.shim');
|
2016-11-15 11:49:23 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-15 14:13:20 -05:00
|
|
|
describe('siblingGenDir', () => {
|
2016-11-15 11:49:23 -05:00
|
|
|
it('should import node_module from factory', () => {
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostSiblingGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/node_modules/@angular/core.d.ts',
|
|
|
|
'/tmp/project/src/gen/my.ngfactory.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('@angular/core');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should import factory from factory', () => {
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostSiblingGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/src/my.other.ngfactory.ts', '/tmp/project/src/my.ngfactory.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('./my.other.ngfactory');
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostSiblingGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/src/my.other.css.ts', '/tmp/project/src/a/my.ngfactory.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('../my.other.css');
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostSiblingGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/src/a/my.other.css.shim.ts', '/tmp/project/src/my.ngfactory.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('./a/my.other.css.shim');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should import application from factory', () => {
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostSiblingGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/src/my.other.ts', '/tmp/project/src/my.ngfactory.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('./my.other');
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostSiblingGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/src/my.other.ts', '/tmp/project/src/a/my.ngfactory.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('../my.other');
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostSiblingGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/src/a/my.other.ts', '/tmp/project/src/my.ngfactory.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('./a/my.other');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to produce an import from main @angular/core', () => {
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
2016-11-15 14:13:20 -05:00
|
|
|
'/tmp/project/node_modules/@angular/core.d.ts', '/tmp/project/src/main.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('@angular/core');
|
|
|
|
});
|
|
|
|
|
2017-02-22 20:23:08 -05:00
|
|
|
it('should be able to produce an import to a shallow import', () => {
|
|
|
|
expect(hostNestedGenDir.fileNameToModuleName('@angular/core', '/tmp/project/src/main.ts'))
|
|
|
|
.toEqual('@angular/core');
|
|
|
|
expect(hostNestedGenDir.fileNameToModuleName(
|
|
|
|
'@angular/upgrade/static', '/tmp/project/src/main.ts'))
|
|
|
|
.toEqual('@angular/upgrade/static');
|
|
|
|
expect(hostNestedGenDir.fileNameToModuleName('myLibrary', '/tmp/project/src/main.ts'))
|
|
|
|
.toEqual('myLibrary');
|
|
|
|
expect(hostNestedGenDir.fileNameToModuleName('lib23-43', '/tmp/project/src/main.ts'))
|
|
|
|
.toEqual('lib23-43');
|
|
|
|
});
|
|
|
|
|
2016-11-15 11:49:23 -05:00
|
|
|
it('should be able to produce an import from main to a sub-directory', () => {
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName('lib/utils.ts', 'main.ts')).toEqual('./lib/utils');
|
2016-11-15 11:49:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to produce an import from to a peer file', () => {
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName('lib/collections.ts', 'lib/utils.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('./collections');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to produce an import from to a sibling directory', () => {
|
2016-11-17 15:24:33 -05:00
|
|
|
expect(hostNestedGenDir.fileNameToModuleName('lib/utils.ts', 'lib2/utils2.ts'))
|
2016-11-15 11:49:23 -05:00
|
|
|
.toEqual('../lib/utils');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to read a metadata file', () => {
|
2016-11-17 12:52:38 -05:00
|
|
|
expect(hostNestedGenDir.getMetadataFor('node_modules/@angular/core.d.ts')).toEqual([
|
2016-12-16 18:33:47 -05:00
|
|
|
{__symbolic: 'module', version: 3, metadata: {foo: {__symbolic: 'class'}}}
|
2016-11-17 12:52:38 -05:00
|
|
|
]);
|
2016-11-15 11:49:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to read metadata from an otherwise unused .d.ts file ', () => {
|
2016-12-16 18:33:47 -05:00
|
|
|
expect(hostNestedGenDir.getMetadataFor('node_modules/@angular/unused.d.ts')).toEqual([
|
|
|
|
dummyMetadata
|
|
|
|
]);
|
2016-11-15 11:49:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to read empty metadata ', () => {
|
2016-11-17 12:52:38 -05:00
|
|
|
expect(hostNestedGenDir.getMetadataFor('node_modules/@angular/empty.d.ts')).toEqual([]);
|
2016-11-15 11:49:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return undefined for missing modules', () => {
|
|
|
|
expect(hostNestedGenDir.getMetadataFor('node_modules/@angular/missing.d.ts')).toBeUndefined();
|
|
|
|
});
|
2016-11-17 12:52:38 -05:00
|
|
|
|
2016-12-14 18:28:51 -05:00
|
|
|
it('should add missing v3 metadata from v1 metadata and .d.ts files', () => {
|
2016-11-17 12:52:38 -05:00
|
|
|
expect(hostNestedGenDir.getMetadataFor('metadata_versions/v1.d.ts')).toEqual([
|
|
|
|
{__symbolic: 'module', version: 1, metadata: {foo: {__symbolic: 'class'}}}, {
|
|
|
|
__symbolic: 'module',
|
2016-12-14 18:28:51 -05:00
|
|
|
version: 3,
|
2016-11-18 18:17:44 -05:00
|
|
|
metadata: {
|
|
|
|
foo: {__symbolic: 'class'},
|
|
|
|
Bar: {__symbolic: 'class', members: {ngOnInit: [{__symbolic: 'method'}]}},
|
2016-12-13 13:56:51 -05:00
|
|
|
BarChild: {__symbolic: 'class', extends: {__symbolic: 'reference', name: 'Bar'}},
|
2016-12-14 14:37:01 -05:00
|
|
|
ReExport: {__symbolic: 'reference', module: './lib/utils2', name: 'ReExport'},
|
2016-12-13 13:56:51 -05:00
|
|
|
},
|
|
|
|
exports: [{from: './lib/utils2', export: ['Export']}],
|
2016-11-17 12:52:38 -05:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
});
|
2016-12-16 18:33:47 -05:00
|
|
|
|
|
|
|
it('should upgrade a missing metadata file into v3', () => {
|
|
|
|
expect(hostNestedGenDir.getMetadataFor('metadata_versions/v1_empty.d.ts')).toEqual([
|
|
|
|
{__symbolic: 'module', version: 3, metadata: {}, exports: [{from: './lib/utils'}]}
|
|
|
|
]);
|
|
|
|
});
|
2016-11-15 11:49:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const dummyModule = 'export let foo: any[];';
|
2016-12-16 18:33:47 -05:00
|
|
|
const dummyMetadata: ModuleMetadata = {
|
|
|
|
__symbolic: 'module',
|
|
|
|
version: 3,
|
|
|
|
metadata:
|
|
|
|
{foo: {__symbolic: 'error', message: 'Variable not initialized', line: 0, character: 11}}
|
|
|
|
};
|
2016-11-15 11:49:23 -05:00
|
|
|
const FILES: Entry = {
|
|
|
|
'tmp': {
|
|
|
|
'src': {
|
|
|
|
'main.ts': `
|
|
|
|
import * as c from '@angular/core';
|
|
|
|
import * as r from '@angular/router';
|
|
|
|
import * as u from './lib/utils';
|
|
|
|
import * as cs from './lib/collections';
|
|
|
|
import * as u2 from './lib2/utils2';
|
|
|
|
`,
|
|
|
|
'lib': {
|
|
|
|
'utils.ts': dummyModule,
|
|
|
|
'collections.ts': dummyModule,
|
|
|
|
},
|
|
|
|
'lib2': {'utils2.ts': dummyModule},
|
|
|
|
'node_modules': {
|
|
|
|
'@angular': {
|
|
|
|
'core.d.ts': dummyModule,
|
|
|
|
'core.metadata.json':
|
2016-12-16 18:33:47 -05:00
|
|
|
`{"__symbolic":"module", "version": 3, "metadata": {"foo": {"__symbolic": "class"}}}`,
|
2016-11-15 11:49:23 -05:00
|
|
|
'router': {'index.d.ts': dummyModule, 'src': {'providers.d.ts': dummyModule}},
|
|
|
|
'unused.d.ts': dummyModule,
|
|
|
|
'empty.d.ts': 'export declare var a: string;',
|
|
|
|
'empty.metadata.json': '[]',
|
|
|
|
}
|
2016-11-17 12:52:38 -05:00
|
|
|
},
|
|
|
|
'metadata_versions': {
|
2016-11-18 18:17:44 -05:00
|
|
|
'v1.d.ts': `
|
2016-12-14 14:37:01 -05:00
|
|
|
import {ReExport} from './lib/utils2';
|
|
|
|
export {ReExport};
|
|
|
|
|
2016-12-13 13:56:51 -05:00
|
|
|
export {Export} from './lib/utils2';
|
2016-12-14 14:37:01 -05:00
|
|
|
|
2016-11-18 18:17:44 -05:00
|
|
|
export declare class Bar {
|
|
|
|
ngOnInit() {}
|
|
|
|
}
|
|
|
|
export declare class BarChild extends Bar {}
|
|
|
|
`,
|
2016-11-17 12:52:38 -05:00
|
|
|
'v1.metadata.json':
|
|
|
|
`{"__symbolic":"module", "version": 1, "metadata": {"foo": {"__symbolic": "class"}}}`,
|
2016-12-16 18:33:47 -05:00
|
|
|
'v1_empty.d.ts': `
|
|
|
|
export * from './lib/utils';
|
|
|
|
`
|
2016-11-15 11:49:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function clone(entry: Entry): Entry {
|
|
|
|
if (typeof entry === 'string') {
|
|
|
|
return entry;
|
|
|
|
} else {
|
|
|
|
const result: Directory = {};
|
|
|
|
for (const name in entry) {
|
|
|
|
result[name] = clone(entry[name]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|