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

handling for spec/loose default changes for template literals and class properties #47

Closed
wants to merge 2 commits into from
Closed
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
104 changes: 101 additions & 3 deletions __tests__/__snapshots__/upgradeConfig.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ Object {
},
"plugins": Array [
"@babel/plugin-proposal-function-bind",
"@babel/plugin-proposal-class-properties",
Array [
"@babel/plugin-proposal-class-properties",
Object {
"loose": true,
},
],
],
"presets": Array [
"@babel/preset-env",
Expand Down Expand Up @@ -83,7 +88,12 @@ Object {
"useBuiltIns": true,
},
],
"@babel/plugin-proposal-class-properties",
Array [
"@babel/plugin-proposal-class-properties",
Object {
"loose": true,
},
],
],
"presets": Array [
"@babel/preset-env",
Expand Down Expand Up @@ -123,7 +133,12 @@ Object {
"useBuiltIns": true,
},
],
"@babel/plugin-proposal-class-properties",
Array [
"@babel/plugin-proposal-class-properties",
Object {
"loose": true,
},
],
],
"presets": Array [
"@babel/preset-env",
Expand Down Expand Up @@ -152,3 +167,86 @@ Object {
],
}
`;

exports[`sets \`loose\` option on plugins that are now spec by default if needed 1`] = `
Object {
"plugins": Array [
Array [
"@babel/plugin-proposal-class-properties",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-proposal-class-properties",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-transform-template-literals",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-transform-template-literals",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-proposal-class-properties",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-proposal-class-properties",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-transform-template-literals",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-transform-template-literals",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-proposal-class-properties",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-proposal-class-properties",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-transform-template-literals",
Object {
"loose": true,
},
],
Array [
"@babel/plugin-transform-template-literals",
Object {
"loose": true,
},
],
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-template-literals",
"@babel/plugin-transform-template-literals",
],
}
`;
23 changes: 23 additions & 0 deletions __tests__/upgradeConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@ test('rename community packages', () => {
})).toMatchSnapshot();
});

test('sets `loose` option on plugins that are now spec by default if needed', () => {
expect(upgradeConfig({
plugins: [
'babel-plugin-transform-class-properties',
'@babel/plugin-transform-class-properties',
'babel-plugin-transform-es2015-template-literals',
'@babel/plugin-transform-es2015-template-literals',
['babel-plugin-transform-class-properties'],
['@babel/plugin-transform-class-properties'],
['babel-plugin-transform-es2015-template-literals'],
['@babel/plugin-transform-es2015-template-literals'],
['babel-plugin-transform-class-properties', { spec: false }],
['@babel/plugin-transform-class-properties', { spec: false }],
['babel-plugin-transform-es2015-template-literals', { spec: false }],
['@babel/plugin-transform-es2015-template-literals', { spec: false }],
['babel-plugin-transform-class-properties', { spec: true }],
['@babel/plugin-transform-class-properties', { spec: true }],
['babel-plugin-transform-es2015-template-literals', { spec: true }],
['@babel/plugin-transform-es2015-template-literals', { spec: true }]
]
})).toMatchSnapshot();
});

test('packages (json5)', async () => {
const json5Data = await readBabelRC(JSON5_PATH);
expect(upgradeConfig(json5Data)).toMatchSnapshot();
Expand Down
6 changes: 6 additions & 0 deletions src/packageData.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,15 @@ const packages = Object.assign(

const latestPackages = new Set(Object.values(packages));

const pluginsNowSpecByDefault = new Set([
'@babel/plugin-transform-template-literals',
'@babel/plugin-proposal-class-properties'
]);

module.exports = {
packages,
presets,
plugins,
latestPackages,
pluginsNowSpecByDefault
};
38 changes: 34 additions & 4 deletions src/upgradeConfig.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { presets: oldPresets, plugins: oldPlugins } = require('./packageData');
const { presets: oldPresets, plugins: oldPlugins, pluginsNowSpecByDefault } = require('./packageData');

const unhandledPluginsNowSpecByDefault = new Set(pluginsNowSpecByDefault);

// TODO: fix all of this
function changePresets(config, options = {}) {
Expand Down Expand Up @@ -72,19 +74,19 @@ function changePlugins(config) {

if (pluginsToReplace.includes(plugin[0])) {
if (oldPlugins[plugin[0]]) {
plugin[0] = oldPlugins[plugin[0]];
plugins[i] = setLooseIfNeeded([oldPlugins[plugin[0]], plugin[1]]);
} else {
plugins.splice(i, 1);
i--;
}
}
} else {
if (plugin.indexOf('babel-plugin') !== 0 && plugin[0].indexOf('@babel/') !== 0) {
Copy link
Member Author

Choose a reason for hiding this comment

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

plugin[0].indexOf looked like a bug -- was preventing @babel/ prefixes from being recognized correctly

Copy link
Member Author

Choose a reason for hiding this comment

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

submitting this as a separate PR with updated tests in #48

if (plugin.indexOf('babel-plugin') !== 0 && plugin.indexOf('@babel/') !== 0) {
plugin = `babel-plugin-${plugin}`;
}
if (pluginsToReplace.includes(plugin)) {
if (oldPlugins[plugin]) {
plugins[i] = oldPlugins[plugin];
plugins[i] = setLooseIfNeeded(oldPlugins[plugin]);
} else {
plugins.splice(i, 1);
i--;
Expand All @@ -95,6 +97,30 @@ function changePlugins(config) {
}
}

function buildPluginGithubUrl(plugin) {
return `https://github.com/babel/babel/tree/master/packages/${plugin.replace('@', '').replace('/', '-')}`;
}

function setLooseIfNeeded(plugin) {
if (Array.isArray(plugin)) {
if (pluginsNowSpecByDefault.has(plugin[0])) {
plugin[1] = plugin[1] || {};
if (!plugin[1].spec) {
Object.assign(plugin[1], { loose: true });
}
delete plugin[1].spec;
}
unhandledPluginsNowSpecByDefault.delete(plugin[0]);
return Object.keys(plugin[1]).length > 0 ? plugin : plugin[0];
} else {
if (pluginsNowSpecByDefault.has(plugin)) {
plugin = [plugin, { loose: true }];
}
unhandledPluginsNowSpecByDefault.delete(plugin);
return plugin;
}
}

module.exports = function upgradeConfig(config, options) {
config = Object.assign({}, config);

Expand All @@ -108,5 +134,9 @@ module.exports = function upgradeConfig(config, options) {
});
}

unhandledPluginsNowSpecByDefault.forEach(plugin => {
console.log(`\`${plugin}\` is now spec by default. For parity with Babel 6, use the \`loose\` flag (${buildPluginGithubUrl(plugin).concat('#loose')})`)
});

return config;
};