Skip to content

Commit df601f9

Browse files
fix: validate rhs of in when transpiling #p in C (#15133)
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com> Fixes #15013
1 parent 641f677 commit df601f9

74 files changed

Lines changed: 313 additions & 110 deletions

File tree

Some content is hidden

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

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ new-version-checklist:
192192
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
193193
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
194194
# @echo "!!!!!! !!!!!!"
195-
# @echo "!!!!!! Write any message that should !!!!!!"
196-
# @echo "!!!!!! block the release here !!!!!!"
195+
# @echo "!!!!!! Update the minVersion of packages/babel-helpers/src/helpers/checkInRHS.js"
196+
# @echo "!!!!!! !!!!!!"
197197
# @echo "!!!!!! !!!!!!"
198198
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
199199
# @echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"

packages/babel-helper-create-class-features-plugin/src/decorators.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,7 @@ function extractElementDescriptor(
142142
}
143143

144144
function addDecorateHelper(file: File) {
145-
try {
146-
return file.addHelper("decorate");
147-
} catch (err) {
148-
if (err.code === "BABEL_HELPER_UNKNOWN") {
149-
err.message +=
150-
"\n '@babel/plugin-transform-decorators' in non-legacy mode" +
151-
" requires '@babel/core' version ^7.0.2 and you appear to be using" +
152-
" an older version.";
153-
}
154-
throw err;
155-
}
145+
return file.addHelper("decorate");
156146
}
157147

158148
type ClassElement = t.Class["body"]["body"][number];

packages/babel-helper-create-class-features-plugin/src/fields.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,21 @@ function unshadow(
206206
}
207207
}
208208

