Skip to content
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

@W-15173926@ [LWS][248] window.top.location and window.location trans… #15

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions packages/babel-plugin-transform-unforgeables/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,23 @@ export function transformUnforgeables() {
MemberExpression(path: NodePath<BabelTypes.MemberExpression>) {
const { node } = path;
if (isLocationProperty(path)) {
const objectPath = path.get('object');
// Skip transforming location in members like `window.top.location`
// letting the transform to `window.top` carry it.
if (isTopProperty(objectPath)) {
return;
}
const alternatePath = objectPath.isConditionalExpression()
? objectPath.get('alternate')
: undefined;
// Skip re-transforming location in members like
// `(window === GLOBAL_THIS ? TOP : window.top).location`.
if (
alternatePath &&
isTopProperty(alternatePath) &&
topMemberTransformBuilder.isTransformed(alternatePath)
) {
return;
let currentNode = node.object;
// For scenarios with `window.top.location`,
// We want to have nested transformations.
if (isTopProperty(path.get('object'))) {
let currPath;
let nextPath = path;
do {
currPath = nextPath;
currPath.skip();
// Walk into nested top member expressions like
// window.top.top -> window.top -> window
nextPath = currPath.get('object') as NodePath<any>;
} while (isTopProperty(nextPath));
currentNode = topMemberTransformBuilder({
EXPRESSION: node.object,
NODE: currPath.node.object,
});
}
if (isLeftOfAssignment(path)) {
const parent = path.parent as BabelTypes.AssignmentExpression;
Expand All @@ -96,12 +96,11 @@ export function transformUnforgeables() {
const { parentPath } = path;
// Skip re-transforming location.
if (!builder.isTransformed(parentPath)) {
// Both transform variations have the same
// placeholder replacements.
parentPath.replaceWith(
builder({
EXPRESSION: parent,
NODE: node.object,
EXPRESSION_OBJECT: currentNode,
EXPRESSION_PROPERTY: node.property,
NODE: currentNode,
VALUE: parent.right,
})
);
Expand All @@ -116,8 +115,9 @@ export function transformUnforgeables() {
) {
path.replaceWith(
locationMemberTransformBuilder({
EXPRESSION: node,
NODE: node.object,
EXPRESSION_OBJECT: currentNode,
EXPRESSION_PROPERTY: node.property,
NODE: currentNode,
})
);
path.skip();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const locationMemberAssignTransform = () =>
// NODE.location = VALUE
Builder.create(
`
(NODE === GLOBAL_THIS || NODE === DOCUMENT) ? LOCATION.assign(VALUE) : EXPRESSION
(NODE === GLOBAL_THIS || NODE === DOCUMENT) ? LOCATION.assign(VALUE) : EXPRESSION_OBJECT.EXPRESSION_PROPERTY = VALUE
`,
{
name: 'locationMemberAssignTransform',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const locationMemberConcatTransform = () =>
// NODE.location += VALUE
Builder.create(
`
(NODE === GLOBAL_THIS || NODE === DOCUMENT) ? LOCATION.assign(LOCATION.href + VALUE) : EXPRESSION
(NODE === GLOBAL_THIS || NODE === DOCUMENT) ? LOCATION.assign(LOCATION.href + VALUE) : EXPRESSION_OBJECT.EXPRESSION_PROPERTY += VALUE
`,
{
name: 'locationMemberConcatTransform',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const locationMemberTransform = () =>
// NODE.location
Builder.create(
`
(NODE === GLOBAL_THIS || NODE === DOCUMENT) ? LOCATION : EXPRESSION
(NODE === GLOBAL_THIS || NODE === DOCUMENT) ? LOCATION : EXPRESSION_OBJECT.EXPRESSION_PROPERTY
`,
{
name: 'locationMemberTransform',
Expand Down
Loading
Loading