Skip to content

Commit

Permalink
upgrade the iterator check
Browse files Browse the repository at this point in the history
  • Loading branch information
peze authored and JacksonTian committed Mar 6, 2024
1 parent 789f08a commit ff0ddd3
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
2 changes: 2 additions & 0 deletions builtin/crypto.dara
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// this package is staging package, don't publish

/**
* HmacSHA1 Signature
* @param stringToSign string
Expand Down
6 changes: 6 additions & 0 deletions lib/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,9 @@ class TypeChecker {
this.checkType(ast.returnType);

const returnType = ast.returnType;
if(returnType.type === 'iterator' && ast.isAsync) {
this.error(`async function return type must be asyncIterator`, ast.functionName);
}
if (ast.functionBody) {
const local = new Env();
// merge the parameters into local env
Expand Down Expand Up @@ -1095,6 +1098,9 @@ class TypeChecker {
});
this.checkType(ast.returnType);
const returnType = ast.returnType;
if(returnType.type === 'iterator') {
this.error(`api return type must be asyncIterator`, ast.apiName);
}
const local = new Env();
// merge the parameters into local env
for (const [key, value] of paramMap) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/multi_module/lib/util.dara
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
static async function test1(): iterator[string];
static function test1(): iterator[string];

static function getQuery(): map[string]string;
2 changes: 1 addition & 1 deletion test/fixtures/multi_module/model/user.dara
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ model Info {
}


static async function test(): iterator[string]{
static function test(): iterator[string]{
var it:iterator[string] = Util.test1();
for(var test : it) {
yield test;
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/multi_module/sdk.dara
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ init(){
};
}

async function test3(): iterator[string]{
async function test3(): asyncIterator[string]{
var it:iterator[string] = Util.test1();
for(var test : it) {
yield test;
Expand Down
73 changes: 66 additions & 7 deletions test/semantic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1367,29 +1367,88 @@ describe('semantic', function () {
it('yield should ok', function () {
expect(function () {
parse(`
async function test1(): iterator[string];
async function test2(): iterator[string]{
async function test2(): iterator[number]{
yield 32;
}
`, '__filename');
}).to.throwException(function (e) {
expect(e).to.be.a(SyntaxError);
expect(e.message).to.be(`async function return type must be asyncIterator`);
});

expect(function () {
parse(`
init();
api get(): asyncIterator[number] {
__request.method = 'GET';
__request.pathname = '/';
__request.headers.key = \`abc\`;
if (true) {
__request.headers.authorization = \`acs \`;
}
} returns {
yield __response.statusCode;
}
`, '__filename');
}).to.ok();

expect(function () {
parse(`
async function test1(): asyncIterator[string];
async function test2(): asyncIterator[string]{
var it:iterator[string] = test1();
for(var test : it) {
yield test;
}
}
async function test3(): iterator[string]{
async function test3(): asyncIterator[string]{
yield 'test';
}
`, '__filename');
}).to.ok();

expect(function () {
parse(`
async function test2(): iterator[string]{
async function test2(): asyncIterator[string]{
yield 32;
}
`, '__filename');
}).to.throwException(function (e) {
expect(e).to.be.a(SyntaxError);
expect(e.message).to.be(`the return type is not expected, expect: string, actual: integer`);
});

expect(function () {
parse(`
async function test2(): iterator[number]{
yield 32;
}
`, '__filename');
}).to.throwException(function (e) {
expect(e).to.be.a(SyntaxError);
expect(e.message).to.be(`async function return type must be asyncIterator`);
});

expect(function () {
parse(`
init();
api get(): iterator[number] {
__request.method = 'GET';
__request.pathname = '/';
__request.headers.key = \`abc\`;
if (true) {
__request.headers.authorization = \`acs \`;
}
} returns {
yield __response.statusCode;
}
`, '__filename');
}).to.throwException(function (e) {
expect(e).to.be.a(SyntaxError);
expect(e.message).to.be(`api return type must be asyncIterator`);
});
});

it('parameter check for call should ok', function () {
Expand Down Expand Up @@ -6783,15 +6842,15 @@ describe('semantic', function () {
'loc': {
'start': {
'line': 1,
'column': 23
'column': 17
},
'end': {
'line': 1,
'column': 28
'column': 22
}
},
'lexeme': 'test1',
'index': 4
'index': 3
});

const apiAst = ast.innerDep.get('API');
Expand Down

0 comments on commit ff0ddd3

Please sign in to comment.