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

[BUGFIX beta] Disallow null/undefined targets in in-element #19021

Merged
merged 1 commit into from
Jun 19, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import { Helper, VMArguments } from '@glimmer/interfaces';
import { VersionedPathReference } from '@glimmer/reference';
import { Tag } from '@glimmer/validator';

let helper: Helper;

if (DEBUG) {
class InElementNullCheckReference implements VersionedPathReference {
public tag: Tag;

constructor(private inner: VersionedPathReference) {
this.tag = inner.tag;
}

value(): unknown {
let value = this.inner.value();

assert(
'You cannot pass a null or undefined destination element to in-element',
value !== null && value !== undefined
);

return value;
}

get(key: string): VersionedPathReference {
return this.inner.get(key);
}
}

helper = (args: VMArguments) => new InElementNullCheckReference(args.positional.at(0));
} else {
helper = (args: VMArguments) => args.positional.at(0);
}

export default helper;
2 changes: 2 additions & 0 deletions packages/@ember/-internals/glimmer/lib/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import InternalComponentManager, {
import { TemplateOnlyComponentDefinition } from './component-managers/template-only';
import { isClassHelper, isHelperFactory } from './helper';
import { default as componentAssertionHelper } from './helpers/-assert-implicit-component-helper-argument';
import { default as inElementNullCheckHelper } from './helpers/-in-element-null-check';
import { default as normalizeClassHelper } from './helpers/-normalize-class';
import { default as trackArray } from './helpers/-track-array';
import { default as action } from './helpers/action';
Expand Down Expand Up @@ -262,6 +263,7 @@ const BUILTINS_HELPERS: IBuiltInHelpers = {
'-mount': mountHelper,
'-outlet': outletHelper,
'-assert-implicit-component-helper-argument': componentAssertionHelper,
'-in-el-null': inElementNullCheckHelper,
};

interface IBuiltInModifiers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,42 @@ moduleFor(
}, /Can only pass null to insertBefore in in-element, received:/);
}

['@feature(EMBER_GLIMMER_IN_ELEMENT) does not allow null as a destination element']() {
let someElement = null;

expectAssertion(() => {
this.render(
strip`
{{#in-element someElement}}
{{text}}
{{/in-element}}
`,
{
someElement,
text: 'Whoop!',
}
);
}, /You cannot pass a null or undefined destination element to in-element/);
}

['@feature(EMBER_GLIMMER_IN_ELEMENT) does not undefined as a destination element']() {
let someElement = undefined;

expectAssertion(() => {
this.render(
strip`
{{#in-element someElement}}
{{text}}
{{/in-element}}
`,
{
someElement,
text: 'Whoop!',
}
);
}, /You cannot pass a null or undefined destination element to in-element/);
}

['@feature(EMBER_GLIMMER_IN_ELEMENT) components are cleaned up properly'](assert) {
let hooks = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export default function transformInElement(env: ASTPluginEnvironment): ASTPlugin

if (node.path.original === 'in-element') {
if (EMBER_GLIMMER_IN_ELEMENT) {
let originalValue = node.params[0];

if (originalValue) {
let subExpr = b.sexpr('-in-el-null', [originalValue]);

node.params.shift();
node.params.unshift(subExpr);
}

node.hash.pairs.forEach(pair => {
if (pair.key === 'insertBefore') {
assert(
Expand Down