Skip to content

add nullable operator #83

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 26 additions & 17 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1566,15 +1566,24 @@ var Parser = exports.Parser = Class.extend({
var typeDecl = this._typeDeclarationNoArrayNoVoid();
if (typeDecl == null)
return null;
// []
while (this._expectOpt("[") != null) {
if ((token = this._expect("]")) == null)
return false;
if (typeDecl instanceof NullableType) {
this._newError("Nullable.<T> cannot be an array, should be: T[]");
return null;
while (true) {
// ?
if (this._expectOpt("?") != null) {
if ((typeDecl = this._toNullableType(typeDecl)) == null)
return null;
}
// []
else if (this._expectOpt("[") != null) {
if ((token = this._expect("]")) == null)
return false;
if (typeDecl instanceof NullableType) {
this._newError("Nullable.<T> cannot be an array, should be: T[]");
return null;
}
typeDecl = this._registerArrayTypeOf(token, typeDecl);
}
typeDecl = this._registerArrayTypeOf(token, typeDecl);
else
break;
}
return typeDecl;
},
Expand All @@ -1589,22 +1598,22 @@ var Parser = exports.Parser = Class.extend({
this._newDeprecatedWarning("use of 'MayBeUndefined' is deprecated, use 'Nullable' instead");
// falls through
case "Nullable":
return this._nullableTypeDeclaration();
if (this._expect(".") == null || this._expect("<") == null)
return null;
var baseType = this._typeDeclaration(false);
if (baseType == null)
return null;
if (this._expect(">") == null)
return null;
return this._toNullableType(baseType);
case "variant":
return Type.variantType;
default:
throw new Error("logic flaw");
}
},

_nullableTypeDeclaration: function () {
if (this._expect(".") == null || this._expect("<") == null)
return null;
var baseType = this._typeDeclaration(false);
if (baseType == null)
return null;
if (this._expect(">") == null)
return null;
_toNullableType: function (baseType) {
if (baseType.equals(Type.variantType)) {
this._newError("variant cannot be declared as nullable (since it is always nullable)");
return null;
Expand Down
22 changes: 22 additions & 0 deletions t/run/218.nullable-notation.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*EXPECTED
123
*/

class Klass {

var str : string?;

function constructor(num : number?) {
if (num == null) {
this.str = null;
} else {
this.str = num as string;
}
}
}

class _Main {
static function main(args : string[]) : void {
log (new Klass(123)).str;
}
}