209+
export function buildCheckInRHS(
210+
rhs: t.Expression,
211+
file: File,
212+
inRHSIsObject?: boolean,
213+
) {
214+
if (inRHSIsObject || !file.availableHelper?.("checkInRHS")) return rhs;
215+
return t.callExpression(file.addHelper("checkInRHS"), [rhs]);
216+
}
217+
209218
const privateInVisitor = privateNameVisitorFactory<{
210219
classRef: t.Identifier;
211220
file: File;
212221
innerBinding?: t.Identifier;
213222
}>({
214-
BinaryExpression(path) {
223+
BinaryExpression(path, { file }) {
215224
const { operator, left, right } = path.node;
216225
if (operator !== "in") return;
217226
if (!t.isPrivateName(left)) return;
@@ -230,19 +239,32 @@ const privateInVisitor = privateNameVisitorFactory<{
230239
if (privateFieldsAsProperties) {
231240
const { id } = privateNamesMap.get(name);
232241
path.replaceWith(template.expression.ast`
233-
Object.prototype.hasOwnProperty.call(${right}, ${t.cloneNode(id)})
242+
Object.prototype.hasOwnProperty.call(${buildCheckInRHS(
243+
right,
244+
file,
245+
)}, ${t.cloneNode(id)})
234246
`);
235247
return;
236248
}
237249

238250
const { id, static: isStatic } = privateNamesMap.get(name);
239251

240252
if (isStatic) {
241-
path.replaceWith(template.expression.ast`${right} === ${this.classRef}`);
253+
path.replaceWith(
254+
template.expression.ast`${buildCheckInRHS(
255+
right,
256+
file,
257+
)} === ${t.cloneNode(this.classRef)}`,
258+
);
242259
return;
243260
}
244261

245-
path.replaceWith(template.expression.ast`${t.cloneNode(id)}.has(${right})`);
262+
path.replaceWith(
263+
template.expression.ast`${t.cloneNode(id)}.has(${buildCheckInRHS(
264+
right,
265+
file,
266+
)})`,
267+
);
246268
},
247269
});
248270

packages/babel-helper-create-class-features-plugin/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import {
88
buildPrivateNamesMap,
99
transformPrivateNamesUsage,
1010
buildFieldsInitNodes,
11+
buildCheckInRHS,
1112
} from "./fields";
1213
import type { PropPath } from "./fields";
1314
import { buildDecoratedClass, hasDecorators } from "./decorators";
1415
import { injectInitialization, extractComputedKeys } from "./misc";
1516
import { enableFeature, FEATURES, isLoose, shouldTransform } from "./features";
1617
import { assertFieldTransformed } from "./typescript";
1718

18-
export { FEATURES, enableFeature, injectInitialization };
19+
export { FEATURES, enableFeature, injectInitialization, buildCheckInRHS };
1920

2021
declare const PACKAGE_JSON: { name: string; version: string };
2122

packages/babel-helpers/src/helpers-generated.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export default Object.freeze({
4141
"7.0.0-beta.0",
4242
'import OverloadYield from"OverloadYield";export default function _awaitAsyncGenerator(value){return new OverloadYield(value,0)}',
4343
),
44+
checkInRHS: helper(
45+
"7.20.1",
46+
'export default function _checkInRHS(value){if(Object(value)!==value)throw TypeError("right-hand side of \'in\' should be an object, got "+(null!==value?typeof value:"null"));return value}',
47+
),
4448
jsx: helper(
4549
"7.0.0-beta.0",
4650
'var REACT_ELEMENT_TYPE;export default function _createRawReactElement(type,props,key,children){REACT_ELEMENT_TYPE||(REACT_ELEMENT_TYPE="function"==typeof Symbol&&Symbol.for&&Symbol.for("react.element")||60103);var defaultProps=type&&type.defaultProps,childrenLength=arguments.length-3;if(props||0===childrenLength||(props={children:void 0}),1===childrenLength)props.children=children;else if(childrenLength>1){for(var childArray=new Array(childrenLength),i=0;i<childrenLength;i++)childArray[i]=arguments[i+3];props.children=childArray}if(props&&defaultProps)for(var propName in defaultProps)void 0===props[propName]&&(props[propName]=defaultProps[propName]);else props||(props=defaultProps||{});return{$$typeof:REACT_ELEMENT_TYPE,type:type,key:void 0===key?null:""+key,ref:null,props:props,_owner:null}}',
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* @minVersion 7.20.1 */
2+
3+
export default function _checkInRHS(value) {
4+
if (Object(value) !== value) {
5+
throw TypeError(
6+
"right-hand side of 'in' should be an object, got " +
7+
(value !== null ? typeof value : "null")
8+
);
9+
}
10+
return value;
11+
}

packages/babel-plugin-proposal-decorators/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default declare((api, options: Options) => {
4040
} else if (version === "2021-12" || version === "2022-03") {
4141
return transformer2022_03(api, options, version);
4242
} else if (!process.env.BABEL_8_BREAKING) {
43+
api.assertVersion("^7.0.2");
4344
return createClassFeaturePlugin({
4445
name: "proposal-decorators",
4546

packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-classes--to-es2015/replacement-static-installed-on-correct-class/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ new (_x = /*#__PURE__*/new WeakMap(), _m = /*#__PURE__*/new WeakSet(), (_temp =
88
writable: true,
99
value: void 0
1010
}), babelHelpers.defineProperty(this, "x", void 0)), (() => {
11-
hasX = o => _x.has(o);
12-
hasM = o => _m.has(o);
11+
hasX = o => _x.has(babelHelpers.checkInRHS(o));
12+
hasM = o => _m.has(babelHelpers.checkInRHS(o));
1313
})(), _initClass();
1414
}
1515
}, (() => {

packages/babel-plugin-proposal-decorators/test/fixtures/2022-03-classes--to-es2015/replacement-static-installed-on-correct-class/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ new (_x = /*#__PURE__*/new WeakMap(), _m = /*#__PURE__*/new WeakSet(), (_temp =
88
writable: true,
99
value: void 0
1010
}), babelHelpers.defineProperty(this, "x", void 0)), (() => {
11-
hasX = o => _x.has(o);
12-
hasM = o => _m.has(o);
11+
hasX = o => _x.has(babelHelpers.checkInRHS(o));
12+
hasM = o => _m.has(babelHelpers.checkInRHS(o));
1313
})(), _initClass();
1414
}
1515
}, (() => {

packages/babel-plugin-proposal-private-property-in-object/src/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
enableFeature,
55
FEATURES,
66
injectInitialization as injectConstructorInit,
7+
buildCheckInRHS,
78
} from "@babel/helper-create-class-features-plugin";
89
import annotateAsPure from "@babel/helper-annotate-as-pure";
910
import type * as t from "@babel/types";
@@ -119,8 +120,9 @@ export default declare((api, opt: Options) => {
119120
enableFeature(this.file, FEATURES.privateIn, loose);
120121
},
121122
visitor: {
122-
BinaryExpression(path) {
123+
BinaryExpression(path, state) {
123124
const { node } = path;
125+
const { file } = state;
124126
if (node.operator !== "in") return;
125127
if (!t.isPrivateName(node.left)) return;
126128

@@ -158,7 +160,10 @@ export default declare((api, opt: Options) => {
158160
}
159161
path.replaceWith(
160162
template.expression.ast`
161-
${t.cloneNode(outerClass.node.id)} === ${path.node.right}
163+
${t.cloneNode(outerClass.node.id)} === ${buildCheckInRHS(
164+
node.right,
165+
file,
166+
)}
162167
`,
163168
);
164169
} else {
@@ -171,7 +176,10 @@ export default declare((api, opt: Options) => {
171176
);
172177

173178
path.replaceWith(
174-
template.expression.ast`${id}.has(${path.node.right})`,
179+
template.expression.ast`${id}.has(${buildCheckInRHS(
180+
node.right,
181+
file,
182+
)})`,
175183
);
176184
}
177185
} else {
@@ -187,7 +195,10 @@ export default declare((api, opt: Options) => {
187195
);
188196

189197
path.replaceWith(
190-
template.expression.ast`${id}.has(${path.node.right})`,
198+
template.expression.ast`${id}.has(${buildCheckInRHS(
199+
node.right,
200+
file,
201+
)})`,
191202
);
192203
}
193204
},

0 commit comments

Comments
 (0)