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

[RFC#236] Deprecate String prototype extensions #19234

Merged
merged 4 commits into from
Nov 11, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions packages/@ember/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { getStrings as _getStrings, setStrings as _setStrings } from './lib/stri

import { ENV } from '@ember/-internals/environment';
import { Cache } from '@ember/-internals/utils';
import { deprecate } from '@ember/debug';
import { getString } from './lib/string_registry';

const STRING_DASHERIZE_REGEXP = /[ _]/g;
Expand Down Expand Up @@ -279,6 +280,26 @@ export function capitalize(str: string): string {
}

if (ENV.EXTEND_PROTOTYPES.String) {
let deprecateEmberStringPrototypeExtension = function (
name: string,
fn: (utility: string, ...options: any) => string | string[],
message = `String prototype extensions are deprecated. Please import ${name} from '@ember/string' instead.`
) {
return function (this: string) {
deprecate(message, false, {
id: 'ember-string.prototype-extensions',
for: '@ember/string',
locks marked this conversation as resolved.
Show resolved Hide resolved
since: {
available: '3.24',
},
until: '4.0.0',
url: 'https://deprecations.emberjs.com/v3.x/#toc_ember-string-prototype_extensions',
});

return fn(this, ...arguments);
};
};

Object.defineProperties(String.prototype, {
/**
See [String.w](/ember/release/classes/String/methods/w?anchor=w).
Expand All @@ -292,9 +313,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
configurable: true,
enumerable: false,
writeable: true,
value() {
return w(this);
},
value: deprecateEmberStringPrototypeExtension('w', w),
},

/**
Expand Down Expand Up @@ -326,9 +345,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
configurable: true,
enumerable: false,
writeable: true,
value() {
return camelize(this);
},
value: deprecateEmberStringPrototypeExtension('camelize', camelize),
},

/**
Expand All @@ -343,9 +360,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
configurable: true,
enumerable: false,
writeable: true,
value() {
return decamelize(this);
},
value: deprecateEmberStringPrototypeExtension('decamelize', decamelize),
},

/**
Expand All @@ -360,9 +375,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
configurable: true,
enumerable: false,
writeable: true,
value() {
return dasherize(this);
},
value: deprecateEmberStringPrototypeExtension('dasherize', dasherize),
},

/**
Expand All @@ -377,9 +390,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
configurable: true,
enumerable: false,
writeable: true,
value() {
return underscore(this);
},
value: deprecateEmberStringPrototypeExtension('underscore', underscore),
},

/**
Expand All @@ -394,9 +405,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
configurable: true,
enumerable: false,
writeable: true,
value() {
return classify(this);
},
value: deprecateEmberStringPrototypeExtension('classify', classify),
},

/**
Expand All @@ -411,9 +420,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
configurable: true,
enumerable: false,
writeable: true,
value() {
return capitalize(this);
},
value: deprecateEmberStringPrototypeExtension('capitalize', capitalize),
},
});
}