Skip to content

Commit

Permalink
[BUGFIX release] Improve class based tranform deprecation message
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode committed May 25, 2021
1 parent 802476f commit e28a201
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
30 changes: 18 additions & 12 deletions packages/ember-template-compiler/lib/system/compile-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,16 @@ interface LegacyPlugin {

export type LegacyPluginClass = new (env: ASTPluginEnvironment) => LegacyPlugin;

function wrapLegacyPluginIfNeeded(_plugin: PluginFunc | LegacyPluginClass): PluginFunc {
let plugin = _plugin;
if (_plugin.prototype && _plugin.prototype.transform) {
function isLegacyPluginClass(plugin: PluginFunc | LegacyPluginClass): plugin is LegacyPluginClass {
return plugin.prototype && typeof plugin.prototype.transform === 'function';
}

function wrapLegacyPluginIfNeeded(plugin: PluginFunc | LegacyPluginClass): PluginFunc {
if (isLegacyPluginClass(plugin)) {
const Plugin = plugin;

deprecate(
'Using class based template compilation plugins is deprecated, please update to the functional style',
`Using class based template compilation plugins is deprecated, please update to the functional style: ${Plugin.name}`,
false,
{
id: 'template-compiler.registerPlugin',
Expand All @@ -102,28 +107,29 @@ function wrapLegacyPluginIfNeeded(_plugin: PluginFunc | LegacyPluginClass): Plug
let pluginInstantiated = false;

return {
name: _plugin.constructor && _plugin.constructor.name,
name: plugin.name,

visitor: {
Program(node: AST.Program): AST.Node | void {
if (!pluginInstantiated) {
pluginInstantiated = true;
let plugin = new (_plugin as LegacyPluginClass)(env);
let instance = new Plugin(env);

plugin.syntax = env.syntax;
instance.syntax = env.syntax;

return plugin.transform(node);
return instance.transform(node);
}
},
},
};
};

pluginFunc.__raw = _plugin as LegacyPluginClass;
plugin = pluginFunc;
}
pluginFunc.__raw = Plugin;

return plugin as PluginFunc;
return pluginFunc;
} else {
return plugin;
}
}

export function registerPlugin(type: string, _plugin: PluginFunc | LegacyPluginClass): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ moduleFor(
class extends CustomPluginsTests {
beforeEach() {
expectDeprecation(
'Using class based template compilation plugins is deprecated, please update to the functional style'
`Using class based template compilation plugins is deprecated, please update to the functional style: ${LegacyCustomTransform.name}`
);
expectDeprecation(
'registerPlugin is deprecated, please pass plugins directly via `compile` and/or `precompile`.'
Expand All @@ -141,7 +141,7 @@ moduleFor(

['@test custom registered plugins are deduplicated'](assert) {
expectDeprecation(
'Using class based template compilation plugins is deprecated, please update to the functional style'
`Using class based template compilation plugins is deprecated, please update to the functional style: ${LegacyCustomTransform.name}`
);
expectDeprecation(
'registerPlugin is deprecated, please pass plugins directly via `compile` and/or `precompile`.'
Expand Down Expand Up @@ -194,7 +194,7 @@ moduleFor(
// override so that we can provide custom AST plugins to compile
compile(templateString) {
expectDeprecation(
'Using class based template compilation plugins is deprecated, please update to the functional style'
'Using class based template compilation plugins is deprecated, please update to the functional style: LegacyCustomTransform'
);
return compile(templateString, {
plugins: {
Expand Down

0 comments on commit e28a201

Please sign in to comment.