Skip to content

Commit

Permalink
Make @babel/plugin-class-features a normal helper package (#9083)
Browse files Browse the repository at this point in the history
* Make @babel/plugin-class-features a normal helper package

This effectively disallows using it directly.

* Rename helper

* Style

* Don't add prefix to plugin name

* Move private methods plugin
  • Loading branch information
nicolo-ribaudo committed Nov 29, 2018
1 parent c4d6f6d commit 4e28459
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 79 deletions.
@@ -1,5 +1,5 @@
{
"name": "@babel/plugin-class-features",
"name": "@babel/helper-create-class-features-plugin",
"version": "7.1.4",
"author": "The Babel Team (https://babeljs.io/team)",
"license": "MIT",
Expand Down
@@ -1,4 +1,3 @@
import { declare } from "@babel/helper-plugin-utils";
import nameFunction from "@babel/helper-function-name";
import { types as t } from "@babel/core";
import {
Expand All @@ -12,13 +11,12 @@ import {
enableFeature,
verifyUsedFeatures,
FEATURES,
setLoose,
isLoose,
} from "./features";

import pkg from "../package.json";

export { enableFeature, FEATURES, setLoose };
export { FEATURES };

// Note: Versions are represented as an integer. e.g. 7.1.5 is represented
// as 70000100005. This method is easier than using a semver-parsing
Expand All @@ -27,60 +25,22 @@ export { enableFeature, FEATURES, setLoose };
const version = pkg.version.split(".").reduce((v, x) => v * 1e5 + +x, 0);
const versionKey = "@babel/plugin-class-features/version";

const getFeatureOptions = (options, name) => {
const value = options[name];

if (value === undefined || value === false) return { enabled: false };
if (value === true) return { enabled: true, loose: false };

if (typeof value === "object") {
if (
typeof value.loose !== "undefined" &&
typeof value.loose !== "boolean"
) {
throw new Error(`.${name}.loose must be a boolean or undefined.`);
}

return { enabled: true, loose: !!value.loose };
}

throw new Error(
`.${name} must be a boolean, an object with a 'loose'` +
` property or undefined.`,
);
};

export default declare((api, options) => {
api.assertVersion(7);

const fields = getFeatureOptions(options, "fields");
const privateMethods = getFeatureOptions(options, "privateMethods");
const decorators = getFeatureOptions(options, "decorators");

export function createClassFeaturePlugin({
name,
feature,
loose,
manipulateOptions,
}) {
return {
name: "class-features",

manipulateOptions(opts, parserOpts) {
if (fields) {
parserOpts.plugins.push("classProperties", "classPrivateProperties");
}
},
name,
manipulateOptions,

pre() {
enableFeature(this.file, feature, loose);

if (!this.file.get(versionKey) || this.file.get(versionKey) < version) {
this.file.set(versionKey, version);
}

if (fields.enabled) {
enableFeature(this.file, FEATURES.fields, fields.loose);
}
if (privateMethods.enabled) {
enableFeature(this.file, FEATURES.privateMethods);
}
if (decorators.enabled) {
throw new Error("Decorators are not supported yet");
enableFeature(this.file, FEATURES.decorators);
}
},

visitor: {
Expand Down Expand Up @@ -203,4 +163,4 @@ export default declare((api, options) => {
},
},
};
});
}
Expand Up @@ -13,7 +13,7 @@
],
"dependencies": {
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/plugin-class-features": "^7.1.4"
"@babel/helper-create-class-features-plugin": "^7.1.4"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
Expand Down
21 changes: 9 additions & 12 deletions packages/babel-plugin-proposal-class-properties/src/index.js
@@ -1,25 +1,22 @@
/* eslint-disable local-rules/plugin-name */

import { declare } from "@babel/helper-plugin-utils";
import pluginClassFeatures, {
enableFeature,
import {
createClassFeaturePlugin,
FEATURES,
} from "@babel/plugin-class-features";
} from "@babel/helper-create-class-features-plugin";

export default declare((api, options) => {
api.assertVersion(7);

const { loose } = options;

return {
return createClassFeaturePlugin({
name: "proposal-class-properties",

inherits: pluginClassFeatures,
feature: FEATURES.fields,
loose: options.loose,

manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("classProperties", "classPrivateProperties");
},

pre() {
enableFeature(this.file, FEATURES.fields, loose);
},
};
});
});
Expand Up @@ -13,7 +13,7 @@
],
"dependencies": {
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/plugin-class-features": "^7.1.4"
"@babel/helper-create-class-features-plugin": "^7.1.4"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
Expand Down
21 changes: 9 additions & 12 deletions packages/babel-plugin-proposal-private-methods/src/index.js
@@ -1,25 +1,22 @@
/* eslint-disable local-rules/plugin-name */

import { declare } from "@babel/helper-plugin-utils";
import pluginClassFeatures, {
enableFeature,
import {
createClassFeaturePlugin,
FEATURES,
} from "@babel/plugin-class-features";
} from "@babel/helper-create-class-features-plugin";

export default declare((api, options) => {
api.assertVersion(7);

const { loose } = options;

return {
return createClassFeaturePlugin({
name: "proposal-private-methods",

inherits: pluginClassFeatures,
feature: FEATURES.privateMethods,
loose: options.loose,

manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("classPrivateMethods");
},

pre() {
enableFeature(this.file, FEATURES.privateMethods, loose);
},
};
});
});

0 comments on commit 4e28459

Please sign in to comment.