Skip to content

Commit 06eaa8f

Browse files
fix(compiler): address when a home module cannot be found (#4521)
1 parent af9639c commit 06eaa8f

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

src/compiler/transformers/test/parse-props.spec.ts

+33
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,39 @@ describe('parse props', () => {
3434
expect(t.cmp?.hasProp).toBe(true);
3535
});
3636

37+
it('should correctly parse a prop with an unresolved type', () => {
38+
const t = transpileModule(`
39+
@Component({tag: 'cmp-a'})
40+
export class CmpA {
41+
@Prop() val?: Foo;
42+
}
43+
`);
44+
expect(getStaticGetter(t.outputText, 'properties')).toEqual({
45+
val: {
46+
attribute: 'val',
47+
complexType: {
48+
references: {
49+
Foo: {
50+
id: 'global::Foo',
51+
location: 'global',
52+
},
53+
},
54+
resolved: 'Foo',
55+
original: 'Foo',
56+
},
57+
docs: {
58+
text: '',
59+
tags: [],
60+
},
61+
mutable: false,
62+
optional: true,
63+
reflect: false,
64+
required: false,
65+
type: 'any',
66+
},
67+
});
68+
});
69+
3770
it('prop required', () => {
3871
const t = transpileModule(`
3972
@Component({tag: 'cmp-a'})

src/compiler/transformers/transform-utils.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,20 @@ const getTypeReferenceLocation = (
519519
const compilerHost = ts.createCompilerHost(options);
520520
const importHomeModule = getHomeModule(sourceFile, localImportPath, options, compilerHost, program);
521521

522-
const importName = namedImportBindings.elements.find((nbe) => nbe.name.getText() === typeName).name;
523-
const originalTypeName = getOriginalTypeName(importName, checker);
524-
525-
const typeDecl = findTypeWithName(importHomeModule, originalTypeName);
526-
type = checker.getTypeAtLocation(typeDecl);
527-
528-
const id = addToLibrary(type, originalTypeName, checker, normalizePath(importHomeModule.fileName, false));
529-
return {
530-
location: 'import',
531-
path: localImportPath,
532-
id,
533-
};
522+
if (importHomeModule) {
523+
const importName = namedImportBindings.elements.find((nbe) => nbe.name.getText() === typeName).name;
524+
const originalTypeName = getOriginalTypeName(importName, checker);
525+
526+
const typeDecl = findTypeWithName(importHomeModule, originalTypeName);
527+
type = checker.getTypeAtLocation(typeDecl);
528+
529+
const id = addToLibrary(type, originalTypeName, checker, normalizePath(importHomeModule.fileName, false));
530+
return {
531+
location: 'import',
532+
path: localImportPath,
533+
id,
534+
};
535+
}
534536
}
535537

536538
// Loop through all top level exports to find if any reference to the type for 'local' reference location

0 commit comments

Comments
 (0)