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
Merge class features plugins #8130
Merged
nicolo-ribaudo
merged 1 commit into
babel:master
from
nicolo-ribaudo:merge-class-plugins
Nov 20, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is auto generated btw, see the script in
scripts
.