Skip to content

Commit

Permalink
[DEPRECATION canary] Deprecate Ember.String an prototype extension
Browse files Browse the repository at this point in the history
This PR deprecates the use of String prototype extension and
Ember.String namespace.

Methods coming from ember-runtime
---

A new `StringUtils` object is added, to be used internally by Ember
instead of using the same object being exposed as `Ember.String`. This
simplifies testing and the changes required in the rest of the code.
Also, dropping the deprecated code will be much simpler.

Methods coming from ember-glimmer
---

Deprecated versions are being added and reexported as the same global
the non-deprecated versions were. Besides, the non-deprecated version is
being exported under the new global `Ember.Template`.
  • Loading branch information
Serabe committed Oct 13, 2017
1 parent 032a271 commit 37a06d4
Show file tree
Hide file tree
Showing 28 changed files with 233 additions and 61 deletions.
2 changes: 1 addition & 1 deletion packages/ember-application/lib/system/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { dictionary } from 'ember-utils';
import { get } from 'ember-metal';
import { assert, info } from 'ember-debug';
import {
String as StringUtils,
Object as EmberObject,
StringUtils,
Namespace
} from 'ember-runtime';
import validateType from '../utils/validate-type';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Ember from 'ember-metal'; // Ember as namespace
import {
A as emberA,
typeOf,
String as StringUtils,
StringUtils,
Namespace,
Object as EmberObject
} from 'ember-runtime';
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-extension-support/lib/data_adapter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getOwner } from 'ember-utils';
import { get, run } from 'ember-metal';
import {
String as StringUtils,
StringUtils,
Namespace,
Object as EmberObject,
A as emberA,
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-glimmer/lib/helpers/-class.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InternalHelperReference } from '../utils/references';
import { String as StringUtils } from 'ember-runtime';
import { StringUtils } from 'ember-runtime';

function classHelper({ positional }) {
let path = positional.at(0);
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-glimmer/lib/helpers/-normalize-class.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InternalHelperReference } from '../utils/references';
import { String as StringUtils } from 'ember-runtime';
import { StringUtils } from 'ember-runtime';

