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 Ember.String/(at)ember/string #18904

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"prettier": "^1.18.2",
"puppeteer": "^1.20.0",
"qunit": "^2.9.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-commonjs": "^9.3.4",
"rollup-plugin-node-resolve": "^4.2.4",
"route-recognizer": "^0.3.4",
"router_js": "^6.2.5",
Expand Down
85 changes: 61 additions & 24 deletions packages/@ember/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { getStrings as _getStrings, setStrings as _setStrings } from './lib/stri
import { ENV } from '@ember/-internals/environment';
import { Cache } from '@ember/-internals/utils';
import { getString } from './lib/string_registry';
import { deprecate } from '@ember/debug';

const STRING_DASHERIZE_REGEXP = /[ _]/g;

Expand Down Expand Up @@ -65,6 +66,22 @@ const DECAMELIZE_CACHE = new Cache<string, string>(1000, str =>
str.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase()
);

export function deprecateEmberStringUtil(name: string, fn: Function, opts = {}) {
return function() {
deprecate(
opts.message ||
`Ember.String namespace is deprecated. Please, use ${name} from '@ember/string' instead.`,
true,
opts.options || {
id: 'ember-string.namespace',
until: '3.5.0',
url: 'https://emberjs.com/deprecations/v2.x/#toc_ember-string-namespace',
}
);
return fn(...arguments);
};
}

