diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/@babel/plugin-transform-computed-properties | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/@babel/plugin-transform-computed-properties')
3 files changed, 376 insertions, 0 deletions
diff --git a/node_modules/@babel/plugin-transform-computed-properties/README.md b/node_modules/@babel/plugin-transform-computed-properties/README.md new file mode 100644 index 00000000..0a420ba0 --- /dev/null +++ b/node_modules/@babel/plugin-transform-computed-properties/README.md @@ -0,0 +1,130 @@ +# @babel/plugin-transform-computed-properties + +> Compile ES2015 computed properties to ES5 + +## Example + +**In** + +```js +var obj = { + ["x" + foo]: "heh", + ["y" + bar]: "noo", + foo: "foo", + bar: "bar" +}; +``` + +**Out** + +```js +var _obj; + +function _defineProperty(obj, key, value) { + if (key in obj) { + Object.defineProperty(obj, key, { + value: value, + enumerable: true, + configurable: true, + writable: true + }); + } else { + obj[key] = value; + } + + return obj; +} + +var obj = ( + _obj = {}, + _defineProperty(_obj, "x" + foo, "heh"), + _defineProperty(_obj, "y" + bar, "noo"), + _defineProperty(_obj, "foo", "foo"), + _defineProperty(_obj, "bar", "bar"), + _obj +); +``` + +## Installation + +```sh +npm install --save-dev @babel/plugin-transform-computed-properties +``` + +## Usage + +### Via `.babelrc` (Recommended) + +**.babelrc** + +Without options: + +```json +{ + "plugins": ["@babel/plugin-transform-computed-properties"] +} +``` + +With options: + +```json +{ + "plugins": [ + ["@babel/plugin-transform-computed-properties", { + "loose": true + }] + ] +} +``` + +### Via CLI + +```sh +babel --plugins @babel/plugin-transform-computed-properties script.js +``` + +### Via Node API + +```javascript +require("@babel/core").transform("code", { + plugins: ["@babel/plugin-transform-computed-properties"] +}); +``` + +## Options + +### `loose` + +`boolean`, defaults to `false` + +Just like method assignment in classes, in loose mode, computed property names +use simple assignments instead of being defined. This is unlikely to be an issue +in production code. + +#### Example + +***In*** + +```js +var obj = { + ["x" + foo]: "heh", + ["y" + bar]: "noo", + foo: "foo", + bar: "bar" +}; +``` + +***Out*** + +```js +var _obj; + +var obj = ( + _obj = {}, + _obj["x" + foo] = "heh", + _obj["y" + bar] = "noo", + _obj.foo = "foo", + _obj.bar = "bar", + _obj +); +``` diff --git a/node_modules/@babel/plugin-transform-computed-properties/lib/index.js b/node_modules/@babel/plugin-transform-computed-properties/lib/index.js new file mode 100644 index 00000000..6ae48311 --- /dev/null +++ b/node_modules/@babel/plugin-transform-computed-properties/lib/index.js @@ -0,0 +1,225 @@ +"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 _core() { + const data = require("@babel/core"); + + _core = function _core() { + return data; + }; + + return data; +} + +var _default = (0, _helperPluginUtils().declare)((api, options) => { + api.assertVersion(7); + const loose = options.loose; + const pushComputedProps = loose ? pushComputedPropsLoose : pushComputedPropsSpec; + const buildMutatorMapAssign = (0, _core().template)(` + MUTATOR_MAP_REF[KEY] = MUTATOR_MAP_REF[KEY] || {}; + MUTATOR_MAP_REF[KEY].KIND = VALUE; + `); + + function getValue(prop) { + if (_core().types.isObjectProperty(prop)) { + return prop.value; + } else if (_core().types.isObjectMethod(prop)) { + return _core().types.functionExpression(null, prop.params, prop.body, prop.generator, prop.async); + } + } + + function pushAssign(objId, prop, body) { + if (prop.kind === "get" && prop.kind === "set") { + pushMutatorDefine(objId, prop, body); + } else { + body.push(_core().types.expressionStatement(_core().types.assignmentExpression("=", _core().types.memberExpression(_core().types.cloneNode(objId), prop.key, prop.computed || _core().types.isLiteral(prop.key)), getValue(prop)))); + } + } + + function pushMutatorDefine({ + body, + getMutatorId, + scope + }, prop) { + let key = !prop.computed && _core().types.isIdentifier(prop.key) ? _core().types.stringLiteral(prop.key.name) : prop.key; + const maybeMemoise = scope.maybeGenerateMemoised(key); + + if (maybeMemoise) { + body.push(_core().types.expressionStatement(_core().types.assignmentExpression("=", maybeMemoise, key))); + key = maybeMemoise; + } + + body.push(...buildMutatorMapAssign({ + MUTATOR_MAP_REF: getMutatorId(), + KEY: _core().types.cloneNode(key), + VALUE: getValue(prop), + KIND: _core().types.identifier(prop.kind) + })); + } + + function pushComputedPropsLoose(info) { + for (var _iterator = info.computedProps, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { + var _ref; + + if (_isArray) { + if (_i >= _iterator.length) break; + _ref = _iterator[_i++]; + } else { + _i = _iterator.next(); + if (_i.done) break; + _ref = _i.value; + } + + const prop = _ref; + + if (prop.kind === "get" || prop.kind === "set") { + pushMutatorDefine(info, prop); + } else { + pushAssign(_core().types.cloneNode(info.objId), prop, info.body); + } + } + } + + function pushComputedPropsSpec(info) { + const objId = info.objId, + body = info.body, + computedProps = info.computedProps, + state = info.state; + + for (var _iterator2 = computedProps, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { + var _ref2; + + if (_isArray2) { + if (_i2 >= _iterator2.length) break; + _ref2 = _iterator2[_i2++]; + } else { + _i2 = _iterator2.next(); + if (_i2.done) break; + _ref2 = _i2.value; + } + + const prop = _ref2; + + const key = _core().types.toComputedKey(prop); + + if (prop.kind === "get" || prop.kind === "set") { + pushMutatorDefine(info, prop); + } else if (_core().types.isStringLiteral(key, { + value: "__proto__" + })) { + pushAssign(objId, prop, body); + } else { + if (computedProps.length === 1) { + return _core().types.callExpression(state.addHelper("defineProperty"), [info.initPropExpression, key, getValue(prop)]); + } else { + body.push(_core().types.expressionStatement(_core().types.callExpression(state.addHelper("defineProperty"), [_core().types.cloneNode(objId), key, getValue(prop)]))); + } + } + } + } + + return { + visitor: { + ObjectExpression: { + exit(path, state) { + const node = path.node, + parent = path.parent, + scope = path.scope; + let hasComputed = false; + var _arr = node.properties; + + for (var _i3 = 0; _i3 < _arr.length; _i3++) { + const prop = _arr[_i3]; + hasComputed = prop.computed === true; + if (hasComputed) break; + } + + if (!hasComputed) return; + const initProps = []; + const computedProps = []; + let foundComputed = false; + + for (var _iterator3 = node.properties, _isArray3 = Array.isArray(_iterator3), _i4 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { + var _ref3; + + if (_isArray3) { + if (_i4 >= _iterator3.length) break; + _ref3 = _iterator3[_i4++]; + } else { + _i4 = _iterator3.next(); + if (_i4.done) break; + _ref3 = _i4.value; + } + + const prop = _ref3; + + if (prop.computed) { + foundComputed = true; + } + + if (foundComputed) { + computedProps.push(prop); + } else { + initProps.push(prop); + } + } + + const objId = scope.generateUidIdentifierBasedOnNode(parent); + + const initPropExpression = _core().types.objectExpression(initProps); + + const body = []; + body.push(_core().types.variableDeclaration("var", [_core().types.variableDeclarator(objId, initPropExpression)])); + let mutatorRef; + + const getMutatorId = function getMutatorId() { + if (!mutatorRef) { + mutatorRef = scope.generateUidIdentifier("mutatorMap"); + body.push(_core().types.variableDeclaration("var", [_core().types.variableDeclarator(mutatorRef, _core().types.objectExpression([]))])); + } + + return _core().types.cloneNode(mutatorRef); + }; + + const single = pushComputedProps({ + scope, + objId, + body, + computedProps, + initPropExpression, + getMutatorId, + state + }); + + if (mutatorRef) { + body.push(_core().types.expressionStatement(_core().types.callExpression(state.addHelper("defineEnumerableProperties"), [_core().types.cloneNode(objId), _core().types.cloneNode(mutatorRef)]))); + } + + if (single) { + path.replaceWith(single); + } else { + body.push(_core().types.expressionStatement(_core().types.cloneNode(objId))); + path.replaceWithMultiple(body); + } + } + + } + } + }; +}); + +exports.default = _default;
\ No newline at end of file diff --git a/node_modules/@babel/plugin-transform-computed-properties/package.json b/node_modules/@babel/plugin-transform-computed-properties/package.json new file mode 100644 index 00000000..b33b08d8 --- /dev/null +++ b/node_modules/@babel/plugin-transform-computed-properties/package.json @@ -0,0 +1,21 @@ +{ + "name": "@babel/plugin-transform-computed-properties", + "version": "7.0.0-beta.47", + "description": "Compile ES2015 computed properties to ES5", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-computed-properties", + "license": "MIT", + "main": "lib/index.js", + "keywords": [ + "babel-plugin" + ], + "dependencies": { + "@babel/helper-plugin-utils": "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" + } +} |
