Skip to content

Commit

Permalink
Add AST-based deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Camba committed Jun 15, 2018
1 parent 53d8e36 commit 456b62c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
Expand Up @@ -3,7 +3,6 @@ import { RENDER_HELPER } from '@ember/deprecated-features';
import { AST, ASTPlugin, ASTPluginEnvironment } from '@glimmer/syntax';
import calculateLocationDisplay from '../system/calculate-location-display';

// Remove after 3.4 once _ENABLE_RENDER_SUPPORT flag is no longer needed.
export default function deprecateRender(env: ASTPluginEnvironment): ASTPlugin | undefined {
if (RENDER_HELPER) {
let { moduleName } = env.meta;
Expand Down
@@ -0,0 +1,45 @@
import { deprecate } from '@ember/debug';
import { AST, ASTPlugin, ASTPluginEnvironment } from '@glimmer/syntax';
import calculateLocationDisplay from '../system/calculate-location-display';

const EVENTS = [
'insert-newline',
'enter',
'escape-press',
'focus-in',
'focus-out',
'key-press',
'key-up',
'key-down',
];

export default function deprecateRender(env: ASTPluginEnvironment): ASTPlugin | undefined {
let { moduleName } = env.meta;

let deprecationMessage = (node: AST.MustacheStatement, eventName: string, actionName: string) => {
let sourceInformation = calculateLocationDisplay(moduleName, node.loc);
return `Please refactor \`{{input ${eventName}="${actionName}"}}\` to \`{{input ${eventName}=(action "${actionName}")}}\. ${sourceInformation}`;
};

return {
name: 'deprecate-send-action',

visitor: {
MustacheStatement(node: AST.MustacheStatement) {
if (node.path.original !== 'input') {
return;
}

node.hash.pairs.forEach(pair => {
if (EVENTS.indexOf(pair.key) > -1 && pair.value.type === 'StringLiteral') {
deprecate(deprecationMessage(node, pair.key, pair.value.original), false, {
id: 'ember-template-compiler.send-action',
until: '4.0.0',
url: 'SOME_URL',
});
}
});
},
},
};
}
2 changes: 2 additions & 0 deletions packages/ember-template-compiler/lib/plugins/index.ts
Expand Up @@ -4,6 +4,7 @@ import AssertReservedNamedArguments from './assert-reserved-named-arguments';
import AssertSplattributeExpressions from './assert-splattribute-expression';
import DeprecateRender from './deprecate-render';
import DeprecateRenderModel from './deprecate-render-model';
import DeprecateSendAction from './deprecate-send-action';
import TransformActionSyntax from './transform-action-syntax';
import TransformAngleBracketComponents from './transform-angle-bracket-components';
import TransformAttrsIntoArgs from './transform-attrs-into-args';
Expand Down Expand Up @@ -40,6 +41,7 @@ const transforms: Array<APluginFunc> = [
TransformInElement,
AssertIfHelperWithoutArguments,
AssertSplattributeExpressions,
DeprecateSendAction
];

if (RENDER_HELPER) {
Expand Down
@@ -0,0 +1,29 @@
import { compile } from '../../index';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

const EVENTS = [
'insert-newline',
'enter',
'escape-press',
'focus-in',
'focus-out',
'key-press',
'key-up',
'key-down',
];

class DeprecateSendActionTest extends AbstractTestCase {}

EVENTS.forEach(function(e) {
DeprecateSendActionTest.prototype[
`@test Using \`{{input ${e}="actionName"}}\` provides a deprecation`
] = function() {
let expectedMessage = `Please refactor \`{{input ${e}="foo-bar"}}\` to \`{{input ${e}=(action "foo-bar")}}\. ('baz/foo-bar' @ L1:C0) `;

expectDeprecation(() => {
compile(`{{input ${e}="foo-bar"}}`, { moduleName: 'baz/foo-bar' });
}, expectedMessage);
};
});

moduleFor('ember-template-compiler: deprecate-send-action', DeprecateSendActionTest);
2 changes: 1 addition & 1 deletion packages/ember-views/lib/mixins/text_support.js
Expand Up @@ -89,7 +89,7 @@ const KEY_EVENTS = {
+--------------------+----------------+
| new line inserted | insert-newline |
| | |
| enter key pressed | insert-newline |
| enter key pressed | enter |
| | |
| cancel key pressed | escape-press |
| | |
Expand Down

0 comments on commit 456b62c

Please sign in to comment.