Skip to content

Commit

Permalink
FEATURE in-element] Add EMBER_GLIMMER_IN_ELEMENT feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
simonihmig committed Apr 11, 2020
1 parent 48112f9 commit ed492bf
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { moduleFor, RenderingTestCase, strip, equalTokens, runTask } from 'internal-test-helpers';

import { Component } from '@ember/-internals/glimmer';
import { set } from '@ember/-internals/metal';
import { EMBER_GLIMMER_IN_ELEMENT } from '@ember/canary-features';

const deprecationMessage = /The use of the private `{{-in-element}}` is deprecated, please refactor to the public `{{in-element}}`/;

moduleFor(
'{{-in-element}}',
class extends RenderingTestCase {
['@test allows rendering into an external element']() {
expectDeprecation(deprecationMessage);
if (EMBER_GLIMMER_IN_ELEMENT) {
expectDeprecation(deprecationMessage);
}

let someElement = document.createElement('div');

Expand Down Expand Up @@ -42,7 +44,9 @@ moduleFor(
}

['@test it appends to the external element by default']() {
expectDeprecation(deprecationMessage);
if (EMBER_GLIMMER_IN_ELEMENT) {
expectDeprecation(deprecationMessage);
}

let someElement = document.createElement('div');
someElement.appendChild(document.createTextNode('foo '));
Expand Down Expand Up @@ -76,7 +80,9 @@ moduleFor(
}

['@test allows appending to the external element with insertBefore=null']() {
expectDeprecation(deprecationMessage);
if (EMBER_GLIMMER_IN_ELEMENT) {
expectDeprecation(deprecationMessage);
}

let someElement = document.createElement('div');
someElement.appendChild(document.createTextNode('foo '));
Expand Down Expand Up @@ -110,7 +116,9 @@ moduleFor(
}

['@test allows clearing the external element with insertBefore=undefined']() {
expectDeprecation(deprecationMessage);
if (EMBER_GLIMMER_IN_ELEMENT) {
expectDeprecation(deprecationMessage);
}

let someElement = document.createElement('div');
someElement.appendChild(document.createTextNode('foo '));
Expand Down Expand Up @@ -144,7 +152,9 @@ moduleFor(
}

['@test does not allow insertBefore=non-null-value']() {
expectDeprecation(deprecationMessage);
if (EMBER_GLIMMER_IN_ELEMENT) {
expectDeprecation(deprecationMessage);
}

let someElement = document.createElement('div');

Expand All @@ -164,7 +174,9 @@ moduleFor(
}

['@test components are cleaned up properly'](assert) {
expectDeprecation(deprecationMessage);
if (EMBER_GLIMMER_IN_ELEMENT) {
expectDeprecation(deprecationMessage);
}

let hooks = [];

Expand Down Expand Up @@ -234,7 +246,9 @@ moduleFor(
}

['@test appending to the root element should not cause double clearing']() {
expectDeprecation(deprecationMessage);
if (EMBER_GLIMMER_IN_ELEMENT) {
expectDeprecation(deprecationMessage);
}

this.render(
strip`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { set } from '@ember/-internals/metal';
moduleFor(
'{{in-element}}',
class extends RenderingTestCase {
['@test allows rendering into an external element']() {
['@feature(EMBER_GLIMMER_IN_ELEMENT) allows rendering into an external element']() {
let someElement = document.createElement('div');

this.render(
Expand Down Expand Up @@ -37,7 +37,7 @@ moduleFor(
equalTokens(someElement, 'Whoop!');
}

["@test it replaces the external element's content by default"]() {
["@feature(EMBER_GLIMMER_IN_ELEMENT) it replaces the external element's content by default"]() {
let someElement = document.createElement('div');
someElement.appendChild(document.createTextNode('foo '));

Expand Down Expand Up @@ -69,7 +69,7 @@ moduleFor(
equalTokens(someElement, 'bar');
}

['@test allows appending to the external element with insertBefore=null']() {
['@feature(EMBER_GLIMMER_IN_ELEMENT) allows appending to the external element with insertBefore=null']() {
let someElement = document.createElement('div');
someElement.appendChild(document.createTextNode('foo '));

Expand Down Expand Up @@ -101,7 +101,7 @@ moduleFor(
equalTokens(someElement, 'foo bar');
}

['@test does not allow insertBefore=non-null-value']() {
['@feature(EMBER_GLIMMER_IN_ELEMENT) does not allow insertBefore=non-null-value']() {
let someElement = document.createElement('div');

expectAssertion(() => {
Expand All @@ -119,7 +119,7 @@ moduleFor(
}, /Can only pass null to insertBefore in in-element, received:/);
}

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

let someElement = document.createElement('div');
Expand Down Expand Up @@ -187,7 +187,7 @@ moduleFor(
assert.deepEqual(hooks, ['didInsertElement', 'willDestroyElement']);
}

['@test appending to the root element should not cause double clearing']() {
['@feature(EMBER_GLIMMER_IN_ELEMENT) appending to the root element should not cause double clearing']() {
this.render(
strip`
Before
Expand Down
2 changes: 2 additions & 0 deletions packages/@ember/canary-features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const DEFAULT_FEATURES = {
EMBER_CUSTOM_COMPONENT_ARG_PROXY: true,
EMBER_GLIMMER_SET_COMPONENT_TEMPLATE: true,
EMBER_ROUTING_MODEL_ARG: true,
EMBER_GLIMMER_IN_ELEMENT: null,
};

/**
Expand Down Expand Up @@ -79,3 +80,4 @@ export const EMBER_GLIMMER_SET_COMPONENT_TEMPLATE = featureValue(
FEATURES.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE
);
export const EMBER_ROUTING_MODEL_ARG = featureValue(FEATURES.EMBER_ROUTING_MODEL_ARG);
export const EMBER_GLIMMER_IN_ELEMENT = featureValue(FEATURES.EMBER_GLIMMER_IN_ELEMENT);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { StaticTemplateMeta } from '@ember/-internals/views';
import { EMBER_GLIMMER_IN_ELEMENT } from '@ember/canary-features';
import { assert, deprecate } from '@ember/debug';
import { AST, ASTPlugin, ASTPluginEnvironment } from '@glimmer/syntax';
import calculateLocationDisplay from '../system/calculate-location-display';
Expand Down Expand Up @@ -54,26 +55,32 @@ export default function transformInElement(env: ASTPluginEnvironment): ASTPlugin
if (!isPath(node.path)) return;

if (node.path.original === 'in-element') {
node.hash.pairs.forEach(pair => {
if (pair.key === 'insertBefore') {
assert(
`Can only pass null to insertBefore in in-element, received: ${JSON.stringify(
pair.value
)}`,
pair.value.type === 'NullLiteral' || pair.value.type === 'UndefinedLiteral'
);
}
});
if (EMBER_GLIMMER_IN_ELEMENT) {
node.hash.pairs.forEach(pair => {
if (pair.key === 'insertBefore') {
assert(
`Can only pass null to insertBefore in in-element, received: ${JSON.stringify(
pair.value
)}`,
pair.value.type === 'NullLiteral' || pair.value.type === 'UndefinedLiteral'
);
}
});
} else {
assert(assertMessage(moduleName, node));
}
} else if (node.path.original === '-in-element') {
let sourceInformation = calculateLocationDisplay(moduleName, node.loc);
deprecate(
`The use of the private \`{{-in-element}}\` is deprecated, please refactor to the public \`{{in-element}}\`. ${sourceInformation}`,
false,
{
id: 'glimmer.private-in-element',
until: '4.0.0',
}
);
if (EMBER_GLIMMER_IN_ELEMENT) {
let sourceInformation = calculateLocationDisplay(moduleName, node.loc);
deprecate(
`The use of the private \`{{-in-element}}\` is deprecated, please refactor to the public \`{{in-element}}\`. ${sourceInformation}`,
false,
{
id: 'glimmer.private-in-element',
until: '4.0.0',
}
);
}

node.path.original = 'in-element';
node.path.parts = ['in-element'];
Expand Down Expand Up @@ -110,3 +117,9 @@ export default function transformInElement(env: ASTPluginEnvironment): ASTPlugin
},
};
}

function assertMessage(moduleName: string, node: AST.BlockStatement) {
let sourceInformation = calculateLocationDisplay(moduleName, node.loc);

return `The {{in-element}} helper cannot be used. ${sourceInformation}`;
}

0 comments on commit ed492bf

Please sign in to comment.