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

Skip inherited properties in synthetic namespaces #4256

Merged
merged 2 commits into from
Nov 1, 2021
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
80 changes: 51 additions & 29 deletions src/utils/interopHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,21 @@ const HELPER_GENERATORS: {
);
},
[MERGE_NAMESPACES_VARIABLE](t, snippets, liveBindings, freeze) {
const { _, n } = snippets;
const { _, cnst, n } = snippets;
const useForEach = cnst === 'var' && liveBindings;
return (
`function ${MERGE_NAMESPACES_VARIABLE}(n, m)${_}{${n}` +
`${t}${loopOverNamespaces(
`{${n}` +
`${t}${t}${t}if${_}(k${_}!==${_}'default'${_}&&${_}!(k in n))${_}{${n}` +
(liveBindings ? copyPropertyLiveBinding : copyPropertyStatic)(
t,
t + t + t + t,
snippets
) +
(liveBindings
? useForEach
? copyOwnPropertyLiveBinding
: copyPropertyLiveBinding
: copyPropertyStatic)(t, t + t + t + t, snippets) +
`${t}${t}${t}}${n}` +
`${t}${t}}`,
!liveBindings,
useForEach,
t,
snippets
)}${n}` +
Expand All @@ -202,7 +203,7 @@ const createNamespaceObject = (
const { _, cnst, getPropertyAccess, n, s } = snippets;
const copyProperty =
`{${n}` +
(liveBindings ? copyNonDefaultPropertyLiveBinding : copyPropertyStatic)(
(liveBindings ? copyNonDefaultOwnPropertyLiveBinding : copyPropertyStatic)(
t,
i + t + t,
snippets
Expand Down Expand Up @@ -236,48 +237,48 @@ const loopOverKeys = (

const loopOverNamespaces = (
body: string,
allowVarLoopVariable: boolean,
useForEach: boolean,
t: string,
{ _, cnst, getDirectReturnFunction, getFunctionIntro, n }: GenerateCodeSnippets
) => {
if (cnst !== 'var' || allowVarLoopVariable) {
if (useForEach) {
const [left, right] = getDirectReturnFunction(['e'], {
functionReturn: false,
lineBreakIndent: { base: t, t },
name: null
});
return (
`for${_}(var i${_}=${_}0;${_}i${_}<${_}m.length;${_}i++)${_}{${n}` +
`${t}${t}${cnst} e${_}=${_}m[i];${n}` +
`${t}${t}if${_}(typeof e${_}!==${_}'string'${_}&&${_}!Array.isArray(e))${_}{${_}for${_}(${cnst} k in e)${_}${body}${_}}${n}${t}}`
`m.forEach(${left}` +
`e${_}&&${_}typeof e${_}!==${_}'string'${_}&&${_}!Array.isArray(e)${_}&&${_}Object.keys(e).forEach(${getFunctionIntro(
['k'],
{
isAsync: false,
name: null
}
)}${body})${right});`
);
}
const [left, right] = getDirectReturnFunction(['e'], {
functionReturn: false,
lineBreakIndent: { base: t, t },
name: null
});
return (
`m.forEach(${left}` +
`e${_}&&${_}typeof e${_}!==${_}'string'${_}&&${_}!Array.isArray(e)${_}&&${_}Object.keys(e).forEach(${getFunctionIntro(
['k'],
{
isAsync: false,
name: null
}
)}${body})${right});`
`for${_}(var i${_}=${_}0;${_}i${_}<${_}m.length;${_}i++)${_}{${n}` +
`${t}${t}${cnst} e${_}=${_}m[i];${n}` +
`${t}${t}if${_}(typeof e${_}!==${_}'string'${_}&&${_}!Array.isArray(e))${_}{${_}for${_}(${cnst} k in e)${_}${body}${_}}${n}${t}}`
);
};

const copyNonDefaultPropertyLiveBinding = (
const copyNonDefaultOwnPropertyLiveBinding = (
t: string,
i: string,
snippets: GenerateCodeSnippets
) => {
const { _, n } = snippets;
return (
`${i}if${_}(k${_}!==${_}'default')${_}{${n}` +
copyPropertyLiveBinding(t, i + t, snippets) +
copyOwnPropertyLiveBinding(t, i + t, snippets) +
`${i}}${n}`
);
};

const copyPropertyLiveBinding = (
const copyOwnPropertyLiveBinding = (
t: string,
i: string,
{ _, cnst, getDirectReturnFunction, n }: GenerateCodeSnippets
Expand All @@ -296,6 +297,27 @@ const copyPropertyLiveBinding = (
);
};

const copyPropertyLiveBinding = (
t: string,
i: string,
{ _, cnst, getDirectReturnFunction, n }: GenerateCodeSnippets
) => {
const [left, right] = getDirectReturnFunction([], {
functionReturn: true,
lineBreakIndent: null,
name: null
});
return (
`${i}${cnst} d${_}=${_}Object.getOwnPropertyDescriptor(e,${_}k);${n}` +
`${i}if${_}(d)${_}{${n}` +
`${i}${t}Object.defineProperty(n,${_}k,${_}d.get${_}?${_}d${_}:${_}{${n}` +
`${i}${t}${t}enumerable:${_}true,${n}` +
`${i}${t}${t}get:${_}${left}e[k]${right}${n}` +
`${i}${t}});${n}` +
`${i}}${n}`
);
};

const copyPropertyStatic = (_t: string, i: string, { _, n }: GenerateCodeSnippets) =>
`${i}n[k]${_}=${_}e[k];${n}`;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const foo = 'bar';
export const __synthetic = Object.create({ inherited: 'ignored' });
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as fromNull from './null';
import * as fromString from './string';
import * as fromArray from './array';
import * as fromInherited from './inherited';

assert.deepStrictEqual(fromNull, { __proto__: null, foo: 'bar' });
assert.deepStrictEqual(fromString, { __proto__: null, foo: 'bar' });
assert.deepStrictEqual(fromArray, { __proto__: null, foo: 'bar' });
assert.deepStrictEqual(fromInherited, { __proto__: null, foo: 'bar' });
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const foo = 'bar';
export const __synthetic = Object.create({ inherited: 'ignored' });
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as fromNull from './null';
import * as fromString from './string';
import * as fromArray from './array';
import * as fromInherited from './inherited';

assert.deepStrictEqual(fromNull, { __proto__: null, foo: 'bar' });
assert.deepStrictEqual(fromString, { __proto__: null, foo: 'bar' });
assert.deepStrictEqual(fromArray, { __proto__: null, foo: 'bar' });
assert.deepStrictEqual(fromInherited, { __proto__: null, foo: 'bar' });