Skip to content

Commit

Permalink
fix(compiler): fix static analysis for functional and vdom text
Browse files Browse the repository at this point in the history
fixes #1903
  • Loading branch information
manucorporat committed Oct 4, 2019
1 parent 0bf3877 commit 0901698
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/compiler/transformers/static-to-meta/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ export const gatherVdomMeta = (m: d.Module | d.ComponentCompilerMeta, args: ts.N

// Parse vdom tag
const hTag = args[0];
if (ts.isIdentifier(hTag)) {
if (hTag.text !== 'Host') {
m.hasVdomFunctional = true;
}
if (!ts.isStringLiteral(hTag) && (!ts.isIdentifier(hTag) || hTag.text !== 'Host')) {
m.hasVdomFunctional = true;
}

// Parse attributes
Expand Down Expand Up @@ -65,7 +63,8 @@ export const gatherVdomMeta = (m: d.Module | d.ComponentCompilerMeta, args: ts.N
// Parse children
if (!m.hasVdomText) {
for (let i = 2; i < args.length; i++) {
if (ts.isStringLiteral(args[i]) || ts.isIdentifier(args[i])) {
const arg = args[i];
if (!ts.isCallExpression(arg) || !ts.isIdentifier(arg.expression) || (arg.expression.text !== 'h')) {
m.hasVdomText = true;
break;
}
Expand Down
14 changes: 14 additions & 0 deletions src/compiler/transformers/test/parse-vdom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ describe('parse vdom', () => {
expect(t.cmp.hasVdomFunctional).toBe(true);
});

it('hasVdomFunctional (2)', () => {
const t = transpileModule(`
@Component({tag: 'cmp-a'})
export class CmpA {
render() {
return <Tunnel.Provider/>
}
}
`);

expect(t.cmp.hasVdomFunctional).toBe(true);
});


it('hasVdomKey', () => {
const t = transpileModule(`
@Component({tag: 'cmp-a'})
Expand Down
73 changes: 72 additions & 1 deletion src/runtime/test/render-vdom.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('render-vdom', () => {
});
});

it('dynamic vdomText', async () => {
it('vdomText from identifier', async () => {
@Component({ tag: 'cmp-a'})
class CmpA {
render() {
Expand All @@ -50,6 +50,52 @@ describe('render-vdom', () => {
});
});

it('vdomText from call expression', async () => {
@Component({ tag: 'cmp-a'})
class CmpA {
render() {
const text = () => 'Hello VDOM';
return <div>{text()}</div>;
}
}

const {build} = await newSpecPage({components: [CmpA], strictBuild: true});
expect(build).toMatchObject({
vdomAttribute: false,
vdomXlink: false,
vdomClass: false,
vdomStyle: false,
vdomKey: false,
vdomRef: false,
vdomListener: false,
vdomFunctional: false,
vdomText: true,
});
});

it('vdomText from object access', async () => {
@Component({ tag: 'cmp-a'})
class CmpA {
render() {
const text = {text: 'Hello VDOM'};
return <div>{text.text}</div>;
}
}

const {build} = await newSpecPage({components: [CmpA], strictBuild: true});
expect(build).toMatchObject({
vdomAttribute: false,
vdomXlink: false,
vdomClass: false,
vdomStyle: false,
vdomKey: false,
vdomRef: false,
vdomListener: false,
vdomFunctional: false,
vdomText: true,
});
});

it('vdomClass', async () => {
@Component({ tag: 'cmp-a'})
class CmpA {
Expand Down Expand Up @@ -204,6 +250,31 @@ describe('render-vdom', () => {
});
});

it('vdomFunctional (2)', async () => {
@Component({ tag: 'cmp-a'})
class CmpA {
render() {
const Tunnel = {
Provider: () => { return; }
};
return <Tunnel.Provider />;
}
}

const {build} = await newSpecPage({components: [CmpA], strictBuild: true});
expect(build).toMatchObject({
vdomAttribute: false,
vdomXlink: false,
vdomClass: false,
vdomStyle: false,
vdomKey: false,
vdomRef: false,
vdomListener: false,
vdomFunctional: true,
vdomText: false,
});
});

it('fallback spread', async () => {
@Component({ tag: 'cmp-a'})
class CmpA {
Expand Down

0 comments on commit 0901698

Please sign in to comment.