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

Fix tmp expr visit #245

Merged
merged 2 commits into from
Jan 21, 2025
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
4 changes: 2 additions & 2 deletions lib/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,7 @@ class TypeChecker {
}

if (isTmpVariable(id.type)) {
this.visitExpr(id, env);
return;
}

Expand Down Expand Up @@ -2345,7 +2346,6 @@ class TypeChecker {
if (ast.left.type === 'static_or_instance_call') {
const id = ast.left.id;
this.checkId(id, env);

if (env.local.hasDefined(id.lexeme) || isTmpVariable(id.type)) {
ast.left.type = 'instance_call';
this.visitInstanceCall(ast, env);
Expand Down Expand Up @@ -2510,7 +2510,7 @@ class TypeChecker {
ast.isStatic = definedApi.type === 'api' ? false : definedApi.isStatic;
ast.isAsync = definedApi.type === 'api' ? true : definedApi.isAsync;
ast.inferred = this.getType(definedApi.returnType);
ast.hasThrow = ast.isAsync || definedApi.hasThrow;
ast.hasThrow = !builtin.has(name) && (ast.isAsync || definedApi.hasThrow);
}

visitConstruct(ast, env) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@darabonba/parser",
"version": "2.1.3",
"version": "2.1.4",
"main": "index.js",
"directories": {
"lib": "lib",
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/tmp_var_call/.libraries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"alibabacloud:OSS:*": "libraries/alibabacloud-OSS-0.0.1"
}
5 changes: 4 additions & 1 deletion test/fixtures/tmp_var_call/Darafile
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
{
}
"libraries": {
"OSS": "alibabacloud:OSS:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "OSS",
"main": "./oss.dara"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
model Config {
accessKeyId: string
};

init(config: Config);

function getAccessKeyId(): string;

static function accessKeyId(): string;
5 changes: 5 additions & 0 deletions test/fixtures/tmp_var_call/main.dara
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import OSS;

init(){
var config = new OSS.Config{};
var oss = new OSS(config);
var arr = 'str.abc'.split('.');
var arr2 = `str.${OSS.accessKeyId()}.${oss.getAccessKeyId()}.abc`.split('.');
var str = ['1','2','3'].join('.');
var num = 1.parseLong();
var en = {
Expand Down
1 change: 0 additions & 1 deletion test/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ function callOSS(): string {
expect(function () {
readAndParse('fixtures/import_module_model/undefined_model.dara');
}).to.throwException(function (e) {
console.log(e);
expect(e).to.be.a(SyntaxError);
expect(e.message).to.be(`the model "ConfigX" is undefined in module "OSS"`);
});
Expand Down
2 changes: 0 additions & 2 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6053,7 +6053,6 @@ describe('parser', function () {
}
`, '__filename');
}).to.throwException(function(e) {
console.log(e);
expect(e).to.be.a(SyntaxError);
expect(e.message).to.be('Unexpected token: Word: `catch`. expect valid expression');
});
Expand Down Expand Up @@ -9898,7 +9897,6 @@ describe('parser', function () {
var str = 'tmpVar'.empty();
}
`, '__filename');
console.log('%j', ast);
const expr = ast.moduleBody.nodes[0].initBody.stmts[0].expr;
expect(expr).to.be.eql({
'type': 'call',
Expand Down
62 changes: 54 additions & 8 deletions test/semantic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,16 @@ describe('semantic', function () {
async function callId(): object {
return call();
}

async function sp(): void {
$sleep(30);
return;
}
init();`, '__filename');
var func = ast.moduleBody.nodes.find((item) => {
var func = ast.moduleBody.nodes.filter((item) => {
return item.type === 'function';
});
var returnExpr = func.functionBody.stmts.stmts[0];
var returnExpr = func[0].functionBody.stmts.stmts[0];
expect(returnExpr).to.eql({
type: 'return',
'loc': loc(10, 11, 11, 9),
Expand Down Expand Up @@ -626,6 +631,9 @@ describe('semantic', function () {
'loc': loc(10, 18, 10, 24)
}
});

var sleepStmt = func[1].functionBody.stmts.stmts[0];
expect(sleepStmt.hasThrow).to.eql(false);
});

it('submodel should ok', function () {
Expand Down Expand Up @@ -3071,7 +3079,6 @@ describe('semantic', function () {

}`, '__filename');
}).to.throwError(function(e) {
console.log(e);
expect(e).to.be.an(SyntaxError);
expect(e.message).to.be('the property "statusCode" is undefined in model "Error"');
});
Expand Down Expand Up @@ -4683,7 +4690,6 @@ describe('semantic', function () {

it('used exceptions should ok', function () {
let ast = readAndParse('fixtures/module_exception_used/main.dara');
console.log(ast.usedExternException);
expect(ast.usedExternException.get('OSS').has('Err1')).to.be(true);
expect(ast.usedExternException.get('OSS').has('Config')).to.be(false);

Expand Down Expand Up @@ -7684,8 +7690,48 @@ init() {
});

it('use tmp variable method call shoule be ok', function(){
expect(function () {
readAndParse('fixtures/tmp_var_call/main.dara');
}).to.not.throwException();
const ast = readAndParse('fixtures/tmp_var_call/main.dara');
let tStrAst = ast.moduleBody.nodes[0].initBody.stmts[3];
expect(tStrAst.expr.left.id.elements[1].expr.left).to.be.eql({
'type': 'static_call',
'id': {
'tag': 2,
'loc': loc(7, 21, 7, 24),
'lexeme': 'OSS',
'index': 41,
'type': 'module'
},
'propertyPath': [
{
'tag': 2,
'loc': loc(7, 25, 7, 36),
'lexeme': 'accessKeyId',
'index': 43
}
]
});

expect(tStrAst.expr.left.id.elements[3].expr.left).to.be.eql({
'type': 'instance_call',
'id': {
'tag': 2,
'loc': loc(7, 42, 7, 45),
'lexeme': 'oss',
'index': 47,
'type': 'variable',
'moduleType': {
'type': 'module',
'name': 'OSS'
}
},
'propertyPath': [
{
'tag': 2,
'loc': loc(7, 46, 7, 60),
'lexeme': 'getAccessKeyId',
'index': 49
}
]
});
});
});
});
Loading