Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX release] Improve class based tranform deprecation message #19555

Merged
merged 1 commit into from May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions packages/ember-template-compiler/lib/system/compile-options.ts
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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this was a bug previously: if plugin is a class, then plugin.constructor would have been Function, so this would always be the string "Function".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Hopefully the name wasn't used to deduplicate plugins!)


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
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