forked from babel/babel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move class properties transformation logic to enanced-classes
- Loading branch information
1 parent
a2afb97
commit 5114047
Showing
16 changed files
with
788 additions
and
520 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src | ||
test | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function hasDecorators(path) { | ||
return !!(path.node.decorators && path.node.decorators.length); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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."); | ||
} | ||
} | ||
} |
Oops, something went wrong.