Skip to content

Commit c59870c

Browse files
Mark class prototype as read-only (#12115)
* initial code 2 fix the issue #2025 * Mark class prototype as read-only * Update fixtures * Fix failure * Update Babel 8 fixtures * Disable in loose mode * Update fixtures Co-authored-by: Nicolò Ribaudo <[email protected]>
1 parent 3908049 commit c59870c

File tree

221 files changed

+490
-458
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

221 files changed

+490
-458
lines changed

packages/babel-cli/test/fixtures/babel-external-helpers/--whitelist/stdout.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
function _createClass(Constructor, protoProps, staticProps) {
1515
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
1616
if (staticProps) _defineProperties(Constructor, staticProps);
17+
Object.defineProperty(Constructor, "prototype", {
18+
writable: false
19+
});
1720
return Constructor;
1821
}
1922

packages/babel-core/test/fixtures/transformation/misc/regression-1155/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ var Foo = /*#__PURE__*/function (_Bar) {
1616
return _super.call(this, parentOptions);
1717
}
1818

19-
return Foo;
19+
return babelHelpers.createClass(Foo);
2020
}(Bar);

packages/babel-helpers/src/helpers.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ helpers.createClass = helper("7.0.0-beta.0")`
223223
export default function _createClass(Constructor, protoProps, staticProps) {
224224
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
225225
if (staticProps) _defineProperties(Constructor, staticProps);
226+
Object.defineProperty(Constructor, "prototype", { writable: false });
226227
return Constructor;
227228
}
228229
`;
@@ -334,12 +335,15 @@ helpers.inherits = helper("7.0.0-beta.0")`
334335
if (typeof superClass !== "function" && superClass !== null) {
335336
throw new TypeError("Super expression must either be null or a function");
336337
}
337-
subClass.prototype = Object.create(superClass && superClass.prototype, {
338-
constructor: {
339-
value: subClass,
340-
writable: true,
341-
configurable: true
342-
}
338+
Object.defineProperty(subClass, "prototype", {
339+
value: Object.create(superClass && superClass.prototype, {
340+
constructor: {
341+
value: subClass,
342+
writable: true,
343+
configurable: true
344+
}
345+
}),
346+
writable: false,
343347
});
344348
if (superClass) setPrototypeOf(subClass, superClass);
345349
}

packages/babel-plugin-proposal-class-properties/test/fixtures/assumption-setPublicClassFields/foobar/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ var Child = /*#__PURE__*/function (_Parent) {
1818
return _this;
1919
}
2020

21-
return Child;
21+
return babelHelpers.createClass(Child);
2222
}(Parent);

packages/babel-plugin-proposal-class-properties/test/fixtures/decorators-legacy-interop/local-define-property/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ function dec() {} // Create a local function binding so babel has to change the
55

66
function _defineProperty() {}
77

8-
let A = (_class = function A() {
8+
let A = (_class = /*#__PURE__*/babelHelpers.createClass(function A() {
99
"use strict";
1010

1111
babelHelpers.classCallCheck(this, A);
1212
babelHelpers.initializerDefineProperty(this, "a", _descriptor, this);
1313
babelHelpers.initializerDefineProperty(this, "b", _descriptor2, this);
1414
babelHelpers.defineProperty(this, "c", 456);
15-
}, (_descriptor = babelHelpers.applyDecoratedDescriptor(_class.prototype, "a", [dec], {
15+
}), (_descriptor = babelHelpers.applyDecoratedDescriptor(_class.prototype, "a", [dec], {
1616
configurable: true,
1717
enumerable: true,
1818
writable: true,

packages/babel-plugin-proposal-class-properties/test/fixtures/decorators-legacy-interop/loose/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ var _class, _descriptor, _descriptor2;
22

33
function dec() {}
44

5-
let A = (_class = function A() {
5+
let A = (_class = /*#__PURE__*/babelHelpers.createClass(function A() {
66
"use strict";
77

88
babelHelpers.classCallCheck(this, A);
99
babelHelpers.initializerDefineProperty(this, "a", _descriptor, this);
1010
babelHelpers.initializerDefineProperty(this, "b", _descriptor2, this);
1111
this.c = 456;
12-
}, (_descriptor = babelHelpers.applyDecoratedDescriptor(_class.prototype, "a", [dec], {
12+
}), (_descriptor = babelHelpers.applyDecoratedDescriptor(_class.prototype, "a", [dec], {
1313
configurable: true,
1414
enumerable: true,
1515
writable: true,

packages/babel-plugin-proposal-class-properties/test/fixtures/decorators-legacy-interop/strict/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ var _class, _descriptor, _descriptor2;
22

33
function dec() {}
44

5-
let A = (_class = function A() {
5+
let A = (_class = /*#__PURE__*/babelHelpers.createClass(function A() {
66
"use strict";
77

88
babelHelpers.classCallCheck(this, A);
99
babelHelpers.initializerDefineProperty(this, "a", _descriptor, this);
1010
babelHelpers.initializerDefineProperty(this, "b", _descriptor2, this);
1111
babelHelpers.defineProperty(this, "c", 456);
12-
}, (_descriptor = babelHelpers.applyDecoratedDescriptor(_class.prototype, "a", [dec], {
12+
}), (_descriptor = babelHelpers.applyDecoratedDescriptor(_class.prototype, "a", [dec], {
1313
configurable: true,
1414
enumerable: true,
1515
writable: true,
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"use strict";
22

3-
let Hello = function Hello() {
3+
let Hello = /*#__PURE__*/babelHelpers.createClass(function Hello() {
44
babelHelpers.classCallCheck(this, Hello);
55
return {
66
toString() {
77
return 'hello';
88
}
99

1010
};
11-
};
11+
});
1212

1313
let Outer = /*#__PURE__*/function (_Hello) {
1414
babelHelpers.inherits(Outer, _Hello);
@@ -22,16 +22,14 @@ let Outer = /*#__PURE__*/function (_Hello) {
2222

2323
babelHelpers.classCallCheck(this, Outer);
2424
_this2 = _this = _super.call(this);
25-
26-
let Inner = function Inner() {
25+
let Inner = /*#__PURE__*/babelHelpers.createClass(function Inner() {
2726
babelHelpers.classCallCheck(this, Inner);
2827
babelHelpers.defineProperty(this, _this2, "hello");
29-
};
30-
28+
});
3129
return babelHelpers.possibleConstructorReturn(_this, new Inner());
3230
}
3331

34-
return Outer;
32+
return babelHelpers.createClass(Outer);
3533
}(Hello);
3634

3735
expect(new Outer().hello).toBe('hello');

packages/babel-plugin-proposal-class-properties/test/fixtures/nested-class/super-property-in-key/output.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,14 @@ let Outer = /*#__PURE__*/function (_Hello) {
2727
babelHelpers.classCallCheck(this, Outer);
2828
_this = _super.call(this);
2929
_babelHelpers$get$cal = babelHelpers.get((_thisSuper = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Outer.prototype)), "toString", _thisSuper).call(_thisSuper);
30-
31-
let Inner = function Inner() {
30+
let Inner = /*#__PURE__*/babelHelpers.createClass(function Inner() {
3231
babelHelpers.classCallCheck(this, Inner);
3332
babelHelpers.defineProperty(this, _babelHelpers$get$cal, 'hello');
34-
};
35-
33+
});
3634
return babelHelpers.possibleConstructorReturn(_this, new Inner());
3735
}
3836

39-
return Outer;
37+
return babelHelpers.createClass(Outer);
4038
}(Hello);
4139

4240
expect(new Outer().hello).toBe('hello');

packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/constructor-collision/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var foo = "bar";
22

33
var _bar = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("bar");
44

5-
var Foo = function Foo() {
5+
var Foo = /*#__PURE__*/babelHelpers.createClass(function Foo() {
66
"use strict";
77

88
babelHelpers.classCallCheck(this, Foo);
@@ -11,4 +11,4 @@ var Foo = function Foo() {
1111
value: foo
1212
});
1313
var _foo = "foo";
14-
};
14+
});

0 commit comments

Comments
 (0)