-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TODO: PR to babel repo
- Loading branch information
Showing
9 changed files
with
190 additions
and
12 deletions.
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
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
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
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
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 |
---|---|---|
@@ -1,10 +1,34 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
|
||
import { parse } from "../babel-parser"; | ||
|
||
export default function babelPluginProposalAwaitOps(api, options) { | ||
import syntax from "./syntax"; | ||
|
||
export default declare((api) => { | ||
api.assertVersion(7); | ||
|
||
const t = api.types; | ||
|
||
return { | ||
parserOverride(code, options) { | ||
return parse(code, options); | ||
name: "transform-await-ops", | ||
inherits: syntax, | ||
parserOverride: parse, | ||
visitor: { | ||
AwaitExpression({ node }) { | ||
if (!node.operation) { | ||
return; | ||
} | ||
|
||
node.argument = t.callExpression( | ||
t.memberExpression( | ||
t.identifier("Promise"), | ||
t.identifier(node.operation) | ||
), | ||
[node.argument] | ||
); | ||
|
||
delete node.operation; | ||
}, | ||
}, | ||
}; | ||
} | ||
}); |
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,11 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
|
||
export default declare(api => { | ||
api.assertVersion(7); | ||
return { | ||
name: "syntax-await-ops", | ||
manipulateOptions(opts, parserOpts) { | ||
parserOpts.plugins.push("await-ops"); | ||
}, | ||
}; | ||
}); |
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 |
---|---|---|
@@ -1,13 +1,110 @@ | ||
import { transform } from "@babel/core"; | ||
import { parseExpression } from "../babel-parser"; | ||
import { Errors } from "../babel-parser/parser/error"; | ||
|
||
import babelPluginProposalAwaitOps from "../src"; | ||
const COMMON_OPTIONS = { | ||
allowAwaitOutsideFunction: true, | ||
plugins: ["await-ops"], | ||
}; | ||
|
||
describe("parser", () => { | ||
it("should just work", () => { | ||
expect( | ||
transform("const foo = 1", { | ||
plugins: [babelPluginProposalAwaitOps], | ||
}).code | ||
).toBe("const foo = 1;"); | ||
expect(parseExpression("await.all foo", COMMON_OPTIONS)) | ||
.toMatchInlineSnapshot(` | ||
Node { | ||
"argument": Node { | ||
"end": 13, | ||
"loc": SourceLocation { | ||
"end": Position { | ||
"column": 13, | ||
"line": 1, | ||
}, | ||
"identifierName": "foo", | ||
"start": Position { | ||
"column": 10, | ||
"line": 1, | ||
}, | ||
}, | ||
"name": "foo", | ||
"start": 10, | ||
"type": "Identifier", | ||
}, | ||
"comments": Array [], | ||
"end": 13, | ||
"errors": Array [], | ||
"loc": SourceLocation { | ||
"end": Position { | ||
"column": 13, | ||
"line": 1, | ||
}, | ||
"start": Position { | ||
"column": 0, | ||
"line": 1, | ||
}, | ||
}, | ||
"operation": "all", | ||
"start": 0, | ||
"type": "AwaitExpression", | ||
} | ||
`); | ||
}); | ||
|
||
it("should be able to parse literal array", () => { | ||
expect(parseExpression("await.race []", COMMON_OPTIONS)) | ||
.toMatchInlineSnapshot(` | ||
Node { | ||
"argument": Node { | ||
"elements": Array [], | ||
"end": 13, | ||
"loc": SourceLocation { | ||
"end": Position { | ||
"column": 13, | ||
"line": 1, | ||
}, | ||
"start": Position { | ||
"column": 11, | ||
"line": 1, | ||
}, | ||
}, | ||
"start": 11, | ||
"type": "ArrayExpression", | ||
}, | ||
"comments": Array [], | ||
"end": 13, | ||
"errors": Array [], | ||
"loc": SourceLocation { | ||
"end": Position { | ||
"column": 13, | ||
"line": 1, | ||
}, | ||
"start": Position { | ||
"column": 0, | ||
"line": 1, | ||
}, | ||
}, | ||
"operation": "race", | ||
"start": 0, | ||
"type": "AwaitExpression", | ||
} | ||
`); | ||
}); | ||
|
||
it("should throw syntax error if plugin not enabled", () => { | ||
expect(() => | ||
parseExpression("await.ops foo", { | ||
allowAwaitOutsideFunction: true, | ||
}) | ||
).toThrow("Unexpected token"); | ||
}); | ||
|
||
it("should throw on unsupported operation", () => { | ||
expect(() => parseExpression("await.ops foo", COMMON_OPTIONS)).toThrow( | ||
Errors.UnexpectedAwaitOperation.replace(/%0/g, "ops") | ||
); | ||
}); | ||
|
||
it("should throw on invalid syntax", () => { | ||
expect(() => parseExpression("await. []", COMMON_OPTIONS)).toThrow( | ||
"Unexpected token" | ||
); | ||
}); | ||
}); |
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,26 @@ | ||
import { transform } from "@babel/core"; | ||
|
||
import babelPluginProposalAwaitOps from "../src"; | ||
|
||
const COMMON_OPTIONS = { | ||
babelrc: false, | ||
plugins: ["@babel/syntax-top-level-await", babelPluginProposalAwaitOps], | ||
}; | ||
|
||
describe("plugin", () => { | ||
it("should just work", () => { | ||
expect(transform(`await.all foo`, COMMON_OPTIONS).code).toBe( | ||
"await Promise.all(foo);" | ||
); | ||
}); | ||
|
||
it("should work with literal array", () => { | ||
expect(transform(`await.all [Promise.resolve()]`, COMMON_OPTIONS).code).toBe( | ||
"await Promise.all([Promise.resolve()]);" | ||
); | ||
}); | ||
|
||
it("should work with regular await expression", () => { | ||
expect(transform(`await foo`, COMMON_OPTIONS).code).toBe("await foo;"); | ||
}); | ||
}); |
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