Skip to content

Commit

Permalink
add exception used type
Browse files Browse the repository at this point in the history
  • Loading branch information
peze authored and JacksonTian committed Jun 28, 2024
1 parent 1e91795 commit aedc6a5
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,6 @@ builtin.set('$default', _function('$default', 'any', [_param('variable', 'any'),

builtin.set('$equal', _function('$equal', 'boolean', [_param('data', 'any'), _param('eqlData', 'any')]));

builtin.set('$kernelInfo', _function('$kernelInfo', 'map[string]string', []));

module.exports = builtin;
21 changes: 21 additions & 0 deletions lib/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ class TypeChecker {
ast.models[key] = value;
}
ast.usedExternModel = this.usedExternModel;
ast.usedExternException = this.resolveUsedExceptions(this.usedExternModel);
ast.conflictModels = this.resolveConflictModels(ast.usedExternModel);
ast.usedTypes = this.usedTypes;
ast.innerDep = this.innerDep;
Expand Down Expand Up @@ -895,6 +896,26 @@ class TypeChecker {
return conflicts;
}

resolveUsedExceptions(usedExternModel) {
const exceptions = new Map();
for (const [moduleName, models] of usedExternModel) {
const checker = this.dependencies.get(moduleName);
for (var name of models) {
const model = checker.models.get(name);
if (model && model.isException) {
if(!exceptions.has(moduleName)) {
exceptions.set(moduleName, new Set());
}

const set = exceptions.get(moduleName);

set.add(name);
}
}
}
return exceptions;
}

checkTypedefs(ast) {
ast.moduleBody.nodes.filter((item) => {
return item.type === 'typedef';
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/module_exception_used/.libraries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"alibabacloud:OSS:*": "libraries/alibabacloud-OSS-0.0.1",
"alibabacloud:Source:*": "libraries/alibabacloud-Source-0.0.1"
}
6 changes: 6 additions & 0 deletions test/fixtures/module_exception_used/Darafile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"libraries": {
"OSS": "alibabacloud:OSS:*",
"Source": "alibabacloud:Source:*"
}
}
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
};

exception Err1 {};

init(config: Config);

function getAccessKeyId(): string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Source",
"main": "./source.dara"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
model Config {
SourceId: string
};

exception Err1 {};

init(config: Config);
18 changes: 18 additions & 0 deletions test/fixtures/module_exception_used/main.dara
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import OSS
import Source

exception Err1{};

init(config1: Source.Config, config2: OSS.Config) {
var a = new OSS.Err1{
code = 'Err'
};

var b = new Source.Err1{
code = 'Err'
};

var c = new Err1{
code = 'Err'
};
}
10 changes: 10 additions & 0 deletions test/semantic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4614,6 +4614,16 @@ describe('semantic', function () {
expect(ast.conflictModels.has('OSS:Config')).to.be(false);
});

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);

expect(ast.usedExternException.get('Source').has('Err1')).to.be(true);
expect(ast.usedExternException.get('Source').has('Config')).to.be(false);
});

it('multi-dimentional array assign check should be ok', function () {
expect(function () {
parse(`
Expand Down

0 comments on commit aedc6a5

Please sign in to comment.