Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
add support for $Exact magic type, fix #121 (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Sep 25, 2016
1 parent a7def9e commit 673bef0
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
**Note**: Gaps between patch versions are faulty/broken releases.
**Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.

# v0.3.14

- **New Feature**
- add support for `$Exact` magic type, fix #121 (@gcanti)

# v0.3.13

- **Bug Fix**
Expand Down
15 changes: 11 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ exports.default = function (_ref) {
// combinators
//

function addTypeName(combinatorArguments, typeName) {
function addTypeName(combinatorArguments, typeName, exact) {
if (t.isStringLiteral(typeName)) {
combinatorArguments.push(typeName);
if (exact) {
combinatorArguments.push(t.objectExpression([t.objectProperty(t.identifier('name'), typeName), t.objectProperty(t.identifier('strict'), t.booleanLiteral(true))]));
} else {
combinatorArguments.push(typeName);
}
}
return combinatorArguments;
}
Expand Down Expand Up @@ -86,8 +90,8 @@ exports.default = function (_ref) {
return callCombinator(refinementId, [type, predicate], name);
}

function getInterfaceCombinator(props, name) {
return callCombinator(interfaceId, [props], name);
function getInterfaceCombinator(props, name, exact) {
return t.callExpression(t.memberExpression(tcombId, interfaceId), addTypeName([props], name, exact));
}

function getDeclareCombinator(name) {
Expand Down Expand Up @@ -233,6 +237,9 @@ exports.default = function (_ref) {
if (name === 'Object') {
return getObjectType();
}
if (name === '$Exact') {
return getInterfaceCombinator(getObjectExpression(annotation.typeParameters.params[0].properties, typeParameters), typeName, true);
}
if (shouldReturnAnyType(name, typeParameters)) {
return getAnyType();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-tcomb",
"version": "0.3.13",
"version": "0.3.14",
"description": "Babel plugin for static and runtime type checking using Flow and tcomb",
"main": "lib/index.js",
"files": [
Expand Down
22 changes: 18 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,17 @@ export default function ({ types: t, template }) {
// combinators
//

function addTypeName(combinatorArguments, typeName) {
function addTypeName(combinatorArguments, typeName, exact) {
if (t.isStringLiteral(typeName)) {
combinatorArguments.push(typeName)
if (exact) {
combinatorArguments.push(t.objectExpression([
t.objectProperty(t.identifier('name'), typeName),
t.objectProperty(t.identifier('strict'), t.booleanLiteral(true))
]))
}
else {
combinatorArguments.push(typeName)
}
}
return combinatorArguments
}
Expand Down Expand Up @@ -159,8 +167,11 @@ export default function ({ types: t, template }) {
return callCombinator(refinementId, [type, predicate], name)
}

function getInterfaceCombinator(props, name) {
return callCombinator(interfaceId, [props], name)
function getInterfaceCombinator(props, name, exact) {
return t.callExpression(
t.memberExpression(tcombId, interfaceId),
addTypeName([props], name, exact)
)
}

function getDeclareCombinator(name) {
Expand Down Expand Up @@ -324,6 +335,9 @@ export default function ({ types: t, template }) {
if (name === 'Object') {
return getObjectType()
}
if (name === '$Exact') {
return getInterfaceCombinator(getObjectExpression(annotation.typeParameters.params[0].properties, typeParameters), typeName, true)
}
if (shouldReturnAnyType(name, typeParameters)) {
return getAnyType()
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/exact/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type A = $Exact<{ x: string }>;
8 changes: 8 additions & 0 deletions test/fixtures/exact/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import _t from "tcomb";

const A = _t.interface({
x: _t.String
}, {
name: "A",
strict: true
});
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ describe('emit asserts for: ', () => {
if ((caseName in skipTests)) {
return
}
if (!(caseName in { 'async': 1 })) {
if (!(caseName in { 'exact': 1 })) {
// return
}
it(`should ${caseName.split('-').join(' ')}`, () => {
Expand Down

0 comments on commit 673bef0

Please sign in to comment.