Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): address when a home module cannot be found #4521

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/compiler/transformers/test/parse-props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,39 @@ describe('parse props', () => {
expect(t.cmp?.hasProp).toBe(true);
});

it('should correctly parse a prop with an unresolved type', () => {
const t = transpileModule(`
@Component({tag: 'cmp-a'})
export class CmpA {
@Prop() val?: Foo;
}
`);
expect(getStaticGetter(t.outputText, 'properties')).toEqual({
val: {
attribute: 'val',
complexType: {
references: {
Foo: {
id: 'global::Foo',
location: 'global',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So TS thinks any type it can't resolve is just a global? Or is this something we're doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah basically, if a type isn't imported into the module or declared inside of it we can safely assume it's a global type

},
},
resolved: 'Foo',
original: 'Foo',
},
docs: {
text: '',
tags: [],
},
mutable: false,
optional: true,
reflect: false,
required: false,
type: 'any',
},
});
});

it('prop required', () => {
const t = transpileModule(`
@Component({tag: 'cmp-a'})
Expand Down
26 changes: 14 additions & 12 deletions src/compiler/transformers/transform-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,18 +519,20 @@ const getTypeReferenceLocation = (
const compilerHost = ts.createCompilerHost(options);
const importHomeModule = getHomeModule(sourceFile, localImportPath, options, compilerHost, program);

const importName = namedImportBindings.elements.find((nbe) => nbe.name.getText() === typeName).name;
const originalTypeName = getOriginalTypeName(importName, checker);

const typeDecl = findTypeWithName(importHomeModule, originalTypeName);
type = checker.getTypeAtLocation(typeDecl);

const id = addToLibrary(type, originalTypeName, checker, normalizePath(importHomeModule.fileName, false));
return {
location: 'import',
path: localImportPath,
id,
};
if (importHomeModule) {
const importName = namedImportBindings.elements.find((nbe) => nbe.name.getText() === typeName).name;
const originalTypeName = getOriginalTypeName(importName, checker);

const typeDecl = findTypeWithName(importHomeModule, originalTypeName);
type = checker.getTypeAtLocation(typeDecl);

const id = addToLibrary(type, originalTypeName, checker, normalizePath(importHomeModule.fileName, false));
return {
location: 'import',
path: localImportPath,
id,
};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this now return undefined if importHomeModule is falsy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, since there's still the code below which deals with the case that this is a global type and so on

}

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