Skip to content

Commit

Permalink
fix the import bug
Browse files Browse the repository at this point in the history
  • Loading branch information
peze authored and yndu13 committed Dec 13, 2024
1 parent 0a9a30a commit 15fb6ff
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 18 deletions.
8 changes: 7 additions & 1 deletion builtin/form.dara
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ static function getBoundary(): string;
* @param boundary the boundary string
* @return the readable from file form
*/
static function toFileForm(form: object, boundary: string): readable;
static function toFileForm(form: object, boundary: string): readable;

/**
* Format a map to form string, like a=a%20b%20c
* @return the form string
*/
static function parseQueryString(val: string): object;
10 changes: 9 additions & 1 deletion builtin/json.dara
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ static function parseJSON(val: string): any;
* Stringify a value by JSON format
* @return the JSON format string
*/
static function stringify(val: any): string;
static function stringify(val: any): string;

/**
* Give a json object and a path, read value with the path
* @param json The json object
* @param path The path string
* @return value of json object with the path
*/
static function readPath(json: any, path: string): any;
32 changes: 17 additions & 15 deletions lib/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,10 @@ function isNeedToMap(expect, actual) {
}

class TypeChecker {
constructor(source, filename, root, libraries, inner = false) {
constructor(source, filename, root, libraries, mainPkg = undefined) {
this.source = source;
this.filename = filename;
this.inner = inner;
this.mainPkg = mainPkg;
// 方法: apis、functions
this.methods = new Map();
// 属性
Expand Down Expand Up @@ -736,7 +736,7 @@ class TypeChecker {

const filePath = this.filename;
const pkgDir = path.dirname(filePath);
const pkgPath = this.inner ? getDarafile(this.root) : getDarafile(pkgDir);
const pkgPath = this.mainPkg ? getDarafile(this.mainPkg) : getDarafile(pkgDir);
if (!fs.existsSync(pkgPath)) {
this.error(`the Darafile not exists`);
}
Expand All @@ -748,10 +748,9 @@ class TypeChecker {
const item = ast.imports[i];
const aliasId = item.lexeme;

let innerModule = !!item.innerPath;
let mainPkg = !!item.innerPath && path.dirname(pkgPath);
const mainModule = item.mainModule;

if (!mainModule && !innerModule && !pkg.libraries[aliasId] && pkg.name !== aliasId) {
if (!mainModule && !mainPkg && !pkg.libraries[aliasId] && pkg.name !== aliasId) {
this.error(`the import "${aliasId}" not defined in Darafile`, item);
}

Expand Down Expand Up @@ -780,8 +779,8 @@ class TypeChecker {
this.error(`the submodule id "${module}" has not been exported in "${mainModule}"`, item);
}
realSpecPath = path.join(libPath, libMeta.exports[module]);
innerModule = true;
} else if(innerModule) {
mainPkg = libPath;
} else if(mainPkg) {
if (item.innerPath.endsWith('.dara') || item.innerPath.endsWith('.tea') || item.innerPath.endsWith('.spec')) {
realSpecPath = path.join(pkgDir, item.innerPath);
} else if(fs.existsSync(path.join(pkgDir, `${item.innerPath}.dara`))){
Expand Down Expand Up @@ -813,9 +812,9 @@ class TypeChecker {
const lexer = new Lexer(source, realSpecPath);
const parser = new Parser(lexer);
const depAst = parser.program();
const checker = existsChecker.get(realSpecPath) || new TypeChecker(source, realSpecPath, this.root, this.libraries, innerModule).check(depAst);
const checker = existsChecker.get(realSpecPath) || new TypeChecker(source, realSpecPath, this.root, this.libraries, mainPkg).check(depAst);
this.dependencies.set(aliasId, checker);
if(innerModule) {
if(mainPkg) {
this.innerDep.set(aliasId, checker.ast);
}
this.usedExternModel.set(aliasId, new Set());
Expand All @@ -825,7 +824,7 @@ class TypeChecker {
checkExports(){
const filePath = this.filename;
const pkgDir = path.dirname(filePath);
const pkgPath = this.inner ? getDarafile(this.root) : getDarafile(pkgDir);
const pkgPath = this.mainPkg ? getDarafile(this.mainPkg) : getDarafile(pkgDir);
if (!fs.existsSync(pkgPath)) {
return;
}
Expand Down Expand Up @@ -878,7 +877,7 @@ class TypeChecker {
this.postCheckInit(ast);
// check unused virtualVariable & virtualMethod
this.postCheckTypes(ast);
if(!this.inner) {
if(!this.mainPkg) {
this.checkExports(ast);
}
ast.models = {};
Expand Down Expand Up @@ -1474,6 +1473,7 @@ class TypeChecker {
}

getParameterType(type, moduleName) {

if (type.tag === Tag.TYPE && type.lexeme === 'object') {
return {
type: 'map',
Expand Down Expand Up @@ -1870,6 +1870,10 @@ class TypeChecker {
return _typedef(id, moduleName);
}

if(builtin.has(id)) {
return this.getModel(find.fieldValue.fieldType.lexeme);
}

return this.getModel(find.fieldValue.fieldType.lexeme, moduleName);
}

Expand Down Expand Up @@ -2507,7 +2511,6 @@ class TypeChecker {
}

const expected = this.getParameterTypes(checker.init, aliasId);

if (!eql(expected, actual)) {
this.error(`the parameter` +
` types are mismatched. expected ` +
Expand Down Expand Up @@ -2643,7 +2646,6 @@ class TypeChecker {
return ast.inferred;
}


if (ast.type === 'property_access' || ast.type === 'property') {
return this.calculatePropertyType(ast, env);
}
Expand Down Expand Up @@ -3403,7 +3405,7 @@ function analyze(source, filePath) {
while(!key.done) {
const ast = builtin.get(key.value);
if(ast.type === 'module') {
builtin.set(key.value, new TypeChecker(source, filePath, null, null, true).check(ast));
builtin.set(key.value, new TypeChecker(source, filePath, null, null, 'builtin').check(ast));
}
key = it.next();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "OSS",
"main": "./oss.tea",
"libraries": {
"Util": "darabonba:Util:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
init();

function putObject(): void {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Util;

init();

function covert(): void {
var a = Util.sleep(300);
}
1 change: 1 addition & 0 deletions test/fixtures/multi_module/model/user.dara
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "../lib/util" Util;
model Info {
name: string,
age: integer,
options: $RetryOptions,
}


Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/multi_module/sdk.dara
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import "./api" API;
type @user = User.Info

init(){
var options = new $RetryOptions{};
@user = new User.Info{
name = 'test'
name = 'test',
options = options,
};
}

Expand Down

0 comments on commit 15fb6ff

Please sign in to comment.