Skip to content

Commit 9a92ad1

Browse files
fix(compiler): add heritage clauses earlier in native transform (#4769)
This fixes an issue that surface in the stencil / ionic-framework nightly tests, where a `super()` call was mistakenly omitted from native components. This was occurring after changing to use the `updateConstructor` helper function in #4741 in the native transform. When that function updates the constructor on a class it inspects the class declaration's heritage clauses to determine if a `super()` call needs to be present in the constructor, and adds one if needed. In the native component transform, however, the transformer to add an `extends HTMLElement` heritage clause and transform the constructor were called separately and then combined into a single updated class declaration using `updateComponentClass`. This meant that when the `updateConstructor` function was called the class declaration node didn't yet have a heritage function, so a `super()` call was not added. This was addressed by a small refactor to the heritage clause insertion function. Instead of creating and returning an updated heritage clause it uses `ts.factory.updateClassDeclaration` to return an updated class declaration node with an appropriate heritage clause added to it. This updated class declaration is then passed to `updateConstructor` and a `super()` call is correctly inserted.
1 parent 9ee02c5 commit 9a92ad1

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

src/compiler/transformers/component-native/native-component.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export const updateNativeComponentClass = (
3535
moduleFile: d.Module,
3636
cmp: d.ComponentCompilerMeta,
3737
): ts.ClassDeclaration | ts.VariableStatement => {
38-
const heritageClauses = updateNativeHostComponentHeritageClauses(classNode, moduleFile);
39-
const members = updateNativeHostComponentMembers(transformOpts, classNode, moduleFile, cmp);
40-
return updateComponentClass(transformOpts, classNode, heritageClauses, members);
38+
const withHeritageClauses = updateNativeHostComponentHeritageClauses(classNode, moduleFile);
39+
const members = updateNativeHostComponentMembers(transformOpts, withHeritageClauses, moduleFile, cmp);
40+
return updateComponentClass(transformOpts, withHeritageClauses, withHeritageClauses.heritageClauses, members);
4141
};
4242

4343
/**
@@ -46,15 +46,15 @@ export const updateNativeComponentClass = (
4646
*
4747
* @param classNode the syntax tree of the Stencil component class to update
4848
* @param moduleFile the Stencil Module associated with the provided class node
49-
* @returns the generated heritage clause
49+
* @returns an updated class declaration with the generated heritage clause
5050
*/
5151
const updateNativeHostComponentHeritageClauses = (
5252
classNode: ts.ClassDeclaration,
5353
moduleFile: d.Module,
54-
): ts.NodeArray<ts.HeritageClause> | [ts.HeritageClause] => {
54+
): ts.ClassDeclaration => {
5555
if (classNode.heritageClauses != null && classNode.heritageClauses.length > 0) {
5656
// the syntax tree has a heritage clause already, don't generate a new one
57-
return classNode.heritageClauses;
57+
return classNode;
5858
}
5959

6060
if (moduleFile.cmps.length >= 1) {
@@ -66,7 +66,14 @@ const updateNativeHostComponentHeritageClauses = (
6666
ts.factory.createExpressionWithTypeArguments(ts.factory.createIdentifier(HTML_ELEMENT), []),
6767
]);
6868

69-
return [heritageClause];
69+
return ts.factory.updateClassDeclaration(
70+
classNode,
71+
classNode.modifiers,
72+
classNode.name,
73+
classNode.typeParameters,
74+
[heritageClause],
75+
classNode.members,
76+
);
7077
};
7178

7279
/**

0 commit comments

Comments
 (0)