Skip to content

Commit

Permalink
Move class properties transformation logic to enanced-classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 20, 2018
1 parent a2afb97 commit 5114047
Show file tree
Hide file tree
Showing 16 changed files with 788 additions and 520 deletions.
3 changes: 3 additions & 0 deletions packages/babel-plugin-class-features/.npmignore
@@ -0,0 +1,3 @@
src
test
*.log
19 changes: 19 additions & 0 deletions packages/babel-plugin-class-features/README.md
@@ -0,0 +1,19 @@
# @babel/plugin-class-features

> Compile class public and private fields, private methods and decorators to ES6
See our website [@babel/plugin-class-features](https://babeljs.io/docs/en/next/babel-plugin-class-features.html) for more information.

## Install

Using npm:

```sh
npm install --save-dev @babel/plugin-class-features
```

or using yarn:

```sh
yarn add @babel/plugin-class-features --dev
```
27 changes: 27 additions & 0 deletions packages/babel-plugin-class-features/package.json
@@ -0,0 +1,27 @@
{
"name": "@babel/plugin-class-features",
"version": "7.1.4",
"author": "The Babel Team (https://babeljs.io/team)",
"license": "MIT",
"description": "Compile class public and private fields, private methods and decorators to ES6",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-class-features",
"main": "lib/index.js",
"keywords": [
"babel",
"babel-plugin"
],
"dependencies": {
"@babel/helper-function-name": "^7.1.0",
"@babel/helper-member-expression-to-functions": "^7.0.0",
"@babel/helper-optimise-call-expression": "^7.0.0",
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/helper-replace-supers": "^7.1.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
},
"devDependencies": {
"@babel/core": "^7.1.0",
"@babel/helper-plugin-test-runner": "^7.0.0"
}
}
3 changes: 3 additions & 0 deletions packages/babel-plugin-class-features/src/decorators.js
@@ -0,0 +1,3 @@
export function hasDecorators(path) {
return !!(path.node.decorators && path.node.decorators.length);
}
62 changes: 62 additions & 0 deletions packages/babel-plugin-class-features/src/features.js
@@ -0,0 +1,62 @@
import { hasDecorators } from "./decorators";

export const FEATURES = Object.freeze({
//classes: 1 << 0,
fields: 1 << 1,
privateMethods: 1 << 2,
decorators: 1 << 3,
});

// We can't use a symbol because this needs to always be the same, even if
// this package isn't deduped by npm. e.g.
// - node_modules/
// - @babel/plugin-class-features
// - @babel/plugin-proposal-decorators
// - node_modules
// - @babel-plugin-class-features
const featuresKey = "@babel/plugin-class-features/featuresKey";
const looseKey = "@babel/plugin-class-features/looseKey";

export function enableFeature(file, feature, loose) {
// We can't blindly enable the feature because, if it was already set,
// "loose" can't be changed, so that
// @babel/plugin-class-properties { loose: true }
// @babel/plugin-class-properties { loose: false }
// is transformed in loose mode.
// We only enabled the feature if it was previously disabled.
if (!hasFeature(file, feature)) {
file.set(featuresKey, file.get(featuresKey) | feature);
if (loose) file.set(looseKey, file.get(looseKey) | feature);
}
}

function hasFeature(file, feature) {
return !!(file.get(featuresKey) & feature);
}

export function isLoose(file, feature) {
return !!(file.get(looseKey) & feature);
}

export function verifyUsedFeatures(path, file) {
if (hasFeature(file, FEATURES.decorators)) {
throw new Error(
"@babel/plugin-class-features doesn't support decorators yet.",
);
}
if (hasFeature(file, FEATURES.privateMethods)) {
throw new Error(
"@babel/plugin-class-features doesn't support private methods yet.",
);
}

if (hasDecorators(path) && !hasFeature(file, FEATURES.decorators)) {
throw path.buildCodeFrameError("Decorators are not enabled.");
}

if (path.isProperty()) {
if (!hasFeature(file, FEATURES.fields)) {
throw path.buildCodeFrameError("Class fields are not enabled.");
}
}
}

0 comments on commit 5114047

Please sign in to comment.