sadpandajoe commented on code in PR #45595: URL: https://github.com/apache/superset/pull/45595#discussion_r4372283142
########## superset-frontend/spec/helpers/swcJestTransformer.js: ########## @@ -0,0 +1,135 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { createTransformer as createSwcTransformer } from '@swc/jest'; + +/** + * Jest transformer: @swc/jest plus one post-processing step. + * + * SWC's CommonJS output defines every ESM export as a lazy, non-configurable + * getter, which is why `jest.spyOn(module, 'name')` and + * `jest.replaceProperty(module, 'name', value)` throw "Cannot redefine + * property" on anything SWC transformed. Babel's output defined plain + * properties, so the suite relies on spying on module exports everywhere. + * This wrapper marks those getters `configurable: true` so the spies keep + * working, while leaving the getters lazy (an eager rewrite would break + * modules that import each other in a cycle). + * + * The second difference is what a module in an import cycle sees when it + * reads an export that has not been initialised yet. Babel assigns + * `exports.name = void 0` up front, so the read yields `undefined`; SWC's + * getter returns the live binding and throws a ReferenceError (temporal dead + * zone) instead. Several suites spread `jest.requireActual()` of a module + * that is still mid-evaluation, so the getters are made lenient to match + * Babel: an uninitialised export reads as `undefined`. + */ +// SWC keeps a leading doc comment of a default export inside the call, as +// `Object.defineProperty(exports, /** ... */ "default", {`, hence the optional +// block comment before the name. +const EXPORT_GETTER_DESCRIPTOR = + /(Object\.defineProperty\((?:target, name|to, k|exports,\s*(?:\/\*[\s\S]*?\*\/\s*)?"[^"]+"), \{\s*enumerable: true,)(\s*)(get:)/g; +// `_export(exports, { get name () { return binding; }, ... });` is how SWC +// declares named exports; the leniency below is scoped to this block and to +// the `Object.defineProperty(exports, ...)` descriptors, never to getters in +// application code. +const EXPORT_BLOCK = /_export\(exports, \{[\s\S]*?\n\}\);/g; +const EXPORT_BLOCK_GETTER = + /(get [\w$]+ \(\) \{\s*)return ([\w$]+(?:\.[\w$]+)*);/g; +// The `get: function() { return binding; }` form SWC uses for a single export +// or a re-export (`from[k]`), matched together with its descriptor. +const EXPORT_DESCRIPTOR_GETTER = + /(Object\.defineProperty\((?:target, name|to, k|exports,\s*(?:\/\*[\s\S]*?\*\/\s*)?"[^"]+"), \{\s*enumerable: true,\s*)(get: function\(\) \{\s*)return ([\w$]+(?:\.[\w$]+)*|from\[k\]);/g; Review Comment: A default export preceded by a `//` comment is not matched here, only a `/* */` one is. SWC emits e.g. `SuperChart.tsx` (`// Wrap in memo…` above `export default memo(SuperChart)`) as `Object.defineProperty(exports, // Wrap in memo…\n"default", {`, so neither this pattern nor `EXPORT_GETTER_DESCRIPTOR` rewrites it. That export stays non-configurable and non-lenient, so `jest.spyOn(module, 'default')` throws `Cannot redefine property: default` and a cyclic read can still hit the ReferenceError. The no-match guards don't fire either, since these single-export modules have no `_export(` call. Could the optional comment group accept line comments too, and could the guard also cover the direct `Object.defineProperty(exports, ...)` form? ########## superset-frontend/spec/helpers/swcJestTransformer.test.js: ########## @@ -0,0 +1,110 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { mirrorBabelExportSemantics } from './swcJestTransformer'; + +// The export-helper text is interpolated so this file is not itself rewritten +// by the transformer when Jest loads it; the fixtures stay raw SWC-shaped input. +const EXPORT_FN = '_export'; +const DEFINE = 'defineProperty'; +const GET = 'get'; +const helper = `function ${EXPORT_FN}(target, all) { + for(var name in all)Object.${DEFINE}(target, name, { + enumerable: true, + ${GET}: Object.getOwnPropertyDescriptor(all, name).get + }); +}`; +const exportsBlock = body => `${EXPORT_FN}(exports, { +${body} +});`; + +const NAMED_EXPORTS = `"use strict"; +Object.${DEFINE}(exports, "__esModule", { + value: true +}); +${helper} +${exportsBlock(` ${GET} answer () { + return answer; + }`)} +const answer = 42; +`; + +function load(code) { + const mod = { exports: {} }; + // eslint-disable-next-line no-new-func + new Function('module', 'exports', code)(mod, mod.exports); + return mod.exports; +} + +test('named exports become configurable so jest.spyOn works', () => { + const exports = load(mirrorBabelExportSemantics(NAMED_EXPORTS)); Review Comment: All five tests build their fixtures from `_export(exports, { get … })`, so `EXPORT_DESCRIPTOR_GETTER` (the `get: function() { return x; }` form used for default exports and `from[k]` re-exports) is never exercised. Deleting that `.replace` leaves this file green while a default export or re-export in a cycle would throw again. Could you add a fixture with a doc-commented `"default"` descriptor and a `Object.defineProperty(to, k, … from[k])` re-export, then assert it is configurable and reads `undefined` before initialisation? `createTransformer` (async path, string vs object result, cache salt) is likewise untested. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
