diff options
Diffstat (limited to 'node_modules/@babel/plugin-proposal-decorators')
5 files changed, 371 insertions, 0 deletions
diff --git a/node_modules/@babel/plugin-proposal-decorators/README.md b/node_modules/@babel/plugin-proposal-decorators/README.md new file mode 100644 index 00000000..b5bf1a1b --- /dev/null +++ b/node_modules/@babel/plugin-proposal-decorators/README.md @@ -0,0 +1,126 @@ +# @babel/plugin-proposal-decorators + +> Compile class and object decorators to ES5 + +## Example + +(examples are from proposal) + +### Simple class decorator + +```js +@annotation +class MyClass { } + +function annotation(target) { + target.annotated = true; +} +``` + +### Class decorator + +```js +@isTestable(true) +class MyClass { } + +function isTestable(value) { + return function decorator(target) { + target.isTestable = value; + } +} +``` + +### Class function decorator + +```js +class C { + @enumerable(false) + method() { } +} + +function enumerable(value) { + return function (target, key, descriptor) { + descriptor.enumerable = value; + return descriptor; + } +} +``` + +## Installation + +```sh +npm install --save-dev @babel/plugin-proposal-decorators +``` + +## Usage + +Add the following line to your .babelrc file: + +```json +{ + "plugins": ["@babel/plugin-proposal-decorators"] +} +``` + +### Via CLI + +```sh +babel --plugins @babel/plugin-proposal-decorators script.js +``` + +### Via Node API + +```javascript +require("@babel/core").transform("code", { + plugins: ["@babel/plugin-proposal-decorators"] +}); +``` + +## Options + +### `legacy` + +`boolean`, defaults to `false`. + +Use the legacy (stage 1) decorators syntax and behavior. + +#### NOTE: Compatibility with `@babel/plugin-proposal-class-properties` + +If you are including your plugins manually and using `@babel/plugin-proposal-class-properties`, make sure that `@babel/plugin-proposal-decorators` comes *before* `@babel/plugin-proposal-class-properties`. + +When using the `legacy: true` mode, `@babel/plugin-proposal-class-properties` must be used in `loose` mode to support the `@babel/plugin-proposal-decorators`. + +Wrong: + +```json +{ + "plugins": [ + "@babel/plugin-proposal-class-properties", + "@babel/plugin-proposal-decorators" + ] +} +``` + +Right: + +```json +{ + "plugins": [ + "@babel/plugin-proposal-decorators", + "@babel/plugin-proposal-class-properties" + ] +} +``` + +```json +{ + "plugins": [ + ["@babel/plugin-proposal-decorators", { "legacy": true }], + ["@babel/plugin-proposal-class-properties", { "loose" : true }] + ] +} +``` + +## References + +* [Proposal: JavaScript Decorators](https://github.com/wycats/javascript-decorators/blob/master/README.md) diff --git a/node_modules/@babel/plugin-proposal-decorators/lib/index.js b/node_modules/@babel/plugin-proposal-decorators/lib/index.js new file mode 100644 index 00000000..0a3b5eb1 --- /dev/null +++ b/node_modules/@babel/plugin-proposal-decorators/lib/index.js @@ -0,0 +1,53 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; + +function _helperPluginUtils() { + const data = require("@babel/helper-plugin-utils"); + + _helperPluginUtils = function _helperPluginUtils() { + return data; + }; + + return data; +} + +function _pluginSyntaxDecorators() { + const data = _interopRequireDefault(require("@babel/plugin-syntax-decorators")); + + _pluginSyntaxDecorators = function _pluginSyntaxDecorators() { + return data; + }; + + return data; +} + +var _transformer = _interopRequireDefault(require("./transformer")); + +var _transformerLegacy = _interopRequireDefault(require("./transformer-legacy")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +var _default = (0, _helperPluginUtils().declare)((api, options) => { + api.assertVersion(7); + const _options$legacy = options.legacy, + legacy = _options$legacy === void 0 ? false : _options$legacy; + + if (typeof legacy !== "boolean") { + throw new Error("'legacy' must be a boolean."); + } + + if (legacy !== true) { + throw new Error("The new decorators proposal is not supported yet." + ' You must pass the `"legacy": true` option to' + " @babel/plugin-proposal-decorators"); + } + + return { + inherits: _pluginSyntaxDecorators().default, + visitor: legacy ? _transformerLegacy.default : _transformer.default + }; +}); + +exports.default = _default;
\ No newline at end of file diff --git a/node_modules/@babel/plugin-proposal-decorators/lib/transformer-legacy.js b/node_modules/@babel/plugin-proposal-decorators/lib/transformer-legacy.js new file mode 100644 index 00000000..1b549d4b --- /dev/null +++ b/node_modules/@babel/plugin-proposal-decorators/lib/transformer-legacy.js @@ -0,0 +1,159 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; + +function _core() { + const data = require("@babel/core"); + + _core = function _core() { + return data; + }; + + return data; +} + +const buildClassDecorator = (0, _core().template)(` + DECORATOR(CLASS_REF = INNER) || CLASS_REF; +`); +const buildClassPrototype = (0, _core().template)(` + CLASS_REF.prototype; +`); +const buildGetDescriptor = (0, _core().template)(` + Object.getOwnPropertyDescriptor(TARGET, PROPERTY); +`); +const buildGetObjectInitializer = (0, _core().template)(` + (TEMP = Object.getOwnPropertyDescriptor(TARGET, PROPERTY), (TEMP = TEMP ? TEMP.value : undefined), { + enumerable: true, + configurable: true, + writable: true, + initializer: function(){ + return TEMP; + } + }) +`); +const WARNING_CALLS = new WeakSet(); + +function applyEnsureOrdering(path) { + const decorators = (path.isClass() ? [path].concat(path.get("body.body")) : path.get("properties")).reduce((acc, prop) => acc.concat(prop.node.decorators || []), []); + const identDecorators = decorators.filter(decorator => !_core().types.isIdentifier(decorator.callee)); + if (identDecorators.length === 0) return; + return _core().types.sequenceExpression(identDecorators.map(decorator => { + const callee = decorator.callee; + const id = decorator.callee = path.scope.generateDeclaredUidIdentifier("dec"); + return _core().types.assignmentExpression("=", id, callee); + }).concat([path.node])); +} + +function applyClassDecorators(classPath) { + if (!hasClassDecorators(classPath.node)) return; + const decorators = classPath.node.decorators || []; + classPath.node.decorators = null; + const name = classPath.scope.generateDeclaredUidIdentifier("class"); + return decorators.map(dec => dec.callee).reverse().reduce(function (acc, decorator) { + return buildClassDecorator({ + CLASS_REF: _core().types.cloneNode(name), + DECORATOR: _core().types.cloneNode(decorator), + INNER: acc + }).expression; + }, classPath.node); +} + +function hasClassDecorators(classNode) { + return !!(classNode.decorators && classNode.decorators.length); +} + +function applyMethodDecorators(path, state) { + if (!hasMethodDecorators(path.node.body.body)) return; + return applyTargetDecorators(path, state, path.node.body.body); +} + +function hasMethodDecorators(body) { + return body.some(node => node.decorators && node.decorators.length); +} + +function applyObjectDecorators(path, state) { + if (!hasMethodDecorators(path.node.properties)) return; + return applyTargetDecorators(path, state, path.node.properties); +} + +function applyTargetDecorators(path, state, decoratedProps) { + const name = path.scope.generateDeclaredUidIdentifier(path.isClass() ? "class" : "obj"); + const exprs = decoratedProps.reduce(function (acc, node) { + const decorators = node.decorators || []; + node.decorators = null; + if (decorators.length === 0) return acc; + + if (node.computed) { + throw path.buildCodeFrameError("Computed method/property decorators are not yet supported."); + } + + const property = _core().types.isLiteral(node.key) ? node.key : _core().types.stringLiteral(node.key.name); + const target = path.isClass() && !node.static ? buildClassPrototype({ + CLASS_REF: name + }).expression : name; + + if (_core().types.isClassProperty(node, { + static: false + })) { + const descriptor = path.scope.generateDeclaredUidIdentifier("descriptor"); + const initializer = node.value ? _core().types.functionExpression(null, [], _core().types.blockStatement([_core().types.returnStatement(node.value)])) : _core().types.nullLiteral(); + node.value = _core().types.callExpression(state.addHelper("initializerWarningHelper"), [descriptor, _core().types.thisExpression()]); + WARNING_CALLS.add(node.value); + acc = acc.concat([_core().types.assignmentExpression("=", descriptor, _core().types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core().types.cloneNode(target), _core().types.cloneNode(property), _core().types.arrayExpression(decorators.map(dec => _core().types.cloneNode(dec.callee))), _core().types.objectExpression([_core().types.objectProperty(_core().types.identifier("enumerable"), _core().types.booleanLiteral(true)), _core().types.objectProperty(_core().types.identifier("initializer"), initializer)])]))]); + } else { + acc = acc.concat(_core().types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core().types.cloneNode(target), _core().types.cloneNode(property), _core().types.arrayExpression(decorators.map(dec => _core().types.cloneNode(dec.callee))), _core().types.isObjectProperty(node) || _core().types.isClassProperty(node, { + static: true + }) ? buildGetObjectInitializer({ + TEMP: path.scope.generateDeclaredUidIdentifier("init"), + TARGET: _core().types.cloneNode(target), + PROPERTY: _core().types.cloneNode(property) + }).expression : buildGetDescriptor({ + TARGET: _core().types.cloneNode(target), + PROPERTY: _core().types.cloneNode(property) + }).expression, _core().types.cloneNode(target)])); + } + + return acc; + }, []); + return _core().types.sequenceExpression([_core().types.assignmentExpression("=", _core().types.cloneNode(name), path.node), _core().types.sequenceExpression(exprs), _core().types.cloneNode(name)]); +} + +var _default = { + ClassDeclaration(path) { + const node = path.node; + + if (!hasClassDecorators(node) && !hasMethodDecorators(node.body.body)) { + return; + } + + const ref = node.id ? _core().types.cloneNode(node.id) : path.scope.generateUidIdentifier("class"); + + const letDeclaration = _core().types.variableDeclaration("let", [_core().types.variableDeclarator(ref, _core().types.toExpression(node))]); + + if (path.parentPath.isExportDefaultDeclaration()) { + path.parentPath.replaceWithMultiple([letDeclaration, _core().types.exportNamedDeclaration(null, [_core().types.exportSpecifier(_core().types.cloneNode(ref), _core().types.identifier("default"))])]); + } else { + path.replaceWith(letDeclaration); + } + }, + + ClassExpression(path, state) { + const decoratedClass = applyEnsureOrdering(path) || applyClassDecorators(path, state) || applyMethodDecorators(path, state); + if (decoratedClass) path.replaceWith(decoratedClass); + }, + + ObjectExpression(path, state) { + const decoratedObject = applyEnsureOrdering(path) || applyObjectDecorators(path, state); + if (decoratedObject) path.replaceWith(decoratedObject); + }, + + AssignmentExpression(path, state) { + if (!WARNING_CALLS.has(path.node.right)) return; + path.replaceWith(_core().types.callExpression(state.addHelper("initializerDefineProperty"), [_core().types.cloneNode(path.get("left.object").node), _core().types.stringLiteral(path.get("left.property").node.name), _core().types.cloneNode(path.get("right.arguments")[0].node), _core().types.cloneNode(path.get("right.arguments")[1].node)])); + } + +}; +exports.default = _default;
\ No newline at end of file diff --git a/node_modules/@babel/plugin-proposal-decorators/lib/transformer.js b/node_modules/@babel/plugin-proposal-decorators/lib/transformer.js new file mode 100644 index 00000000..4e7a7b3f --- /dev/null +++ b/node_modules/@babel/plugin-proposal-decorators/lib/transformer.js @@ -0,0 +1,8 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; +var _default = {}; +exports.default = _default;
\ No newline at end of file diff --git a/node_modules/@babel/plugin-proposal-decorators/package.json b/node_modules/@babel/plugin-proposal-decorators/package.json new file mode 100644 index 00000000..1a35ad49 --- /dev/null +++ b/node_modules/@babel/plugin-proposal-decorators/package.json @@ -0,0 +1,25 @@ +{ + "name": "@babel/plugin-proposal-decorators", + "version": "7.0.0-beta.47", + "author": "Logan Smyth <loganfsmyth@gmail.com>", + "license": "MIT", + "description": "Compile class and object decorators to ES5", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-decorators", + "main": "lib/index.js", + "keywords": [ + "babel", + "babel-plugin", + "decorators" + ], + "dependencies": { + "@babel/helper-plugin-utils": "7.0.0-beta.47", + "@babel/plugin-syntax-decorators": "7.0.0-beta.47" + }, + "peerDependencies": { + "@babel/core": "7.0.0-beta.47" + }, + "devDependencies": { + "@babel/core": "7.0.0-beta.47", + "@babel/helper-plugin-test-runner": "7.0.0-beta.47" + } +} |
