From 2b4cbe7ef0cb0ac1504c7e69bc30ee198303ba41 Mon Sep 17 00:00:00 2001 From: Kevin Kwok Date: Thu, 18 Aug 2016 21:10:15 -0700 Subject: [PATCH] export __async is the promise itself --- dist/top-level-await.js | 29 ++++++++++++++++++++++----- lib/top-level-await.js | 44 +++++++++++++++++++++++++++++------------ package.json | 2 +- 3 files changed, 56 insertions(+), 19 deletions(-) diff --git a/dist/top-level-await.js b/dist/top-level-await.js index a432b29..6f53394 100644 --- a/dist/top-level-await.js +++ b/dist/top-level-await.js @@ -22,9 +22,7 @@ exports.default = function (_ref) { path.traverse({ AwaitExpression: function AwaitExpression(path) { var parent = path.getFunctionParent(); - if (parent.type === 'Program') { - has_await = true; - } + if (parent.type === 'Program') has_await = true; }, ExportDeclaration: function ExportDeclaration() { has_export = true; @@ -58,11 +56,11 @@ exports.default = function (_ref) { }); // here's the actual magical mess - path.replaceWith(t.program(imports.concat([t.exportNamedDeclaration(t.variableDeclaration("const", [t.variableDeclarator(t.identifier('__async'), t.booleanLiteral(true))]), [], null), t.exportDefaultDeclaration(t.callExpression(t.functionExpression(null, // anonymous + path.replaceWith(t.program(imports.concat([t.exportNamedDeclaration(t.variableDeclaration("const", [t.variableDeclarator(t.identifier('__async'), t.callExpression(t.functionExpression(null, // anonymous [], // params t.blockStatement(statements), false, // generator true), // async - []))]))); + []))]), [], null)]))); } } }; @@ -72,6 +70,27 @@ var _types = require("babylon/lib/tokenizer/types"); var _parser = require("babylon/lib/parser"); +// Copyright (c) 2016 antimatter15 + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + + _parser.plugins.topLevelAwait = function (instance) { instance.extend("parseTopLevel", function (file, program) { return function (file, program) { diff --git a/lib/top-level-await.js b/lib/top-level-await.js index 62ef6d4..076012b 100644 --- a/lib/top-level-await.js +++ b/lib/top-level-await.js @@ -1,3 +1,24 @@ +// Copyright (c) 2016 antimatter15 + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + + import { TokenType, types as tt } from "babylon/lib/tokenizer/types"; import { plugins, prototype as pp } from "babylon/lib/parser"; @@ -40,9 +61,7 @@ export default function({ types: t }) { path.traverse({ AwaitExpression(path){ var parent = path.getFunctionParent(); - if(parent.type === 'Program'){ - has_await = true; - } + if(parent.type === 'Program') has_await = true; }, ExportDeclaration(){ has_export = true }, ExportNamespaceSpecifier(){ has_export = true }, @@ -71,16 +90,15 @@ export default function({ types: t }) { // here's the actual magical mess path.replaceWith(t.program(imports.concat([ t.exportNamedDeclaration(t.variableDeclaration("const", [ - t.variableDeclarator(t.identifier('__async'), t.booleanLiteral(true)) - ]), [], null), - t.exportDefaultDeclaration(t.callExpression( - t.functionExpression( - null, // anonymous - [], // params - t.blockStatement(statements), - false, // generator - true), // async - [])) + t.variableDeclarator(t.identifier('__async'), t.callExpression( + t.functionExpression( + null, // anonymous + [], // params + t.blockStatement(statements), + false, // generator + true), // async + [])) + ]), [], null) ]))) } } diff --git a/package.json b/package.json index ef113e5..28eed5b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "babel-plugin-top-level-await", - "version": "0.6.0", + "version": "0.7.0", "description": "babel plugin for automatically wrapping top level awaits", "main": "dist/top-level-await.js", "directories": {