babel-plugin-ts-optchain
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -130,1 +130,38 @@ import dedent from "dedent"; | ||
}); | ||
test("can handle function in accessor in the leaf getter", async () => { | ||
const code = dedent` | ||
import { oc } from "ts-optchain"; | ||
oc(data).foo.bar.baz[last(1)](); | ||
`; | ||
const res = runPlugin(code); | ||
expect(res.code).toEqual(dedent` | ||
import { oc } from "babel-plugin-ts-optchain/lib/runtime"; | ||
oc(data, ["foo", "bar", "baz", last(1)]); | ||
`); | ||
}); | ||
test("has good error message when chain does not end with function call", async () => { | ||
const code = dedent` | ||
import { oc } from "ts-optchain"; | ||
const foo = oc(data).foo; | ||
const bar = foo.bar(); | ||
`; | ||
expect(() => { | ||
runPlugin(code); | ||
}).toThrow("Last property accessor in ts-optchain must be a function call"); | ||
}); | ||
test("has good error message when there are no property accessors at all", async () => { | ||
const code = dedent` | ||
import { oc } from "ts-optchain"; | ||
const x = oc(data); | ||
const bar = x.bar(); | ||
`; | ||
expect(() => { | ||
runPlugin(code); | ||
}).toThrow("You must add at least one property accessor to oc() calls"); | ||
}); |
{ | ||
"name": "babel-plugin-ts-optchain", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/plugin.js", |
@@ -35,3 +35,18 @@ # babel-plugin-ts-optchain | ||
## Limitations | ||
You must call `oc()` in a single chain. Eg. this does not work: | ||
```ts | ||
const x = oc(data); | ||
const bar = x.foo.bar(); | ||
``` | ||
Write it like this | ||
```ts | ||
const bar = oc(data).foo.bar(); | ||
``` | ||
[ts-optchain]: https://github.com/rimeto/ts-optchain | ||
[mb]: https://github.com/burakcan/mb |
@@ -31,2 +31,13 @@ import * as types from "@babel/types"; | ||
defaultValue = path.parent.arguments[0]; | ||
} else { | ||
if (t.isMemberExpression(path.node)) { | ||
throw new Error( | ||
"Last property accessor in ts-optchain must be a function call. " + | ||
`Add () to .${path.node.property.name};`, | ||
); | ||
} else { | ||
throw new Error( | ||
"You must add at least one property accessor to oc() calls", | ||
); | ||
} | ||
} | ||
@@ -33,0 +44,0 @@ |
52
19003
15
448