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-for-of | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/@babel/plugin-transform-for-of')
3 files changed, 476 insertions, 0 deletions
diff --git a/node_modules/@babel/plugin-transform-for-of/README.md b/node_modules/@babel/plugin-transform-for-of/README.md new file mode 100644 index 00000000..4a8c5263 --- /dev/null +++ b/node_modules/@babel/plugin-transform-for-of/README.md @@ -0,0 +1,154 @@ +# @babel/plugin-transform-for-of + +> Compile ES2015 for...of to ES5 + +## Example + +**In** + +```js +for (var i of foo) {} +``` + +**Out** + +```js +var _iteratorNormalCompletion = true; +var _didIteratorError = false; +var _iteratorError = undefined; + +try { + for (var _iterator = foo[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var i = _step.value; + } +} catch (err) { + _didIteratorError = true; + _iteratorError = err; +} finally { + try { + if (!_iteratorNormalCompletion && _iterator.return != null) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } +} +``` + +## Installation + +```sh +npm install --save-dev @babel/plugin-transform-for-of +``` + +## Usage + +### Via `.babelrc` (Recommended) + +**.babelrc** + +Without options: + +```js +{ + "plugins": ["@babel/plugin-transform-for-of"] +} +``` + +With options: + +```js +{ + "plugins": [ + ["@babel/plugin-transform-for-of", { + "loose": true, // defaults to false + "assumeArray": true // defaults to false + }] + ] +} +``` + +### Via CLI + +```sh +babel --plugins @babel/plugin-transform-for-of script.js +``` + +### Via Node API + +```javascript +require("@babel/core").transform("code", { + plugins: ["@babel/plugin-transform-for-of"] +}); +``` + +## Options + +### `loose` + +`boolean`, defaults to `false` + +In loose mode, arrays are put in a fast path, thus heavily increasing performance. +All other iterables will continue to work fine. + +#### Example + +**In** + +```js +for (var i of foo) {} +``` + +**Out** + +```js +for (var _iterator = foo, _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; + } + + var i = _ref; +} +``` + +#### Abrupt completions + +In loose mode an iterator's `return` method will not be called on abrupt completions caused by thrown errors. + +Please see [google/traceur-compiler#1773](https://github.com/google/traceur-compiler/issues/1773) and +[babel/babel#838](https://github.com/babel/babel/issues/838) for more information. + +### `assumeArray` +`boolean`, defaults to `false` + +This will apply the optimization shown below to all for-of loops by assuming that _all_ loops are arrays. + +Can be useful when you just want a for-of loop to represent a basic for loop over an array. + +### Optimization + +If a basic array is used, Babel will compile the for-of loop down to a regular for loop. + +**In** + +```js +for (let a of [1,2,3]) {} +``` + +**Out** + +```js +var _arr = [1, 2, 3]; +for (var _i = 0; _i < _arr.length; _i++) { + var a = _arr[_i]; +} +``` diff --git a/node_modules/@babel/plugin-transform-for-of/lib/index.js b/node_modules/@babel/plugin-transform-for-of/lib/index.js new file mode 100644 index 00000000..cd3a4610 --- /dev/null +++ b/node_modules/@babel/plugin-transform-for-of/lib/index.js @@ -0,0 +1,301 @@ +"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, + assumeArray = options.assumeArray; + + if (loose === true && assumeArray === true) { + throw new Error(`The loose and assumeArray options cannot be used together in @babel/plugin-transform-for-of`); + } + + if (assumeArray) { + return { + visitor: { + ForOfStatement(path) { + const scope = path.scope; + const _path$node = path.node, + left = _path$node.left, + right = _path$node.right, + body = _path$node.body; + const i = scope.generateUidIdentifier("i"); + let array = scope.maybeGenerateMemoised(right, true); + const inits = [_core().types.variableDeclarator(i, _core().types.numericLiteral(0))]; + + if (array) { + inits.push(_core().types.variableDeclarator(array, right)); + } else { + array = right; + } + + const item = _core().types.memberExpression(_core().types.cloneNode(array), _core().types.cloneNode(i), true); + + let assignment; + + if (_core().types.isVariableDeclaration(left)) { + assignment = left; + assignment.declarations[0].init = item; + } else { + assignment = _core().types.expressionStatement(_core().types.assignmentExpression("=", left, item)); + } + + const block = _core().types.toBlock(body); + + block.body.unshift(assignment); + path.replaceWith(_core().types.forStatement(_core().types.variableDeclaration("let", inits), _core().types.binaryExpression("<", _core().types.cloneNode(i), _core().types.memberExpression(_core().types.cloneNode(array), _core().types.identifier("length"))), _core().types.updateExpression("++", _core().types.cloneNode(i)), block)); + } + + } + }; + } + + const pushComputedProps = loose ? pushComputedPropsLoose : pushComputedPropsSpec; + const buildForOfArray = (0, _core().template)(` + for (var KEY = 0; KEY < ARR.length; KEY++) BODY; + `); + const buildForOfLoose = (0, _core().template)(` + for (var LOOP_OBJECT = OBJECT, + IS_ARRAY = Array.isArray(LOOP_OBJECT), + INDEX = 0, + LOOP_OBJECT = IS_ARRAY ? LOOP_OBJECT : LOOP_OBJECT[Symbol.iterator]();;) { + INTERMEDIATE; + if (IS_ARRAY) { + if (INDEX >= LOOP_OBJECT.length) break; + ID = LOOP_OBJECT[INDEX++]; + } else { + INDEX = LOOP_OBJECT.next(); + if (INDEX.done) break; + ID = INDEX.value; + } + } + `); + const buildForOf = (0, _core().template)(` + var ITERATOR_COMPLETION = true; + var ITERATOR_HAD_ERROR_KEY = false; + var ITERATOR_ERROR_KEY = undefined; + try { + for ( + var ITERATOR_KEY = OBJECT[Symbol.iterator](), STEP_KEY; + !(ITERATOR_COMPLETION = (STEP_KEY = ITERATOR_KEY.next()).done); + ITERATOR_COMPLETION = true + ) {} + } catch (err) { + ITERATOR_HAD_ERROR_KEY = true; + ITERATOR_ERROR_KEY = err; + } finally { + try { + if (!ITERATOR_COMPLETION && ITERATOR_KEY.return != null) { + ITERATOR_KEY.return(); + } + } finally { + if (ITERATOR_HAD_ERROR_KEY) { + throw ITERATOR_ERROR_KEY; + } + } + } + `); + + function _ForOfStatementArray(path) { + const node = path.node, + scope = path.scope; + const nodes = []; + let right = node.right; + + if (!_core().types.isIdentifier(right) || !scope.hasBinding(right.name)) { + const uid = scope.generateUid("arr"); + nodes.push(_core().types.variableDeclaration("var", [_core().types.variableDeclarator(_core().types.identifier(uid), right)])); + right = _core().types.identifier(uid); + } + + const iterationKey = scope.generateUidIdentifier("i"); + let loop = buildForOfArray({ + BODY: node.body, + KEY: iterationKey, + ARR: right + }); + + _core().types.inherits(loop, node); + + _core().types.ensureBlock(loop); + + const iterationValue = _core().types.memberExpression(_core().types.cloneNode(right), _core().types.cloneNode(iterationKey), true); + + const left = node.left; + + if (_core().types.isVariableDeclaration(left)) { + left.declarations[0].init = iterationValue; + loop.body.body.unshift(left); + } else { + loop.body.body.unshift(_core().types.expressionStatement(_core().types.assignmentExpression("=", left, iterationValue))); + } + + if (path.parentPath.isLabeledStatement()) { + loop = _core().types.labeledStatement(path.parentPath.node.label, loop); + } + + nodes.push(loop); + return nodes; + } + + function replaceWithArray(path) { + if (path.parentPath.isLabeledStatement()) { + path.parentPath.replaceWithMultiple(_ForOfStatementArray(path)); + } else { + path.replaceWithMultiple(_ForOfStatementArray(path)); + } + } + + return { + visitor: { + ForOfStatement(path, state) { + const right = path.get("right"); + + if (right.isArrayExpression() || right.isGenericType("Array") || _core().types.isArrayTypeAnnotation(right.getTypeAnnotation())) { + replaceWithArray(path); + return; + } + + const node = path.node; + const build = pushComputedProps(path, state); + const declar = build.declar; + const loop = build.loop; + const block = loop.body; + path.ensureBlock(); + + if (declar) { + block.body.push(declar); + } + + block.body = block.body.concat(node.body.body); + + _core().types.inherits(loop, node); + + _core().types.inherits(loop.body, node.body); + + if (build.replaceParent) { + path.parentPath.replaceWithMultiple(build.node); + path.remove(); + } else { + path.replaceWithMultiple(build.node); + } + } + + } + }; + + function pushComputedPropsLoose(path, file) { + const node = path.node, + scope = path.scope, + parent = path.parent; + const left = node.left; + let declar, id, intermediate; + + if (_core().types.isIdentifier(left) || _core().types.isPattern(left) || _core().types.isMemberExpression(left)) { + id = left; + intermediate = null; + } else if (_core().types.isVariableDeclaration(left)) { + id = scope.generateUidIdentifier("ref"); + declar = _core().types.variableDeclaration(left.kind, [_core().types.variableDeclarator(left.declarations[0].id, _core().types.identifier(id.name))]); + intermediate = _core().types.variableDeclaration("var", [_core().types.variableDeclarator(_core().types.identifier(id.name))]); + } else { + throw file.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`); + } + + const iteratorKey = scope.generateUidIdentifier("iterator"); + const isArrayKey = scope.generateUidIdentifier("isArray"); + const loop = buildForOfLoose({ + LOOP_OBJECT: iteratorKey, + IS_ARRAY: isArrayKey, + OBJECT: node.right, + INDEX: scope.generateUidIdentifier("i"), + ID: id, + INTERMEDIATE: intermediate + }); + + const isLabeledParent = _core().types.isLabeledStatement(parent); + + let labeled; + + if (isLabeledParent) { + labeled = _core().types.labeledStatement(parent.label, loop); + } + + return { + replaceParent: isLabeledParent, + declar: declar, + node: labeled || loop, + loop: loop + }; + } + + function pushComputedPropsSpec(path, file) { + const node = path.node, + scope = path.scope, + parent = path.parent; + const left = node.left; + let declar; + const stepKey = scope.generateUid("step"); + + const stepValue = _core().types.memberExpression(_core().types.identifier(stepKey), _core().types.identifier("value")); + + if (_core().types.isIdentifier(left) || _core().types.isPattern(left) || _core().types.isMemberExpression(left)) { + declar = _core().types.expressionStatement(_core().types.assignmentExpression("=", left, stepValue)); + } else if (_core().types.isVariableDeclaration(left)) { + declar = _core().types.variableDeclaration(left.kind, [_core().types.variableDeclarator(left.declarations[0].id, stepValue)]); + } else { + throw file.buildCodeFrameError(left, `Unknown node type ${left.type} in ForStatement`); + } + + const template = buildForOf({ + ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"), + ITERATOR_COMPLETION: scope.generateUidIdentifier("iteratorNormalCompletion"), + ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"), + ITERATOR_KEY: scope.generateUidIdentifier("iterator"), + STEP_KEY: _core().types.identifier(stepKey), + OBJECT: node.right + }); + + const isLabeledParent = _core().types.isLabeledStatement(parent); + + const tryBody = template[3].block.body; + const loop = tryBody[0]; + + if (isLabeledParent) { + tryBody[0] = _core().types.labeledStatement(parent.label, loop); + } + + return { + replaceParent: isLabeledParent, + declar: declar, + loop: loop, + node: template + }; + } +}); + +exports.default = _default;
\ No newline at end of file diff --git a/node_modules/@babel/plugin-transform-for-of/package.json b/node_modules/@babel/plugin-transform-for-of/package.json new file mode 100644 index 00000000..d078c9a0 --- /dev/null +++ b/node_modules/@babel/plugin-transform-for-of/package.json @@ -0,0 +1,21 @@ +{ + "name": "@babel/plugin-transform-for-of", + "version": "7.0.0-beta.47", + "description": "Compile ES2015 for...of to ES5", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-for-of", + "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" + } +} |
