Skip to content

Commit

Permalink
fix the new keyword error
Browse files Browse the repository at this point in the history
  • Loading branch information
peze authored and JacksonTian committed Nov 16, 2023
1 parent aff0566 commit 8af50f3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,11 @@ class TypeChecker {
keyType: _basic('string'),
valueType: this.getParameterType(type.valueType, moduleName)
};
} else if (type.type === 'iterator' || type.type === 'asyncIterator') {
return {
type: type.type,
valueType: this.getParameterType(type.valueType, moduleName)
};
} else if (type.tag === Tag.TYPE) {
return _basic(type.lexeme);
} else if (type.tag === Tag.ID && type.lexeme.startsWith('$')) {
Expand Down Expand Up @@ -1687,8 +1692,8 @@ class TypeChecker {
const leftType = this.getExprType(ast.left, env);
this.visitExpr(ast.right, env);
const rightType = this.getExprType(ast.right, env);
if(ast.type !== 'eq' && !(leftType.type === 'basic' && isNumber(leftType.name)) ||
!(rightType.type === 'basic' && isNumber(rightType.name))) {
if(ast.type !== 'eq' && (!(leftType.type === 'basic' && isNumber(leftType.name)) ||
!(rightType.type === 'basic' && isNumber(rightType.name)))) {
this.error(`${ast.type} can only operate number type, ` +
`but left: ${display(leftType)} and right: ${display(rightType)}`, ast);
}
Expand Down
23 changes: 23 additions & 0 deletions test/semantic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,18 @@ describe('semantic', function () {
});

it('iterator should ok', function () {
expect(function () {
parse(`
static function getIterator(it: iterator[string]): iterator[string] {
return it;
}
static function useIterator(it: iterator[string]): void{
getIterator(it);
}
`, '__filename');
}).to.not.throwException();

expect(function () {
parse(`
function getIterator(it: iterator[string]): void {
Expand Down Expand Up @@ -2389,6 +2401,17 @@ describe('semantic', function () {
return 'false';
}`, '__filename');
}).to.not.throwException();

expect(function () {
parse(`static function callOSS(): string {
var a = 'string';
var b = 'string';
if(a == b) {
return 'true';
}
return 'false';
}`, '__filename');
}).to.not.throwException();

expect(function () {
parse(`static function callOSS(): boolean {
Expand Down

0 comments on commit 8af50f3

Please sign in to comment.