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

[DEPRECATE] registerPlugin / unregisterPlugin and legacy class based AST plugins #19429

Merged
merged 1 commit into from Feb 25, 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
41 changes: 40 additions & 1 deletion packages/ember-template-compiler/lib/system/compile-options.ts
@@ -1,5 +1,5 @@
import { EMBER_STRICT_MODE } from '@ember/canary-features';
import { assert } from '@ember/debug';
import { assert, deprecate } from '@ember/debug';
import { assign } from '@ember/polyfills';
import { PrecompileOptions } from '@glimmer/compiler';
import { AST, ASTPlugin, ASTPluginEnvironment, Syntax } from '@glimmer/syntax';
Expand Down Expand Up @@ -68,6 +68,19 @@ type LegacyPluginClass = new (env: ASTPluginEnvironment) => LegacyPlugin;
function wrapLegacyPluginIfNeeded(_plugin: PluginFunc | LegacyPluginClass): PluginFunc {
let plugin = _plugin;
if (_plugin.prototype && _plugin.prototype.transform) {
deprecate(
'Using class based template compilation plugins is deprecated, please update to the functional style',
false,
{
id: 'template-compiler.registerPlugin',
until: '4.0.0',
for: 'ember-source',
since: {
enabled: '3.27.0',
},
}
);

const pluginFunc: PluginFunc = (env: ASTPluginEnvironment): ASTPlugin => {
let pluginInstantiated = false;

Expand Down Expand Up @@ -97,6 +110,19 @@ function wrapLegacyPluginIfNeeded(_plugin: PluginFunc | LegacyPluginClass): Plug
}

export function registerPlugin(type: string, _plugin: PluginFunc | LegacyPluginClass): void {
deprecate(
'registerPlugin is deprecated, please pass plugins directly via `compile` and/or `precompile`.',
false,
{
id: 'template-compiler.registerPlugin',
until: '4.0.0',
for: 'ember-source',
since: {
enabled: '3.27.0',
},
}
);

if (type !== 'ast') {
throw new Error(
`Attempting to register ${_plugin} as "${type}" which is not a valid Glimmer plugin type.`
Expand All @@ -116,6 +142,19 @@ export function registerPlugin(type: string, _plugin: PluginFunc | LegacyPluginC
}

export function unregisterPlugin(type: string, PluginClass: PluginFunc | LegacyPluginClass): void {
deprecate(
'unregisterPlugin is deprecated, please pass plugins directly via `compile` and/or `precompile`.',
false,
{
id: 'template-compiler.registerPlugin',
until: '4.0.0',
for: 'ember-source',
since: {
enabled: '3.27.0',
},
}
);

if (type !== 'ast') {
throw new Error(
`Attempting to unregister ${PluginClass} as "${type}" which is not a valid Glimmer plugin type.`
Expand Down
Expand Up @@ -120,19 +120,34 @@ class CustomPluginsTests extends RenderingTestCase {
}

moduleFor(
'ember-template-compiler: registerPlugin with a custom plugins in legacy format',
'ember-template-compiler: [DEPRECATED] registerPlugin with a custom plugins in legacy format',
class extends CustomPluginsTests {
beforeEach() {
expectDeprecation(
'Using class based template compilation plugins is deprecated, please update to the functional style'
);
expectDeprecation(
'registerPlugin is deprecated, please pass plugins directly via `compile` and/or `precompile`.'
);
registerPlugin('ast', LegacyCustomTransform);
}

afterEach() {
unregisterPlugin('ast', LegacyCustomTransform);
expectDeprecation(() => {
unregisterPlugin('ast', LegacyCustomTransform);
}, /unregisterPlugin is deprecated, please pass plugins directly via `compile` and\/or `precompile`/);
return super.afterEach();
}

['@test custom registered plugins are deduplicated'](assert) {
expectDeprecation(
'Using class based template compilation plugins is deprecated, please update to the functional style'
);
expectDeprecation(
'registerPlugin is deprecated, please pass plugins directly via `compile` and/or `precompile`.'
);
registerPlugin('ast', LegacyCustomTransform);

this.registerTemplate(
'application',
'<div data-test="foo" data-blah="derp" class="hahaha"></div>'
Expand All @@ -143,19 +158,27 @@ moduleFor(
);

moduleFor(
'ember-template-compiler: registerPlugin with a custom plugins',
'ember-template-compiler: [DEPRECATED] registerPlugin with a custom plugins',
class extends CustomPluginsTests {
beforeEach() {
registerPlugin('ast', customTransform);
expectDeprecation(() => {
registerPlugin('ast', customTransform);
}, /registerPlugin is deprecated, please pass plugins directly via `compile` and\/or `precompile`/);
}

afterEach() {
unregisterPlugin('ast', customTransform);
expectDeprecation(() => {
unregisterPlugin('ast', customTransform);
}, /unregisterPlugin is deprecated, please pass plugins directly via `compile` and\/or `precompile`/);

return super.afterEach();
}

['@test custom registered plugins are deduplicated'](assert) {
registerPlugin('ast', customTransform);
expectDeprecation(() => {
registerPlugin('ast', customTransform);
}, /registerPlugin is deprecated, please pass plugins directly via `compile` and\/or `precompile`/);

this.registerTemplate(
'application',
'<div data-test="foo" data-blah="derp" class="hahaha"></div>'
Expand All @@ -170,6 +193,9 @@ moduleFor(
class extends RenderingTestCase {
// 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'
);
return compile(templateString, {
plugins: {
ast: [LegacyCustomTransform],
Expand Down