/**
Defines string helper methods including string formatting and localization.
Unless `EmberENV.EXTEND_PROTOTYPES.String` is `false` these methods will also be
Expand Down Expand Up @@ -109,6 +126,7 @@ function _fmt(str: string, formats: any[]) {
@param {Array} formats Optional array of parameters to interpolate into string.
@return {String} formatted string
@public
@deprecated
*/
export function loc(str: string, formats: any[]): string {
if (!Array.isArray(formats) || arguments.length > 2) {
Expand Down Expand Up @@ -140,6 +158,7 @@ export function loc(str: string, formats: any[]): string {
@param {String} str The string to split
@return {Array} array containing the split strings
@public
@deprecated
*/
export function w(str: string): string[] {
return str.split(/\s+/);
Expand All @@ -161,6 +180,7 @@ export function w(str: string): string[] {
@param {String} str The string to decamelize.
@return {String} the decamelized string.
@public
@deprecated
*/
export function decamelize(str: string): string {
return DECAMELIZE_CACHE.get(str);
Expand All @@ -183,6 +203,7 @@ export function decamelize(str: string): string {
@param {String} str The string to dasherize.
@return {String} the dasherized string.
@public
@deprecated
*/
export function dasherize(str: string): string {
return STRING_DASHERIZE_CACHE.get(str);
Expand All @@ -206,6 +227,7 @@ export function dasherize(str: string): string {
@param {String} str The string to camelize.
@return {String} the camelized string.
@public
@deprecated
*/
export function camelize(str: string): string {
return CAMELIZE_CACHE.get(str);
Expand All @@ -228,6 +250,7 @@ export function camelize(str: string): string {
@param {String} str the string to classify
@return {String} the classified string
@public
@deprecated
*/
export function classify(str: string): string {
return CLASSIFY_CACHE.get(str);
Expand All @@ -251,6 +274,7 @@ export function classify(str: string): string {
@param {String} str The string to underscore.
@return {String} the underscored string.
@public
@deprecated
*/
export function underscore(str: string): string {
return UNDERSCORE_CACHE.get(str);
Expand All @@ -273,11 +297,28 @@ export function underscore(str: string): string {
@param {String} str The string to capitalize.
@return {String} The capitalized string.
@public
@deprecated
*/
export function capitalize(str: string): string {
return CAPITALIZE_CACHE.get(str);
}

function deprecateEmberStringPrototypeExtension(
name: string,
fn: (utility: string, ...options: any) => string | string[],
message: string = `String prototype extensions are deprecated. Please, us ${name} from '@ember/string' instead.`
) {
return function(this: string) {
deprecate(message, false, {
id: 'ember-string.prototype_extensions',
until: '4.0.0',
url: 'https://emberjs.com/deprecations/v3.x/#toc_ember-string-prototype-extensions',
});

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

if (ENV.EXTEND_PROTOTYPES.String) {
Object.defineProperties(String.prototype, {
/**
Expand All @@ -287,14 +328,13 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for @ember/string
@static
@private
@deprecated
*/
w: {
configurable: true,
enumerable: false,
writeable: true,
value() {
return w(this);
},
value: deprecateEmberStringPrototypeExtension('w', w),
},

/**
Expand All @@ -304,14 +344,17 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for @ember/string
@static
@private
@deprecated
*/
loc: {
configurable: true,
enumerable: false,
writeable: true,
value(this: string, ...args: any[]) {
return loc(this, args);
},
value: deprecateEmberStringPrototypeExtension(
'loc',
loc,
'`loc` is deprecated. Please, use an i18n addon instead. See https://emberobserver.com/categories/internationalization for a list of them.'
),
},

/**
Expand All @@ -321,14 +364,13 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for @ember/string
@static
@private
@deprecated
*/
camelize: {
configurable: true,
enumerable: false,
writeable: true,
value() {
return camelize(this);
},
value: deprecateEmberStringPrototypeExtension('camelize', camelize),
},

/**
Expand All @@ -338,14 +380,13 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for @ember/string
@static
@private
@deprecated
*/
decamelize: {
configurable: true,
enumerable: false,
writeable: true,
value() {
return decamelize(this);
},
value: deprecateEmberStringPrototypeExtension('decamelize', decamelize),
},

/**
Expand All @@ -355,14 +396,13 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for @ember/string
@static
@private
@deprecated
*/
dasherize: {
configurable: true,
enumerable: false,
writeable: true,
value() {
return dasherize(this);
},
value: deprecateEmberStringPrototypeExtension('dasherize', dasherize),
},

/**
Expand All @@ -372,14 +412,13 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for @ember/string
@static
@private
@deprecated
*/
underscore: {
configurable: true,
enumerable: false,
writeable: true,
value() {
return underscore(this);
},
value: deprecateEmberStringPrototypeExtension('underscore', underscore),
},

/**
Expand All @@ -389,14 +428,13 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for @ember/string
@static
@private
@deprecated
*/
classify: {
configurable: true,
enumerable: false,
writeable: true,
value() {
return classify(this);
},
value: deprecateEmberStringPrototypeExtension('classify', classify),
},

/**
Expand All @@ -406,14 +444,13 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for @ember/string
@static
@private
@deprecated
*/
capitalize: {
configurable: true,
enumerable: false,
writeable: true,
value() {
return capitalize(this);
},
value: deprecateEmberStringPrototypeExtension('capitalize', capitalize),
},
});
}
57 changes: 47 additions & 10 deletions packages/ember/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,34 @@ if (LOGGER) {

// ****@ember/-internals/runtime****
Ember.A = A;

function deprecateStringNamespace(fn) {
return function() {
deprecate(
`Importing ${fn.name} \`@ember/string\` without the addon installed is deprecated`,
false,
{
id: 'ember-string.namespace',
until: '4.0.0',
url: 'https://deprecations.emberjs.com/v3.x#toc_ember-string-namespace',
}
);

fn(...arguments);
};
}

Ember.String = {
loc,
w,
dasherize,
decamelize,
camelize,
classify,
underscore,
capitalize,
loc: deprecateStringNamespace(loc),
w: deprecateStringNamespace(w),
dasherize: deprecateStringNamespace(dasherize),
decamelize: deprecateStringNamespace(decamelize),
camelize: deprecateStringNamespace(camelize),
classify: deprecateStringNamespace(classify),
underscore: deprecateStringNamespace(underscore),
capitalize: deprecateStringNamespace(capitalize),
};

Ember.Object = EmberObject;
Ember._RegistryProxyMixin = RegistryProxyMixin;
Ember._ContainerProxyMixin = ContainerProxyMixin;
Expand Down Expand Up @@ -544,11 +562,30 @@ Ember.HTMLBars = {

if (ENV.EXTEND_PROTOTYPES.String) {
String.prototype.htmlSafe = function() {
deprecate(
'Using string extensions is deprecated, please import htmlSafe from `@ember/template` instead.',
false,
{
id: 'ember-string.prototype_extensions',
until: '4.0.0',
url: 'https://deprecations.emberjs.com/v3.x#toc_ember-string-prototype_extensions',
}
);

return htmlSafe(this);
};
}
Ember.String.htmlSafe = htmlSafe;
Ember.String.isHTMLSafe = isHTMLSafe;
function deprecateStringHTMLSafe(fn, ...args) {
deprecate('This is deprecated, please import utilities from `@ember/template` instead.', false, {
id: 'ember-string.html_safe',
until: '4.0.0',
url: 'https://deprecations.emberjs.com/v3.x#toc_ember-string-html_safe',
});

fn(...args);
}
Ember.String.htmlSafe = deprecateStringHTMLSafe(htmlSafe);
Ember.String.isHTMLSafe = deprecateStringHTMLSafe(isHTMLSafe);

/**
Global hash of shared templates. This will automatically be populated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function setupQUnit() {

callback(hooks);
});
};
} as typeof QUnit.module;

QUnit.assert.rejects = async function(
promise: Promise<any>,
Expand Down
Loading