function normalizeClass({ positional, named }) {
let classNameParts = positional.at(0).value().split('.');
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-glimmer/lib/helpers/loc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@submodule ember-glimmer
*/
import { InternalHelperReference } from '../utils/references';
import { String as StringUtils } from 'ember-runtime';
import { StringUtils } from 'ember-runtime';

/**
Calls [Ember.String.loc](/api/classes/Ember.String.html#method_loc) with the
Expand Down
4 changes: 3 additions & 1 deletion packages/ember-glimmer/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
{{/each}}
{{/with}}
```
NOTE: The alias should not reuse a name from the bound property path.
For example: `{{#with foo.bar as |foo|}}` is not supported because it attempts to alias using
Expand Down Expand Up @@ -268,7 +268,9 @@ export {
SafeString,
escapeExpression,
htmlSafe,
deprecatedHTMLSafe,
isHTMLSafe,
deprecatedIsHTMLSafe,
getSafeString as _getSafeString
} from './utils/string';
export {
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-glimmer/lib/utils/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@glimmer/wire-format';
import { assert } from 'ember-debug';
import { get } from 'ember-metal';
import { String as StringUtils } from 'ember-runtime';
import { StringUtils } from 'ember-runtime';
import { ROOT_REF } from '../component';
import { htmlSafe, isHTMLSafe } from './string';

Expand Down
28 changes: 28 additions & 0 deletions packages/ember-glimmer/lib/utils/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function escapeExpression(string) {
@static
@return {Handlebars.SafeString} A string that will not be HTML escaped by Handlebars.
@public
@deprecated
*/
export function htmlSafe(str) {
if (str === null || str === undefined) {
Expand All @@ -97,6 +98,19 @@ export function htmlSafe(str) {
return new SafeString(str);
}

export function deprecatedHTMLSafe(str) {
deprecate(
'Ember.String namespace is deprecated. Please, import `htmlSafe` from `ember/template`.',
false,
{
id: 'ember-glimmer/ember-string-html-safe',
until: '3.5.0',
url: ''
}
);
return htmlSafe(str);
}

/**
Detects if a string was decorated using `Ember.String.htmlSafe`.
Expand All @@ -113,7 +127,21 @@ export function htmlSafe(str) {
@static
@return {Boolean} `true` if the string was decorated with `htmlSafe`, `false` otherwise.
@public
@deprecated
*/
export function isHTMLSafe(str) {
return str && typeof str.toHTML === 'function';
}

export function deprecatedIsHTMLSafe(str) {
deprecate(
'Ember.String namespace is deprecated. Please, import `isHTMLSafe` from `ember/template`.',
false,
{
id: 'ember-glimmer/ember-string-is-html-safe',
until: '3.5.0',
url: ''
}
);
return isHTMLSafe(str);
}
4 changes: 3 additions & 1 deletion packages/ember-glimmer/tests/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export {
InteractiveRender,
InertRenderer,
htmlSafe,
deprecatedHTMLSafe,
SafeString,
DOMChanges,
isHTMLSafe
isHTMLSafe,
deprecatedIsHTMLSafe
} from 'ember-glimmer';
18 changes: 17 additions & 1 deletion packages/ember-glimmer/tests/utils/string-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { SafeString, htmlSafe, isHTMLSafe } from './helpers';
import {
SafeString,
htmlSafe,
deprecatedHTMLSafe,
isHTMLSafe,
deprecatedIsHTMLSafe
} from './helpers';
import { TestCase } from './abstract-test-case';
import { moduleFor } from './test-case';

moduleFor('SafeString', class extends TestCase {
['@test deprecated version is deprecated']() {
expectDeprecation(/ember\/template/);
deprecatedHTMLSafe("Hello");
}

['@test htmlSafe should return an instance of SafeString']() {
let safeString = htmlSafe('you need to be more <b>bold</b>');

Expand All @@ -25,6 +36,11 @@ moduleFor('SafeString', class extends TestCase {
});

moduleFor('SafeString isHTMLSafe', class extends TestCase {
['@test deprecated version is deprecated']() {
expectDeprecation(/ember\/template/);
deprecatedIsHTMLSafe("Hello");
}

['@test isHTMLSafe should detect SafeString']() {
let safeString = htmlSafe('<em>Emphasize</em> the important things.');

Expand Down
2 changes: 1 addition & 1 deletion packages/ember-routing/lib/system/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DEBUG } from 'ember-env-flags';
import {
typeOf,
copy,
String as StringUtils,
StringUtils,
Object as EmberObject,
A as emberA,
Evented,
Expand Down
57 changes: 33 additions & 24 deletions packages/ember-runtime/lib/ext/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,27 @@ import {
capitalize,
classify
} from '../system/string';
import { deprecate } from 'ember-debug';
import { assign } from 'ember-utils';

const StringPrototype = String.prototype;

function deprecateEmberStringExtension(fn, opts = {}) {
let { name } = fn;
return function(...args) {
deprecate(
opts.message || `Extending String prototype is deprecated. Please, use ${name} from '@ember/string' instead.`,
false,
opts.options || {
id: 'ember-runtime/string-prototype-extension',
until: '3.5.0',
url: ''
}
);
return fn(this, args);
}
}

if (ENV.EXTEND_PROTOTYPES.String) {
/**
See [Ember.String.fmt](/api/classes/Ember.String.html#method_fmt).
Expand All @@ -38,9 +56,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for String
@private
*/
StringPrototype.w = function () {
return w(this);
};
StringPrototype.w = deprecateEmberStringExtension(w);

/**
See [Ember.String.loc](/api/classes/Ember.String.html#method_loc).
Expand All @@ -49,9 +65,14 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for String
@private
*/
StringPrototype.loc = function (...args) {
return loc(this, args);
};
StringPrototype.loc = deprecateEmberStringExtension(loc, {
message: '`loc` is deprecated. Please, use an i18n addon instead. See https://emberobserver.com/categories/internationalization for a list of them.',
options: {
id: 'ember-string-utils/loc',
until: '3.5.0',
url: ''
}
});

/**
See [Ember.String.camelize](/api/classes/Ember.String.html#method_camelize).
Expand All @@ -60,9 +81,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for String
@private
*/
StringPrototype.camelize = function () {
return camelize(this);
};
StringPrototype.camelize = deprecateEmberStringExtension(camelize);

/**
See [Ember.String.decamelize](/api/classes/Ember.String.html#method_decamelize).
Expand All @@ -71,9 +90,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for String
@private
*/
StringPrototype.decamelize = function () {
return decamelize(this);
};
StringPrototype.decamelize = deprecateEmberStringExtension(decamelize);

/**
See [Ember.String.dasherize](/api/classes/Ember.String.html#method_dasherize).
Expand All @@ -82,9 +99,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for String
@private
*/
StringPrototype.dasherize = function () {
return dasherize(this);
};
StringPrototype.dasherize = deprecateEmberStringExtension(dasherize);

/**
See [Ember.String.underscore](/api/classes/Ember.String.html#method_underscore).
Expand All @@ -93,9 +108,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for String
@private
*/
StringPrototype.underscore = function () {
return underscore(this);
};
StringPrototype.underscore = deprecateEmberStringExtension(underscore);

/**
See [Ember.String.classify](/api/classes/Ember.String.html#method_classify).
Expand All @@ -104,9 +117,7 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for String
@private
*/
StringPrototype.classify = function () {
return classify(this);
};
StringPrototype.classify = deprecateEmberStringExtension(classify);

/**
See [Ember.String.capitalize](/api/classes/Ember.String.html#method_capitalize).
Expand All @@ -115,7 +126,5 @@ if (ENV.EXTEND_PROTOTYPES.String) {
@for String
@private
*/
StringPrototype.capitalize = function () {
return capitalize(this);
};
StringPrototype.capitalize = deprecateEmberStringExtension(capitalize);
}
2 changes: 1 addition & 1 deletion packages/ember-runtime/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

export { default as Object, FrameworkObject } from './system/object';
export { default as String } from './system/string';
export { default as String, StringUtils } from './system/string';
export {
default as RegistryProxyMixin,
buildFakeRegistryWithDeprecations
Expand Down
Loading

0 comments on commit 37a06d4

Please sign in to comment.