Skip to content

Commit

Permalink
throw an error on non-frozen raw templates for avoiding possible brea…
Browse files Browse the repository at this point in the history
…king changes in the future
  • Loading branch information
zloirock committed Dec 18, 2022
1 parent 4d1b116 commit 5c6b8fa
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
- Sync version of this proposal moved to Stage 3, [November 2022 TC39 meeting](https://github.com/babel/proposals/issues/85#issuecomment-1333747094)
- Added `/actual/` namespace entries for Stage 3 proposal
- Added [`String.dedent` stage 2 proposal](https://github.com/tc39/proposal-string-dedent)
- Method `String.dedent`
- Throws an error on non-frozen raw templates for avoiding possible breaking changes in the future, [proposal-string-dedent/75](https://github.com/tc39/proposal-string-dedent/issues/75)
- [Compat data targets](/packages/core-js-compat#targets-option) improvements:
- [React Native from 0.70 shipped with Hermes as the default engine.](https://reactnative.dev/blog/2022/07/08/hermes-as-the-default) However, bundled Hermes versions differ from standalone Hermes releases. So added **`react-native`** target for React Native with bundled Hermes.
- [According to the documentation](https://developer.oculus.com/documentation/web/browser-intro/), Oculus Browser was renamed to Meta Quest Browser, so `oculus` target was renamed to **`quest`**.
Expand Down
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
constantSuper: true,
enumerableModuleMeta: true,
iterableIsArray: true,
mutableTemplateObject: true,
mutableTemplateObject: false,
noClassCalls: true,
noDocumentAll: true,
noIncompleteNsImportDetection: true,
Expand Down
5 changes: 5 additions & 0 deletions packages/core-js/modules/esnext.string.dedent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
var FREEZING = require('../internals/freezing');
var $ = require('../internals/export');
var shared = require('../internals/shared');
var getBuiltIn = require('../internals/get-built-in');
Expand Down Expand Up @@ -28,6 +29,8 @@ var $Array = Array;
var $TypeError = TypeError;
// eslint-disable-next-line es/no-object-freeze -- safe
var freeze = Object.freeze || Object;
// eslint-disable-next-line es/no-object-isfrozen -- safe
var isFrozen = Object.isFrozen;
var min = Math.min;
var charAt = uncurryThis(''.charAt);
var stringSlice = uncurryThis(''.slice);
Expand All @@ -43,6 +46,8 @@ var INVALID_CLOSING_LINE = 'Invalid closing line';

var dedentTemplateStringsArray = function (template) {
var rawInput = template.raw;
// https://github.com/tc39/proposal-string-dedent/issues/75
if (FREEZING && !isFrozen(rawInput)) throw $TypeError('Raw template should be frozen');
if (globalDedentRegistry.has(rawInput)) return globalDedentRegistry.get(rawInput);
var raw = dedentStringsArray(rawInput);
var cookedArr = cookStrings(raw);
Expand Down
7 changes: 6 additions & 1 deletion tests/helpers/qunit-helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { DESCRIPTORS } from './constants';
import assign from 'core-js-pure/es/object/assign';
import defineProperties from 'core-js-pure/es/object/define-properties';
import isIterable from 'core-js-pure/es/is-iterable';
import ASYNC_ITERATOR from 'core-js-pure/es/symbol/async-iterator';
import { is, arrayFromArrayLike } from './helpers';

// for Babel template transform
if (!Object.freeze) Object.freeze = Object;
if (!DESCRIPTORS) Object.defineProperties = defineProperties;

const { getOwnPropertyDescriptor } = Object;
const { toString, propertyIsEnumerable } = Object.prototype;

Expand Down Expand Up @@ -79,7 +84,7 @@ assign(QUnit.assert, {
this.pushResult({
result: /native code/.test(source),
actual: source,
expected: 'The function should looks like a native',
expected: 'The function should look like a native',
message,
});
},
Expand Down
16 changes: 9 additions & 7 deletions tests/unit-global/esnext.string.dedent.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const freeze = Object.freeze || Object;

QUnit.test('String.dedent', assert => {
const { cooked, dedent } = String;
assert.isFunction(dedent);
Expand All @@ -24,8 +26,8 @@ QUnit.test('String.dedent', assert => {
${ ' zxc' }
`, ' qwe\n asd\n zxc', '#3');

assert.same(dedent({ raw: ['\n qwe\n '] }), 'qwe', '#4');
assert.same(dedent({ raw: ['\n qwe', '\n '] }, 1), 'qwe1', '#5');
assert.same(dedent({ raw: freeze(['\n qwe\n ']) }), 'qwe', '#4');
assert.same(dedent({ raw: freeze(['\n qwe', '\n ']) }, 1), 'qwe1', '#5');

assert.same(dedent(cooked)`
qwe
Expand All @@ -40,15 +42,15 @@ QUnit.test('String.dedent', assert => {
assert.same(dedent(tag), dedent(tag), '#7');

if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => dedent({ raw: ['\n', Symbol(), '\n'] }), TypeError, 'throws on symbol');
assert.throws(() => dedent({ raw: freeze(['\n', Symbol(), '\n']) }), TypeError, 'throws on symbol');
}

assert.throws(() => dedent([]), TypeError, '[]');
assert.throws(() => dedent(['qwe']), TypeError, '[qwe]');
assert.throws(() => dedent({ raw: [] }), TypeError, 'empty tpl');
assert.throws(() => dedent({ raw: ['qwe'] }), TypeError, 'wrong start');
assert.throws(() => dedent({ raw: ['\n', 'qwe'] }), TypeError, 'wrong start');
assert.throws(() => dedent({ raw: ['\n qwe', 5, '\n '] }, 1, 2), TypeError, 'wrong part');
assert.throws(() => dedent({ raw: freeze([]) }), TypeError, 'empty tpl');
assert.throws(() => dedent({ raw: freeze(['qwe']) }), TypeError, 'wrong start');
assert.throws(() => dedent({ raw: freeze(['\n', 'qwe']) }), TypeError, 'wrong start');
assert.throws(() => dedent({ raw: freeze(['\n qwe', 5, '\n ']) }, 1, 2), TypeError, 'wrong part');
assert.throws(() => dedent([undefined]), TypeError);
assert.throws(() => dedent(null), TypeError);
});
15 changes: 8 additions & 7 deletions tests/unit-pure/esnext.string.dedent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Symbol from 'core-js-pure/es/symbol';
import cooked from 'core-js-pure/full/string/cooked';
import dedent from 'core-js-pure/full/string/dedent';
import freeze from 'core-js-pure/es/object/freeze';

QUnit.test('String.dedent', assert => {
assert.isFunction(dedent);
Expand All @@ -25,8 +26,8 @@ QUnit.test('String.dedent', assert => {
${ ' zxc' }
`, ' qwe\n asd\n zxc', '#3');

assert.same(dedent({ raw: ['\n qwe\n '] }), 'qwe', '#4');
assert.same(dedent({ raw: ['\n qwe', '\n '] }, 1), 'qwe1', '#5');
assert.same(dedent({ raw: freeze(['\n qwe\n ']) }), 'qwe', '#4');
assert.same(dedent({ raw: freeze(['\n qwe', '\n ']) }, 1), 'qwe1', '#5');

assert.same(dedent(cooked)`
qwe
Expand All @@ -41,15 +42,15 @@ QUnit.test('String.dedent', assert => {
assert.same(dedent(tag), dedent(tag), '#7');

if (typeof Symbol == 'function' && !Symbol.sham) {
assert.throws(() => dedent({ raw: ['\n', Symbol(), '\n'] }), TypeError, 'throws on symbol');
assert.throws(() => dedent({ raw: freeze(['\n', Symbol(), '\n']) }), TypeError, 'throws on symbol');
}

assert.throws(() => dedent([]), TypeError, '[]');
assert.throws(() => dedent(['qwe']), TypeError, '[qwe]');
assert.throws(() => dedent({ raw: [] }), TypeError, 'empty tpl');
assert.throws(() => dedent({ raw: ['qwe'] }), TypeError, 'wrong start');
assert.throws(() => dedent({ raw: ['\n', 'qwe'] }), TypeError, 'wrong start');
assert.throws(() => dedent({ raw: ['\n qwe', 5, '\n '] }, 1, 2), TypeError, 'wrong part');
assert.throws(() => dedent({ raw: freeze([]) }), TypeError, 'empty tpl');
assert.throws(() => dedent({ raw: freeze(['qwe']) }), TypeError, 'wrong start');
assert.throws(() => dedent({ raw: freeze(['\n', 'qwe']) }), TypeError, 'wrong start');
assert.throws(() => dedent({ raw: freeze(['\n qwe', 5, '\n ']) }, 1, 2), TypeError, 'wrong part');
assert.throws(() => dedent([undefined]), TypeError);
assert.throws(() => dedent(null), TypeError);
});

0 comments on commit 5c6b8fa

Please sign in to comment.