Skip to content

Commit

Permalink
[BUGFIX release] Improve class based tranform deprecation message
Browse files Browse the repository at this point in the history
(cherry picked from commit e28a201)
  • Loading branch information
chancancode committed May 27, 2021
1 parent 7959115 commit e72e6e1
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 @@ -78,11 +78,16 @@ interface LegacyPlugin {
}
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 @@ -98,28 +103,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 e72e6e1

Please sign in to comment.