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

extract more test helpers to repo-utils #15902

Merged
merged 8 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 1 addition & 7 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,13 +736,7 @@ function pluginImportMetaUrl({ types: t, template }) {

if (binding.path.isImportSpecifier()) {
path.parentPath.parentPath.assertVariableDeclaration();
const { parentPath } = path;
parentPath.replaceWith(
t.variableDeclarator(
parentPath.node.id,
template.expression.ast`{ __dirname, __filename, require }`
)
);
path.parentPath.parentPath.remove();
}
},

Expand Down
3 changes: 2 additions & 1 deletion packages/babel-core/test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import pluginSyntaxJSX from "@babel/plugin-syntax-jsx";
import pluginFlowStripTypes from "@babel/plugin-transform-flow-strip-types";
import { itBabel8, commonJS } from "$repo-utils";

const { __dirname: cwd } = commonJS(import.meta.url);
const { __dirname } = commonJS(import.meta.url);
const cwd = __dirname;
Comment on lines +15 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would supporting const cwd = commonJS(import.meta.url).__dirname make the plugin significantly more complex?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current babelImportMetaUrl plugin removes const { __dirname } = commonJS(import.meta.url) and let __dirname references the global variable, so it breaks when we rename the variable in destructuring. At first I replaced commonJS(import.meta.url) by { __dirname, __filename, require } but the test throws with _dirname (one underscore) not defined, I guess it is a glitch in the replacer as it renames __dirname to _dirname (incorrectly). I will investigate this issue later.

Since it is very likely that we will get rid of the custom babelImportMetaUrl in Babel 8, I workaround this issue and just keep this PR mergeable.


function assertIgnored(result) {
expect(result).toBeNull();
Expand Down