Skip to content

Commit

Permalink
Merge pull request #17251 from rwjblue/remove-debug-with-assertion
Browse files Browse the repository at this point in the history
[BUGFIX beta] Prevent errors with debug compiled templates in prod.
  • Loading branch information
rwjblue authored Dec 5, 2018
2 parents 03c231f + 7341f91 commit daad932
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { DEBUG } from '@glimmer/env';
import { Opaque } from '@glimmer/interfaces';
import { Tag, VersionedPathReference } from '@glimmer/reference';
import { Arguments, Helper, VM } from '@glimmer/runtime';
import { Maybe } from '@glimmer/util';
import { Arguments, VM } from '@glimmer/runtime';

let helper: Maybe<Helper> = undefined;
class ComponentAssertionReference implements VersionedPathReference<Opaque> {
public tag: Tag;

if (DEBUG) {
class ComponentAssertionReference implements VersionedPathReference<Opaque> {
public tag: Tag;

constructor(private component: VersionedPathReference<Opaque>, private message: string) {
this.tag = component.tag;
}

value(): Opaque {
let value = this.component.value();
constructor(private component: VersionedPathReference<Opaque>, private message: string) {
this.tag = component.tag;
}

if (typeof value === 'string') {
throw new TypeError(this.message);
}
value(): Opaque {
let value = this.component.value();

return value;
if (typeof value === 'string') {
throw new TypeError(this.message);
}

get(property: string): VersionedPathReference<Opaque> {
return this.component.get(property);
}
return value;
}

helper = (_vm: VM, args: Arguments) =>
new ComponentAssertionReference(args.positional.at(0), args.positional.at(1).value() as string);
get(property: string): VersionedPathReference<Opaque> {
return this.component.get(property);
}
}

export default helper;
export default (_vm: VM, args: Arguments) => {
if (DEBUG) {
return new ComponentAssertionReference(args.positional.at(0), args.positional
.at(1)
.value() as string);
} else {
return args.positional.at(0);
}
};
6 changes: 1 addition & 5 deletions packages/@ember/-internals/glimmer/lib/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '@ember/canary-features';
import { assert } from '@ember/debug';
import { _instrumentStart } from '@ember/instrumentation';
import { DEBUG } from '@glimmer/env';
import {
ComponentDefinition,
Opaque,
Expand Down Expand Up @@ -82,12 +81,9 @@ const BUILTINS_HELPERS = {
'-get-dynamic-var': getDynamicVar,
'-mount': mountHelper,
'-outlet': outletHelper,
'-assert-implicit-component-helper-argument': componentAssertionHelper,
};

if (DEBUG) {
BUILTINS_HELPERS['-assert-implicit-component-helper-argument'] = componentAssertionHelper;
}

const BUILTIN_MODIFIERS = {
action: { manager: new ActionModifierManager(), state: null },
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DEBUG } from '@glimmer/env';
import { AST, ASTPlugin, ASTPluginEnvironment } from '@glimmer/syntax';
import calculateLocationDisplay from '../system/calculate-location-display';
import { Builders } from '../types';
Expand Down Expand Up @@ -205,31 +204,21 @@ function isBlockInvocation(node: AST.BlockStatement, locals: string[][]): boolea
return isIllegalName(node.path, locals);
}

let wrapInAssertion: (
moduleName: string,
node: AST.PathExpression,
builder: Builders
) => AST.Expression;

if (DEBUG) {
wrapInAssertion = (moduleName, node, b) => {
let error = b.string(
`expected \`${
node.original
}\` to be a contextual component but found a string. Did you mean \`(component ${
node.original
})\`? ${calculateLocationDisplay(moduleName, node.loc)}`
);

return b.sexpr(
b.path('-assert-implicit-component-helper-argument'),
[node, error],
b.hash(),
node.loc
);
};
} else {
wrapInAssertion = (_, node) => node;
function wrapInAssertion(moduleName: string, node: AST.PathExpression, b: Builders) {
let error = b.string(
`expected \`${
node.original
}\` to be a contextual component but found a string. Did you mean \`(component ${
node.original
})\`? ${calculateLocationDisplay(moduleName, node.loc)}`
);

return b.sexpr(
b.path('-assert-implicit-component-helper-argument'),
[node, error],
b.hash(),
node.loc
);
}

function wrapInComponent(
Expand Down

0 comments on commit daad932

Please sign in to comment.