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
fix: Deleting super property should throw #15223
Merged
nicolo-ribaudo
merged 7 commits into
babel:main
from
SuperSodaSea:fix/delete-super-property
Dec 5, 2022
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0e3494e
fix: Deleting super property should throw
SuperSodaSea 61bbf47
Fix @babel/core dependency
SuperSodaSea a7169ad
Use @babel/template instead of @babel/core
SuperSodaSea 64f4c70
Merge remote-tracking branch 'upstream/main' into fix/delete-super-pr…
SuperSodaSea 38dcf9d
Add optional delete in Handler
SuperSodaSea 9604c1a
Update test
SuperSodaSea 7ce2779
Update packages/babel-helper-member-expression-to-functions/src/index.ts
SuperSodaSea File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,9 +1,11 @@ | ||||||
import type { NodePath, Scope } from "@babel/traverse"; | ||||||
import traverse from "@babel/traverse"; | ||||||
import { template } from "@babel/core"; | ||||||
import type { File } from "@babel/core"; | ||||||
import environmentVisitor from "@babel/helper-environment-visitor"; | ||||||
import memberExpressionToFunctions from "@babel/helper-member-expression-to-functions"; | ||||||
import type { HandlerState } from "@babel/helper-member-expression-to-functions"; | ||||||
import optimiseCall from "@babel/helper-optimise-call-expression"; | ||||||
import environmentVisitor from "@babel/helper-environment-visitor"; | ||||||
import traverse from "@babel/traverse"; | ||||||
import type { NodePath, Scope } from "@babel/traverse"; | ||||||
import { | ||||||
assignmentExpression, | ||||||
booleanLiteral, | ||||||
|
@@ -16,7 +18,6 @@ import { | |||||
thisExpression, | ||||||
} from "@babel/types"; | ||||||
import type * as t from "@babel/types"; | ||||||
import type { File } from "@babel/core"; | ||||||
|
||||||
// TODO (Babel 8): Don't export this. | ||||||
export { | ||||||
|
@@ -64,6 +65,26 @@ const visitor = traverse.visitors.merge< | |||||
Super(path, state) { | ||||||
const { node, parentPath } = path; | ||||||
if (!parentPath.isMemberExpression({ object: node })) return; | ||||||
if (parentPath.parentPath.isUnaryExpression({ operator: "delete" })) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic should be moved in a different place: instead of special-casing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 38dcf9d :) |
||||||
const deletePath = parentPath.parentPath; | ||||||
if (parentPath.node.computed) { | ||||||
deletePath.replaceWith( | ||||||
sequenceExpression([ | ||||||
callExpression(this.file.addHelper("toPropertyKey"), [ | ||||||
cloneNode(parentPath.node.property as t.Expression), | ||||||
]), | ||||||
template.expression.ast` | ||||||
function () { throw new ReferenceError("'delete super[expr]' is invalid"); }() | ||||||
`, | ||||||
]), | ||||||
); | ||||||
} else { | ||||||
deletePath.replaceWith(template.expression.ast` | ||||||
function () { throw new ReferenceError("'delete super.prop' is invalid"); }() | ||||||
`); | ||||||
} | ||||||
return; | ||||||
} | ||||||
state.handle(parentPath); | ||||||
}, | ||||||
}, | ||||||
|
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-transform-classes/test/fixtures/regression/15169/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
expect(() => { | ||
class x { static y = delete super[0]; } | ||
}).toThrow(ReferenceError); |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-transform-classes/test/fixtures/regression/15169/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
class x { static y = delete super[0]; } |
8 changes: 8 additions & 0 deletions
8
packages/babel-plugin-transform-classes/test/fixtures/regression/15169/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var x = /*#__PURE__*/babelHelpers.createClass(function x() { | ||
"use strict"; | ||
|
||
babelHelpers.classCallCheck(this, x); | ||
}); | ||
babelHelpers.defineProperty(x, "y", (babelHelpers.toPropertyKey(0), function () { | ||
throw new ReferenceError("'delete super[expr]' is invalid"); | ||
}())); |
23 changes: 23 additions & 0 deletions
23
...plugin-transform-classes/test/fixtures/spec/delete-super-property-to-property-key/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// [Symbol.toPrimitive] must be called if exist | ||
var counter = 0; | ||
expect(() => { | ||
(new class { | ||
f() { | ||
delete super[{ | ||
[Symbol.toPrimitive]: function() { ++counter; return 0; }, | ||
}]; | ||
} | ||
}).f(); | ||
}).toThrow(ReferenceError); | ||
expect(counter).toBe(1); | ||
|
||
// [Symbol.toPrimitive] must return a primitive value | ||
expect(() => { | ||
(new class { | ||
f() { | ||
delete super[{ | ||
[Symbol.toPrimitive]: function() { return {}; }, | ||
}]; | ||
} | ||
}).f(); | ||
}).toThrow(TypeError); |
18 changes: 18 additions & 0 deletions
18
...lugin-transform-classes/test/fixtures/spec/delete-super-property-to-property-key/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// [Symbol.toPrimitive] must be called if exist | ||
var counter = 0; | ||
(new class { | ||
f() { | ||
delete super[{ | ||
[Symbol.toPrimitive]: function() { ++counter; return 0; }, | ||
}]; | ||
} | ||
}).f(); | ||
|
||
// [Symbol.toPrimitive] must return a primitive value | ||
(new class { | ||
f() { | ||
delete super[{ | ||
[Symbol.toPrimitive]: function() { return {}; }, | ||
}]; | ||
} | ||
}).f(); |
45 changes: 45 additions & 0 deletions
45
...ugin-transform-classes/test/fixtures/spec/delete-super-property-to-property-key/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// [Symbol.toPrimitive] must be called if exist | ||
var counter = 0; | ||
new ( /*#__PURE__*/function () { | ||
"use strict"; | ||
|
||
function _class() { | ||
babelHelpers.classCallCheck(this, _class); | ||
} | ||
babelHelpers.createClass(_class, [{ | ||
key: "f", | ||
value: function f() { | ||
babelHelpers.toPropertyKey({ | ||
[Symbol.toPrimitive]: function () { | ||
++counter; | ||
return 0; | ||
} | ||
}), function () { | ||
throw new ReferenceError("'delete super[expr]' is invalid"); | ||
}(); | ||
} | ||
}]); | ||
return _class; | ||
}())().f(); | ||
|
||
// [Symbol.toPrimitive] must return a primitive value | ||
new ( /*#__PURE__*/function () { | ||
"use strict"; | ||
|
||
function _class2() { | ||
babelHelpers.classCallCheck(this, _class2); | ||
} | ||
babelHelpers.createClass(_class2, [{ | ||
key: "f", | ||
value: function f() { | ||
babelHelpers.toPropertyKey({ | ||
[Symbol.toPrimitive]: function () { | ||
return {}; | ||
} | ||
}), function () { | ||
throw new ReferenceError("'delete super[expr]' is invalid"); | ||
}(); | ||
} | ||
}]); | ||
return _class2; | ||
}())().f(); |
27 changes: 27 additions & 0 deletions
27
packages/babel-plugin-transform-classes/test/fixtures/spec/delete-super-property/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
expect(() => { | ||
(new class { | ||
f() { delete super.x; } | ||
}).f(); | ||
}).toThrow(ReferenceError); | ||
|
||
expect(() => { | ||
(new class { | ||
f() { delete super[0]; } | ||
}).f(); | ||
}).toThrow(ReferenceError); | ||
|
||
// [expr] should be evaluated | ||
var counter = 0; | ||
expect(() => { | ||
(new class { | ||
f() { delete super[++counter]; } | ||
}).f(); | ||
}).toThrow(ReferenceError); | ||
expect(counter).toBe(1); | ||
|
||
// TypeError before ReferenceError | ||
expect(() => { | ||
(new class { | ||
f() { delete super[0()]; } | ||
}).f(); | ||
}).toThrow(TypeError); |
18 changes: 18 additions & 0 deletions
18
packages/babel-plugin-transform-classes/test/fixtures/spec/delete-super-property/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(new class { | ||
f() { delete super.x; } | ||
}).f(); | ||
|
||
(new class { | ||
f() { delete super[0]; } | ||
}).f(); | ||
|
||
// [expr] should be evaluated | ||
var counter = 0; | ||
(new class { | ||
f() { delete super[++counter]; } | ||
}).f(); | ||
|
||
// TypeError before ReferenceError | ||
(new class { | ||
f() { delete super[0()]; } | ||
}).f(); |
69 changes: 69 additions & 0 deletions
69
packages/babel-plugin-transform-classes/test/fixtures/spec/delete-super-property/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
new ( /*#__PURE__*/function () { | ||
"use strict"; | ||
|
||
function _class() { | ||
babelHelpers.classCallCheck(this, _class); | ||
} | ||
babelHelpers.createClass(_class, [{ | ||
key: "f", | ||
value: function f() { | ||
(function () { | ||
throw new ReferenceError("'delete super.prop' is invalid"); | ||
})(); | ||
} | ||
}]); | ||
return _class; | ||
}())().f(); | ||
new ( /*#__PURE__*/function () { | ||
"use strict"; | ||
|
||
function _class2() { | ||
babelHelpers.classCallCheck(this, _class2); | ||
} | ||
babelHelpers.createClass(_class2, [{ | ||
key: "f", | ||
value: function f() { | ||
babelHelpers.toPropertyKey(0), function () { | ||
throw new ReferenceError("'delete super[expr]' is invalid"); | ||
}(); | ||
} | ||
}]); | ||
return _class2; | ||
}())().f(); | ||
|
||
// [expr] should be evaluated | ||
var counter = 0; | ||
new ( /*#__PURE__*/function () { | ||
"use strict"; | ||
|
||
function _class3() { | ||
babelHelpers.classCallCheck(this, _class3); | ||
} | ||
babelHelpers.createClass(_class3, [{ | ||
key: "f", | ||
value: function f() { | ||
babelHelpers.toPropertyKey(++counter), function () { | ||
throw new ReferenceError("'delete super[expr]' is invalid"); | ||
}(); | ||
} | ||
}]); | ||
return _class3; | ||
}())().f(); | ||
|
||
// TypeError before ReferenceError | ||
new ( /*#__PURE__*/function () { | ||
"use strict"; | ||
|
||
function _class4() { | ||
babelHelpers.classCallCheck(this, _class4); | ||
} | ||
babelHelpers.createClass(_class4, [{ | ||
key: "f", | ||
value: function f() { | ||
babelHelpers.toPropertyKey(0()), function () { | ||
throw new ReferenceError("'delete super[expr]' is invalid"); | ||
}(); | ||
} | ||
}]); | ||
return _class4; | ||
}())().f(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should need to be a peer dependency, though I'm not sure, see what other people think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that other packages use this: