Skip to content

Commit

Permalink
fix(babel): exports.hasOwnProperty is not a function (#1175)
Browse files Browse the repository at this point in the history
* fix: exports.hasOwnProperty is not a function

The Proxy for `this.#exports` did not forward unknown properties to the
underlying Object instance.

fixes #1140

Co-authored-by: Victor Lin <victor.lin@airbnb.com>
Co-authored-by: Anton Evzhakov <anton@evz.name>
  • Loading branch information
3 people committed Jan 13, 2023
1 parent 2d3a741 commit 860b8d2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/fair-dolphins-live.md
@@ -0,0 +1,6 @@
---
'@linaria/babel-preset': patch
'@linaria/testkit': patch
---

Ensure that the Proxy for this.#exports forwards unknown properties to the underlying Object instance.
9 changes: 8 additions & 1 deletion packages/babel/src/module.ts
Expand Up @@ -184,7 +184,14 @@ class Module {

return values;
}
const value = this.#lazyValues.get(key)?.();
let value: unknown;
if (this.#lazyValues.has(key)) {
value = this.#lazyValues.get(key)?.();
} else {
// Support Object.prototype methods on `exports`
// e.g `exports.hasOwnProperty`
value = Reflect.get(target, key);
}
this.debug('evaluated', 'get %s: %o', key, value);
return value;
},
Expand Down
Expand Up @@ -52,6 +52,7 @@ export default function prepareForRuntime(
ast: true,
babelrc: false,
configFile: false,
sourceType: 'unambiguous',
});

const result = babel.transformFromAstSync(file, code, {
Expand Down
7 changes: 5 additions & 2 deletions packages/babel/src/utils/getTagProcessor.ts
Expand Up @@ -335,8 +335,11 @@ function getBuilderForIdentifier(
...t,
addDefaultImport: (importedSource: string, nameHint?: string) =>
addDefault(path, importedSource, { importedType, nameHint }),
addNamedImport: (name: string, importedSource: string, nameHint?: string) =>
addNamed(path, name, importedSource, { importedType, nameHint }),
addNamedImport: (
name: string,
importedSource: string,
nameHint: string = name
) => addNamed(path, name, importedSource, { importedType, nameHint }),
};

return (...args: BuilderArgs) =>
Expand Down
10 changes: 10 additions & 0 deletions packages/testkit/src/module.test.ts
Expand Up @@ -133,6 +133,16 @@ it('has access to the global object', () => {
).not.toThrow();
});

it('has access to Object prototype methods on `exports`', () => {
const mod = new Module(getFileName(), options);

expect(() =>
mod.evaluate(dedent`
exports.hasOwnProperty('keyss');
`)
).not.toThrow();
});

it("doesn't have access to the process object", () => {
const mod = new Module(getFileName(), options);

Expand Down

0 comments on commit 860b8d2

Please sign in to comment.