fix(compiler): handle type references to namespaced symbols correctly (#36106)
When the compiler needs to convert a type reference to a value
expression, it may encounter a type that refers to a namespaced symbol.
Such namespaces need to be handled specially as there's various forms
available. Consider a namespace named "ns":
1. One can refer to a namespace by itself: `ns`. A namespace is only
allowed to be used in a type position if it has been merged with a
class, but even if this is the case it may not be possible to convert
that type into a value expression depending on the import form. More
on this later (case a below)
2. One can refer to a type within the namespace: `ns.Foo`. An import
needs to be generated to `ns`, from which the `Foo` property can then
be read.
3. One can refer to a type in a nested namespace within `ns`:
`ns.Foo.Bar` and possibly even deeper nested. The value
representation is similar to case 2, but includes additional property
accesses.
The exact strategy of how to deal with these cases depends on the type
of import used. There's two flavors available:
a. A namespaced import like `import * as ns from 'ns';` that creates
a local namespace that is irrelevant to the import that needs to be
generated (as said import would be used instead of the original
import).
If the local namespace "ns" itself is referred to in a type position,
it is invalid to convert it into a value expression. Some JavaScript
libraries publish a value as default export using `export = MyClass;`
syntax, however it is illegal to refer to that value using "ns".
Consequently, such usage in a type position *must* be accompanied by
an `@Inject` decorator to provide an explicit token.
b. An explicit namespace declaration within a module, that can be
imported using a named import like `import {ns} from 'ns';` where the
"ns" module declares a namespace using `declare namespace ns {}`.
In this case, it's the namespace itself that needs to be imported,
after which any qualified references into the namespace are converted
into property accesses.
Before this change, support for namespaces in the type-to-value
conversion was limited and only worked correctly for a single qualified
name using a namespace import (case 2a). All other cases were either
producing incorrect code or would crash the compiler (case 1a).
Crashing the compiler is not desirable as it does not indicate where
the issue is. Moreover, the result of a type-to-value conversion is
irrelevant when an explicit injection token is provided using `@Inject`,
so referring to a namespace in a type position (case 1) could still be
valid.
This commit introduces logic to the type-to-value conversion to be able
to properly deal with all type references to namespaced symbols.
Fixes #36006
Resolves FW-1995
PR Close #36106
This commit is contained in:
@@ -3880,6 +3880,140 @@ runInEachFileSystem(os => {
|
||||
expect(jsContents).toMatch(setClassMetadataRegExp('type: i1.Other'));
|
||||
});
|
||||
|
||||
describe('namespace support', () => {
|
||||
it('should generate correct imports for type references to namespaced symbols using a namespace import',
|
||||
() => {
|
||||
env.write(`/node_modules/ns/index.d.ts`, `
|
||||
export declare class Zero {}
|
||||
export declare namespace one {
|
||||
export declare class One {}
|
||||
}
|
||||
export declare namespace one.two {
|
||||
export declare class Two {}
|
||||
}
|
||||
`);
|
||||
env.write(`test.ts`, `
|
||||
import {Inject, Injectable, InjectionToken} from '@angular/core';
|
||||
import * as ns from 'ns';
|
||||
|
||||
@Injectable()
|
||||
export class MyService {
|
||||
constructor(
|
||||
zero: ns.Zero,
|
||||
one: ns.one.One,
|
||||
two: ns.one.two.Two,
|
||||
) {}
|
||||
}
|
||||
`);
|
||||
|
||||
env.driveMain();
|
||||
const jsContents = trim(env.getContents('test.js'));
|
||||
expect(jsContents).toContain(`import * as i1 from "ns";`);
|
||||
expect(jsContents).toContain('i0.ɵɵinject(i1.Zero)');
|
||||
expect(jsContents).toContain('i0.ɵɵinject(i1.one.One)');
|
||||
expect(jsContents).toContain('i0.ɵɵinject(i1.one.two.Two)');
|
||||
expect(jsContents).toMatch(setClassMetadataRegExp('type: i1.Zero'));
|
||||
expect(jsContents).toMatch(setClassMetadataRegExp('type: i1.one.One'));
|
||||
expect(jsContents).toMatch(setClassMetadataRegExp('type: i1.one.two.Two'));
|
||||
});
|
||||
|
||||
it('should generate correct imports for type references to namespaced symbols using named imports',
|
||||
() => {
|
||||
env.write(`/node_modules/ns/index.d.ts`, `
|
||||
export namespace ns {
|
||||
export declare class Zero {}
|
||||
export declare namespace one {
|
||||
export declare class One {}
|
||||
}
|
||||
export declare namespace one.two {
|
||||
export declare class Two {}
|
||||
}
|
||||
}
|
||||
`);
|
||||
env.write(`test.ts`, `
|
||||
import {Inject, Injectable, InjectionToken} from '@angular/core';
|
||||
import {ns} from 'ns';
|
||||
import {ns as alias} from 'ns';
|
||||
|
||||
@Injectable()
|
||||
export class MyService {
|
||||
constructor(
|
||||
zero: ns.Zero,
|
||||
one: ns.one.One,
|
||||
two: ns.one.two.Two,
|
||||
aliasedZero: alias.Zero,
|
||||
aliasedOne: alias.one.One,
|
||||
aliasedTwo: alias.one.two.Two,
|
||||
) {}
|
||||
}
|
||||
`);
|
||||
|
||||
env.driveMain();
|
||||
const jsContents = trim(env.getContents('test.js'));
|
||||
expect(jsContents).toContain(`import * as i1 from "ns";`);
|
||||
expect(jsContents)
|
||||
.toContain(
|
||||
'i0.ɵɵinject(i1.ns.Zero), ' +
|
||||
'i0.ɵɵinject(i1.ns.one.One), ' +
|
||||
'i0.ɵɵinject(i1.ns.one.two.Two), ' +
|
||||
'i0.ɵɵinject(i1.ns.Zero), ' +
|
||||
'i0.ɵɵinject(i1.ns.one.One), ' +
|
||||
'i0.ɵɵinject(i1.ns.one.two.Two)');
|
||||
expect(jsContents).toMatch(setClassMetadataRegExp('type: i1.ns.Zero'));
|
||||
expect(jsContents).toMatch(setClassMetadataRegExp('type: i1.ns.one.One'));
|
||||
expect(jsContents).toMatch(setClassMetadataRegExp('type: i1.ns.one.two.Two'));
|
||||
});
|
||||
|
||||
it('should not error for a namespace import as parameter type when @Inject is used', () => {
|
||||
env.tsconfig({'strictInjectionParameters': true});
|
||||
env.write(`/node_modules/foo/index.d.ts`, `
|
||||
export = Foo;
|
||||
declare class Foo {}
|
||||
declare namespace Foo {}
|
||||
`);
|
||||
env.write(`test.ts`, `
|
||||
import {Inject, Injectable, InjectionToken} from '@angular/core';
|
||||
import * as Foo from 'foo';
|
||||
|
||||
export const TOKEN = new InjectionToken<Foo>('Foo');
|
||||
|
||||
@Injectable()
|
||||
export class MyService {
|
||||
constructor(@Inject(TOKEN) foo: Foo) {}
|
||||
}
|
||||
`);
|
||||
|
||||
env.driveMain();
|
||||
const jsContents = trim(env.getContents('test.js'));
|
||||
expect(jsContents).toContain('i0.ɵɵinject(TOKEN)');
|
||||
expect(jsContents).toMatch(setClassMetadataRegExp('type: undefined'));
|
||||
});
|
||||
|
||||
it('should error for a namespace import as parameter type used for DI', () => {
|
||||
env.tsconfig({'strictInjectionParameters': true});
|
||||
env.write(`/node_modules/foo/index.d.ts`, `
|
||||
export = Foo;
|
||||
declare class Foo {}
|
||||
declare namespace Foo {}
|
||||
`);
|
||||
env.write(`test.ts`, `
|
||||
import {Injectable} from '@angular/core';
|
||||
import * as Foo from 'foo';
|
||||
|
||||
@Injectable()
|
||||
export class MyService {
|
||||
constructor(foo: Foo) {}
|
||||
}
|
||||
`);
|
||||
|
||||
const diags = env.driveDiagnostics();
|
||||
expect(diags.length).toBe(1);
|
||||
expect(diags[0].messageText)
|
||||
.toBe(
|
||||
`No suitable injection token for parameter 'foo' of class 'MyService'.\nFound Foo`);
|
||||
});
|
||||
});
|
||||
|
||||
it('should use `undefined` in setClassMetadata if types can\'t be represented as values',
|
||||
() => {
|
||||
env.write(`types.ts`, `
|
||||
|
||||
Reference in New Issue
Block a user