Skip to content

Commit

Permalink
feat(transform-conformance): support enabling all class-related plugi…
Browse files Browse the repository at this point in the history
…ns when any of them are enabled in update_fixtures (#8200)

The `class-properties` plugin supports all class-related plugins, so we need to ensure that once any of them are enabled, we also enable all class-related plugins.
  • Loading branch information
Dunqing committed Dec 31, 2024
1 parent ad77ad5 commit 3c77ed1
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
54 changes: 54 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tasks/transform_conformance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"@babel/plugin-transform-logical-assignment-operators": "^7.25.9",
"@babel/plugin-transform-optional-chaining": "^7.25.9",
"@babel/plugin-transform-private-methods": "^7.25.9",
"@babel/plugin-transform-private-property-in-object": "^7.25.9",
"@babel/runtime": "^7.26.0"
},
"dependencies": {
"@babel/plugin-transform-typescript": "^7.26.3"
}
}
60 changes: 60 additions & 0 deletions tasks/transform_conformance/update_fixtures.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { extname, join as pathJoin } from 'path';
const PACKAGES = [
'babel-plugin-transform-class-properties',
'babel-plugin-transform-private-methods',
'babel-plugin-transform-private-property-in-object',
'babel-plugin-transform-logical-assignment-operators',
];
const FILTER_OUT_PRESETS = ['env'];
Expand All @@ -28,8 +29,20 @@ const FILTER_OUT_PLUGINS = [
'transform-destructuring',
];

const CLASS_PLUGINS = [
'transform-class-properties',
'transform-private-methods',
'transform-private-property-in-object',
];

const PACKAGES_PATH = pathJoin(import.meta.dirname, '../coverage/babel/packages');

// These fixtures transform incorrectly by Babel. Haven't figured out why yet.
const IGNORED_FIXTURES = [
'compile-to-class/constructor-collision-ignores-types',
'compile-to-class/constructor-collision-ignores-types-loose',
];

// Copied from `@babel/helper-transform-fixture-test-runner`
const EXTERNAL_HELPERS_VERSION = '7.100.0';

Expand All @@ -46,6 +59,10 @@ for (const packageName of PACKAGES) {
* @returns {undefined}
*/
async function updateDir(dirPath, options, hasChangedOptions) {
if (IGNORED_FIXTURES.some(p => dirPath.endsWith(p))) {
return;
}

const files = await readdir(dirPath, { withFileTypes: true });

const dirFiles = [],
Expand Down Expand Up @@ -118,10 +135,48 @@ function updateOptions(options) {

filter('presets', FILTER_OUT_PRESETS);
filter('plugins', FILTER_OUT_PLUGINS);
if (ensureAllClassPluginsEnabled(options)) {
hasChangedOptions = true;
}

return hasChangedOptions;
}

// Ensure all class plugins are enabled if any of class related plugins are enabled
function ensureAllClassPluginsEnabled(options) {
let plugins = options.plugins;
if (!plugins) return false;

let already_enabled = [];
let pluginOptions;
plugins.forEach(plugin => {
let pluginName = getName(plugin);
if (CLASS_PLUGINS.includes(pluginName)) {
if (Array.isArray(plugin) && plugin[1]) {
// Store options for the plugin, so that we can ensure all plugins are
// enabled with the same options
pluginOptions = plugin[1];
}
already_enabled.push(pluginName);
}
});

if (already_enabled.length) {
CLASS_PLUGINS.forEach(pluginName => {
if (!already_enabled.includes(pluginName)) {
if (pluginOptions) {
plugins.push([pluginName, pluginOptions]);
} else {
plugins.push(pluginName);
}
}
});
return true;
} else {
return false;
}
}

/**
* Transform input with Babel and save to output file.
* @param {string} inputPath - Path of input file
Expand All @@ -135,8 +190,13 @@ async function transform(inputPath, options) {
babelrc: false,
cwd: import.meta.dirname,
};
delete options.BABEL_8_BREAKING;
delete options.validateLogs;
delete options.SKIP_ON_PUBLISH;
delete options.SKIP_babel7plugins_babel8core;
delete options.minNodeVersion;
delete options.validateLogs;
delete options.SKIP_ON_PUBLISH;

function prefixName(plugin, type) {
if (Array.isArray(plugin)) {
Expand Down

0 comments on commit 3c77ed1

Please sign in to comment.