Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Commit

Permalink
Update: Add support for numeric and string literal types (fixes #156) (
Browse files Browse the repository at this point in the history
…#172)

Added support to typed.js for string and numeric literals in types.
  • Loading branch information
mister-walter authored and nzakas committed Aug 22, 2016
1 parent 8e219a0 commit 12c7ad9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
30 changes: 29 additions & 1 deletion lib/typed.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
OptionalType: 'OptionalType',
NullableType: 'NullableType',
NameExpression: 'NameExpression',
TypeApplication: 'TypeApplication'
TypeApplication: 'TypeApplication',
StringLiteralType: 'StringLiteralType',
NumericLiteralType: 'NumericLiteralType'
};

Token = {
Expand Down Expand Up @@ -462,6 +464,10 @@
token = Token.EQUAL;
return token;

case 0x2D: /* '-' */
token = scanNumber();
return token;

default:
if (esutils.code.isDecimalDigit(ch)) {
token = scanNumber();
Expand Down Expand Up @@ -888,6 +894,20 @@

return parseTypeName();

case Token.STRING:
next();
return {
type: Syntax.StringLiteralType,
value: value
};

case Token.NUMBER:
next();
return {
type: Syntax.NumericLiteralType,
value: value
};

default:
utility.throwError('unexpected token');
}
Expand Down Expand Up @@ -1220,6 +1240,14 @@
result += '>';
break;

case Syntax.StringLiteralType:
result = '"' + node.value + '"';
break;

case Syntax.NumericLiteralType:
result = String(node.value);
break;

default:
utility.throwError('Unknown type ' + node.type);
}
Expand Down
46 changes: 45 additions & 1 deletion test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,25 @@ describe('parse', function () {
]
});
});

it('string literal property', function () {
var res = doctrine.parse(
[
"/**",
" * @typedef {Object} comment",
" * @property {('public'|'protected'|'private')} access",
"*/"
].join('\n'), { unwrap: true });

res.tags.should.have.length(2);
res.tags[1].should.have.property('title', 'property');
res.tags[1].should.have.property('name', 'access');
res.tags[1].type.should.have.property('type', 'UnionType');
res.tags[1].type.elements.should.have.length(3);
res.tags[1].type.elements.should.containEql({type: 'StringLiteralType', value: 'public'});
res.tags[1].type.elements.should.containEql({type: 'StringLiteralType', value: 'private'});
res.tags[1].type.elements.should.containEql({type: 'StringLiteralType', value: 'protected'});
});
});

describe('parseType', function () {
Expand Down Expand Up @@ -1744,6 +1763,29 @@ describe('parseType', function () {
});
});

it('string literal type', function () {
var type;
type = doctrine.parseType('"Hello, World"');
type.should.eql({
type: 'StringLiteralType',
value: 'Hello, World'
});
});

it('numeric literal type', function () {
var type;
type = doctrine.parseType('32');
type.should.eql({
type: 'NumericLiteralType',
value: 32
});
type = doctrine.parseType('-142.42');
type.should.eql({
type: 'NumericLiteralType',
value: -142.42
});
});

it('illegal tokens', function () {
doctrine.parseType.bind(doctrine, ".").should.throw('unexpected token');
doctrine.parseType.bind(doctrine, ".d").should.throw('unexpected token');
Expand Down Expand Up @@ -2405,7 +2447,9 @@ describe('exported Syntax', function() {
OptionalType: 'OptionalType',
NullableType: 'NullableType',
NameExpression: 'NameExpression',
TypeApplication: 'TypeApplication'
TypeApplication: 'TypeApplication',
StringLiteralType: 'StringLiteralType',
NumericLiteralType: 'NumericLiteralType'
});
});
});
Expand Down
18 changes: 18 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ describe('stringify', function () {
testStringify("function(a:number,callback:function(a:Array.<(String|Number|Object)>):boolean):String");
testStringify("function(a:(string|number),this:string,new:true):function():number");
testStringify("function(a:(string|number),this:string,new:true):function(a:function(val):result):number");

// literal types
testStringify('"Hello, World!"');
testStringify("-142.35");
});

describe('literals', function() {
Expand All @@ -102,6 +106,20 @@ describe('literals', function() {
type: doctrine.Syntax.UndefinedLiteral
}).should.equal('undefined');
});

it('StringLiteralType', function () {
doctrine.type.stringify({
type: doctrine.Syntax.StringLiteralType,
value: 'Hello, World!'
}).should.equal('"Hello, World!"');
});

it('NumericLiteralType', function () {
doctrine.type.stringify({
type: doctrine.Syntax.NumericLiteralType,
value: '-142.35'
}).should.equal('-142.35');
});
});

describe('Expression', function () {
Expand Down

0 comments on commit 12c7ad9

Please sign in to comment.