|
7 | 7 | */
|
8 | 8 | import {ExternalExpr} from '@angular/compiler';
|
9 | 9 | import * as ts from 'typescript';
|
| 10 | +import {UnifiedModulesHost} from '../../core/api'; |
10 | 11 |
|
11 |
| -import {absoluteFrom as _, LogicalFileSystem} from '../../file_system'; |
| 12 | +import {absoluteFrom as _, basename, LogicalFileSystem} from '../../file_system'; |
12 | 13 | import {runInEachFileSystem, TestFile} from '../../file_system/testing';
|
13 | 14 | import {Declaration, TypeScriptReflectionHost} from '../../reflection';
|
14 | 15 | import {getDeclaration, makeProgram} from '../../testing';
|
15 |
| -import {AbsoluteModuleStrategy, ImportFlags, LogicalProjectStrategy} from '../src/emitter'; |
| 16 | +import {AbsoluteModuleStrategy, ImportFlags, LogicalProjectStrategy, RelativePathStrategy, UnifiedModulesStrategy} from '../src/emitter'; |
16 | 17 | import {Reference} from '../src/references';
|
17 | 18 | import {ModuleResolver} from '../src/resolver';
|
18 | 19 |
|
@@ -49,6 +50,41 @@ runInEachFileSystem(() => {
|
49 | 50 | expect(emitted).toBeNull();
|
50 | 51 | });
|
51 | 52 |
|
| 53 | + it('should prefer non-aliased exports', () => { |
| 54 | + const {strategy, program} = makeStrategy([ |
| 55 | + { |
| 56 | + name: _('/node_modules/external.d.ts'), |
| 57 | + contents: ` |
| 58 | + declare class Foo {} |
| 59 | + export {Foo as A}; |
| 60 | + export {Foo}; |
| 61 | + export {Foo as B}; |
| 62 | + `, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: _('/context.ts'), |
| 66 | + contents: 'export class Context {}', |
| 67 | + }, |
| 68 | + ]); |
| 69 | + const decl = |
| 70 | + getDeclaration(program, _('/node_modules/external.d.ts'), 'Foo', ts.isClassDeclaration); |
| 71 | + const context = program.getSourceFile(_('/context.ts'))!; |
| 72 | + |
| 73 | + const reference = new Reference(decl, { |
| 74 | + specifier: 'external', |
| 75 | + resolutionContext: context.fileName, |
| 76 | + }); |
| 77 | + const emitted = strategy.emit(reference, context, ImportFlags.None); |
| 78 | + if (emitted === null) { |
| 79 | + return fail('Reference should be emitted'); |
| 80 | + } |
| 81 | + if (!(emitted.expression instanceof ExternalExpr)) { |
| 82 | + return fail('Reference should be emitted as ExternalExpr'); |
| 83 | + } |
| 84 | + expect(emitted.expression.value.name).toEqual('Foo'); |
| 85 | + expect(emitted.expression.value.moduleName).toEqual('external'); |
| 86 | + }); |
| 87 | + |
52 | 88 | it('should generate an import using the exported name of the declaration', () => {
|
53 | 89 | const {strategy, program} = makeStrategy([
|
54 | 90 | {
|
@@ -173,5 +209,108 @@ runInEachFileSystem(() => {
|
173 | 209 | // Expect the prefixed name from the TestHost.
|
174 | 210 | expect((ref!.expression as ExternalExpr).value.name).toEqual('testFoo');
|
175 | 211 | });
|
| 212 | + |
| 213 | + it('should prefer non-aliased exports', () => { |
| 214 | + const {program, host} = makeProgram([ |
| 215 | + { |
| 216 | + name: _('/index.ts'), |
| 217 | + contents: ` |
| 218 | + declare class Foo {} |
| 219 | + export {Foo as A}; |
| 220 | + export {Foo}; |
| 221 | + export {Foo as B}; |
| 222 | + `, |
| 223 | + }, |
| 224 | + { |
| 225 | + name: _('/context.ts'), |
| 226 | + contents: 'export class Context {}', |
| 227 | + } |
| 228 | + ]); |
| 229 | + const checker = program.getTypeChecker(); |
| 230 | + const logicalFs = new LogicalFileSystem([_('/')], host); |
| 231 | + const strategy = new LogicalProjectStrategy(new TypeScriptReflectionHost(checker), logicalFs); |
| 232 | + const decl = getDeclaration(program, _('/index.ts'), 'Foo', ts.isClassDeclaration); |
| 233 | + const context = program.getSourceFile(_('/context.ts'))!; |
| 234 | + const emitted = strategy.emit(new Reference(decl), context); |
| 235 | + if (emitted === null) { |
| 236 | + return fail('Reference should be emitted'); |
| 237 | + } |
| 238 | + if (!(emitted.expression instanceof ExternalExpr)) { |
| 239 | + return fail('Reference should be emitted as ExternalExpr'); |
| 240 | + } |
| 241 | + expect(emitted.expression.value.name).toEqual('Foo'); |
| 242 | + expect(emitted.expression.value.moduleName).toEqual('./index'); |
| 243 | + }); |
| 244 | + }); |
| 245 | + |
| 246 | + describe('RelativePathStrategy', () => { |
| 247 | + it('should prefer non-aliased exports', () => { |
| 248 | + const {program} = makeProgram([ |
| 249 | + { |
| 250 | + name: _('/index.ts'), |
| 251 | + contents: ` |
| 252 | + declare class Foo {} |
| 253 | + export {Foo as A}; |
| 254 | + export {Foo}; |
| 255 | + export {Foo as B}; |
| 256 | + `, |
| 257 | + }, |
| 258 | + { |
| 259 | + name: _('/context.ts'), |
| 260 | + contents: 'export class Context {}', |
| 261 | + } |
| 262 | + ]); |
| 263 | + const checker = program.getTypeChecker(); |
| 264 | + const strategy = new RelativePathStrategy(new TypeScriptReflectionHost(checker)); |
| 265 | + const decl = getDeclaration(program, _('/index.ts'), 'Foo', ts.isClassDeclaration); |
| 266 | + const context = program.getSourceFile(_('/context.ts'))!; |
| 267 | + const emitted = strategy.emit(new Reference(decl), context); |
| 268 | + if (emitted === null) { |
| 269 | + return fail('Reference should be emitted'); |
| 270 | + } |
| 271 | + if (!(emitted.expression instanceof ExternalExpr)) { |
| 272 | + return fail('Reference should be emitted as ExternalExpr'); |
| 273 | + } |
| 274 | + expect(emitted.expression.value.name).toEqual('Foo'); |
| 275 | + expect(emitted.expression.value.moduleName).toEqual('./index'); |
| 276 | + }); |
| 277 | + }); |
| 278 | + |
| 279 | + describe('UnifiedModulesStrategy', () => { |
| 280 | + it('should prefer non-aliased exports', () => { |
| 281 | + const {program} = makeProgram([ |
| 282 | + { |
| 283 | + name: _('/index.ts'), |
| 284 | + contents: ` |
| 285 | + declare class Foo {} |
| 286 | + export {Foo as A}; |
| 287 | + export {Foo}; |
| 288 | + export {Foo as B}; |
| 289 | + `, |
| 290 | + }, |
| 291 | + { |
| 292 | + name: _('/context.ts'), |
| 293 | + contents: 'export class Context {}', |
| 294 | + } |
| 295 | + ]); |
| 296 | + const checker = program.getTypeChecker(); |
| 297 | + const host: UnifiedModulesHost = { |
| 298 | + fileNameToModuleName(importedFilePath): string { |
| 299 | + return basename(importedFilePath, '.ts'); |
| 300 | + } |
| 301 | + }; |
| 302 | + const strategy = new UnifiedModulesStrategy(new TypeScriptReflectionHost(checker), host); |
| 303 | + const decl = getDeclaration(program, _('/index.ts'), 'Foo', ts.isClassDeclaration); |
| 304 | + const context = program.getSourceFile(_('/context.ts'))!; |
| 305 | + const emitted = strategy.emit(new Reference(decl), context); |
| 306 | + if (emitted === null) { |
| 307 | + return fail('Reference should be emitted'); |
| 308 | + } |
| 309 | + if (!(emitted.expression instanceof ExternalExpr)) { |
| 310 | + return fail('Reference should be emitted as ExternalExpr'); |
| 311 | + } |
| 312 | + expect(emitted.expression.value.name).toEqual('Foo'); |
| 313 | + expect(emitted.expression.value.moduleName).toEqual('index'); |
| 314 | + }); |
176 | 315 | });
|
177 | 316 | });
|
0 commit comments