Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate assertion for Object Type Application #17

Merged
merged 2 commits into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"test": "test"
},
"scripts": {
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
"build": "cross-env NODE_ENV=production babel src --out-dir lib --source-maps",
"watch": "babel src --out-dir lib --watch --source-maps",
"prepublish": "npm run --if-present build",
"test": "mocha"
Expand All @@ -44,6 +44,7 @@
"babel-traverse": "^6.7.3",
"babel-types": "^6.7.2",
"babylon": "^6.7.0",
"cross-env": "^3.1.4",
"escodegen": "^1.8.0",
"esprima": "^3.1.1",
"estraverse": "^4.2.0",
Expand Down
82 changes: 44 additions & 38 deletions src/AssertGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,32 @@ export default class AssertGenerator {
}, typeNode);
const generator = new Generator(node);
const tagType = typeNode.type.type;
if (tagType === "NameExpression") {
return NameExpression(node, generator);
} else if (tagType === "AllLiteral") {
return AllLiteral(node, generator);
} else if (tagType === "FunctionType") {
return FunctionType(node, generator);
} else if (tagType === "RecordType") {
return RecordType(node, generator);
} else if (tagType === "UnionType") {
return UnionType(node, generator);
} else if (tagType === "TypeApplication") {
return TypeApplication(node, generator);
} else if (tagType === "RestType") {
return RestType(node, generator);
} else if (tagType === "NullableType") {
return NullableType(node, generator);
} else if (tagType === "NonNullableType") {
return NonNullableType(node, generator);
switch (tagType) {
case "NameExpression":
return NameExpression(node, generator);
case "AllLiteral":
return AllLiteral(node, generator);
case "FunctionType":
return FunctionType(node, generator);
case "RecordType":
return RecordType(node, generator);
case "UnionType":
return UnionType(node, generator);
case "TypeApplication":
return TypeApplication(node, generator);
case "RestType":
return RestType(node, generator);
case "NullableType":
return NullableType(node, generator);
case "NonNullableType":
return NonNullableType(node, generator);
Copy link
Owner

@azu azu Jan 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add default: return; case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

default:
return;
}
}

/**
* @param tagNode tagNode is defined by doctorin
* @param tagNode tagNode is defined by doctrine
* @param {CodeGenerator} Generator
* @return {string|undefined} return assertion code string
* Reference https://esdoc.org/tags.html#type-syntax
Expand All @@ -128,24 +131,27 @@ export default class AssertGenerator {
}
const generator = new Generator(tagNode);
const tagType = tagNode.type.type;
if (tagType === "NameExpression") {
return NameExpression(tagNode, generator);
} else if (tagType === "AllLiteral") {
return AllLiteral(tagNode, generator);
} else if (tagType === "FunctionType") {
return FunctionType(tagNode, generator);
} else if (tagType === "RecordType") {
return RecordType(tagNode, generator);
} else if (tagType === "UnionType") {
return UnionType(tagNode, generator);
} else if (tagType === "TypeApplication") {
return TypeApplication(tagNode, generator);
} else if (tagType === "RestType") {
return RestType(tagNode, generator);
} else if (tagType === "NullableType") {
return NullableType(tagNode, generator);
} else if (tagType === "NonNullableType") {
return NonNullableType(tagNode, generator);
switch (tagType) {
case "NameExpression":
return NameExpression(tagNode, generator);
case "AllLiteral":
return AllLiteral(tagNode, generator);
case "FunctionType":
return FunctionType(tagNode, generator);
case "RecordType":
return RecordType(tagNode, generator);
case "UnionType":
return UnionType(tagNode, generator);
case "TypeApplication":
return TypeApplication(tagNode, generator);
case "RestType":
return RestType(tagNode, generator);
case "NullableType":
return NullableType(tagNode, generator);
case "NonNullableType":
return NonNullableType(tagNode, generator);
default:
return;
}
}
};
}
8 changes: 8 additions & 0 deletions src/tag/TypeApplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"use strict";
import {Expression} from "./Expression";
// @param {Array.<string>}
// @param {Object<string,number>}
/**
* @return {string|undefined}
*/
Expand All @@ -25,5 +26,12 @@ export function TypeApplication(tag, CodeGenerator) {
} else {
return CodeGenerator.assert(`Array.isArray(${tag.name})`);
}
} else if (expectedType === "Object") {
const applications = tag.type.applications;
if (applications && applications.length === 2) {
const expression = Expression(`${tag.name}`, {name: "object"});
const itemExpression = Expression(`${tag.name}[key]`, applications[1]);
return CodeGenerator.assert(`${expression} && Object.keys(${tag.name}).every(function(key) { return (${itemExpression}); })`);
}
}
}
Loading