Skip to content

Commit

Permalink
add extends fields in modelBody
Browse files Browse the repository at this point in the history
  • Loading branch information
peze authored and JacksonTian committed Oct 14, 2024
1 parent 5fb0d9c commit 1fd597b
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
node-version: [14.x, 16.x, 18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
2 changes: 1 addition & 1 deletion builtin/date.dara
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function unix(): integer;

function diff(unit: string, date: $Date): integer;

function UTC(): string;
function UTC(): $Date;

function add(unit: string, timeLong: integer): $Date;

Expand Down
41 changes: 41 additions & 0 deletions lib/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,60 @@ builtin.set('$Request', _model('$Request', [
_mapfield('headers', 'string', 'string'),
_field('body', 'readable')
]));

builtin.set('$SSEEvent', _model('$SSEEvent', [
_field('id', 'string'),
_field('event', 'string'),
_field('data', 'string'),
_field('retry', 'integer')
]));

builtin.set('$Error', _model('$Error', [
_field('name', 'string'),
_field('message', 'string'),
_field('code', 'string'),
_field('stack', 'string')
]));

builtin.set('$RetryOptions', _model('$RetryOptions', [
_field('retryable', 'boolean'),
]));

builtin.set('$ExtendsParameters', _model('$ExtendsParameters', [
_mapfield('headers', 'string', 'string'),
_mapfield('queries', 'string', 'string'),
]));

builtin.set('$RuntimeOptions', _model('$RuntimeOptions', [
_field('retryOptions', {
'tag': 2,
'lexeme': '$RetryOptions',
'idType': 'model'
}),
_field('autoretry', 'boolean'),
_field('ignoreSSL', 'boolean'),
_field('key', 'string'),
_field('cert', 'string'),
_field('ca', 'string'),
_field('maxAttempts', 'number'),
_field('backoffPolicy', 'string'),
_field('backoffPeriod', 'number'),
_field('readTimeout', 'number'),
_field('connectTimeout', 'number'),
_field('httpProxy', 'string'),
_field('httpsProxy', 'string'),
_field('noProxy', 'string'),
_field('socks5Proxy', 'string'),
_field('socks5NetWork', 'string'),
_field('maxIdleConns', 'number'),
_field('keepAlive', 'boolean'),
_field('extendsParameters', {
'tag': 2,
'lexeme': '$ExtendsParameters',
'idType': 'model'
}),
]));

builtin.set('$ResponseError', _model('$ResponseError', [
_field('statusCode', 'number'),
_field('retryAfter', 'number'),
Expand Down
11 changes: 9 additions & 2 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,9 @@ class Parser extends BaseParser {

template() {
var elements = [];
var loc = {
start: this.look.loc.start,
};
elements.push({
type: 'element',
value: this.look
Expand All @@ -1236,9 +1239,11 @@ class Parser extends BaseParser {
var last = this.look;
this.move();
if (last.tag === Tag.TEMPLATE && last.tail === true) {
loc.end = last.loc.end;
return {
type: 'template_string',
elements: elements
elements: elements,
loc
};
}

Expand All @@ -1251,6 +1256,7 @@ class Parser extends BaseParser {
});
this.move();
if (current.tail === true) {
loc.end = current.loc.end;
break;
}
} else {
Expand All @@ -1264,7 +1270,8 @@ class Parser extends BaseParser {

return {
type: 'template_string',
elements: elements
elements: elements,
loc
};
}

Expand Down
61 changes: 59 additions & 2 deletions lib/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,6 @@ class TypeChecker {

this.checkId(ast.id, env);


// check property
const type = this.getVariableType(ast.id, env);
ast.id.inferred = type;
Expand Down Expand Up @@ -2555,6 +2554,12 @@ class TypeChecker {
return _basic('class');
}

if(builtin.get(ast.id.lexeme)) {
const builtinModel = builtin.get(ast.id.lexeme);
if(builtinModel.type === 'model') {
return _model(ast.id.lexeme);
}
}


if (this.dependencies.has(ast.id.lexeme)) {
Expand Down Expand Up @@ -3064,6 +3069,7 @@ class TypeChecker {
} else {
throw new Error('unimplemented');
}

const expected = this.getExprType(ast.left, env);
ast.left.inferred = expected;
this.visitExpr(ast.expr, env);
Expand Down Expand Up @@ -3213,9 +3219,12 @@ class TypeChecker {
}
}

const isException = type === 'exception';

modelBody.extendFileds = this.getExtendFileds(extendOn, isException, keys);
this.models.set(modelName, {
type: type,
isException: type === 'exception',
isException: isException,
extendOn: extendOn,
modelName: {
tag: Tag.ID,
Expand All @@ -3226,6 +3235,54 @@ class TypeChecker {
});
}

getExtendFileds(extendOn, isException, keys) {
if(!extendOn && !isException) {
return [];
}
let extendModel, moduleName;
let checker = this;

if(!extendOn && isException) {
extendModel = builtin.get('$Error');
isException = false;
} else if (extendOn.type === 'moduleModel') {
const [ main, ...path ] = extendOn.path;
moduleName = main.lexeme;
checker = this.dependencies.get(moduleName);
const typeName = path.map((item) => {
return item.lexeme;
}).join('.');
extendModel = checker.models.get(typeName);
} else if (extendOn.type === 'subModel') {
let modelName = extendOn.path.map((tag) => {
return tag.lexeme;
}).join('.');
extendModel = this.models.get(modelName);
} else if (extendOn.idType === 'builtin_model') {
extendModel = builtin.get(extendOn.lexeme);
} else {
extendModel = this.models.get(extendOn.lexeme);
}
if(!extendModel) {
return [];
}

let extendFileds = extendModel.modelBody.nodes.filter(node => {
const fieldName = node.fieldName.lexeme;
if(keys.has(fieldName)) {
return false;
}
keys.set(fieldName, true);
// non
// node.tokenRange = [];
return true;
});
if(extendModel.extendOn) {
return extendFileds.concat(this.getExtendFileds(extendModel.extendOn, isException, keys));
}
return extendFileds;
}

visitModel(ast) {
assert.equal(ast.type, 'model');
const modelName = ast.modelName.lexeme;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "Jackson Tian",
"license": "MIT",
"devDependencies": {
"codecov": "^3",
"codecov": "3.1.0",
"eslint": "^8",
"expect.js": "^0.3.1",
"mocha": "^10",
Expand Down
12 changes: 12 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1966,6 +1966,7 @@ describe('parser', function () {
}
}
],
loc: loc(6, 17, 6, 24),
'tokenRange': [21, 22],
'type': 'template_string'
});
Expand Down Expand Up @@ -2007,6 +2008,7 @@ describe('parser', function () {
}
}
],
loc: loc(6, 17, 6, 32),
'tokenRange': [21, 24],
'type': 'template_string'
});
Expand Down Expand Up @@ -2072,6 +2074,7 @@ describe('parser', function () {
}
}
],
loc: loc(6, 17, 6, 37),
'tokenRange': [21, 26],
'type': 'template_string'
});
Expand Down Expand Up @@ -4417,6 +4420,7 @@ describe('parser', function () {
}
}
],
'loc': loc(7, 46, 7, 68),
'tokenRange': [41, 44],
'type': 'template_string'
},
Expand Down Expand Up @@ -4586,6 +4590,7 @@ describe('parser', function () {
}
],
'tokenRange': [63, 66],
'loc': loc(9, 46, 9, 68),
'type': 'template_string'
},
'left': {
Expand Down Expand Up @@ -4662,6 +4667,7 @@ describe('parser', function () {
}
}
],
loc: loc(7, 46, 7, 68),
'tokenRange': [41, 44],
'type': 'template_string'
},
Expand Down Expand Up @@ -4891,6 +4897,7 @@ describe('parser', function () {
}
}
],
loc: loc(9, 23, 9, 51),
'tokenRange': [29, 34],
'type': 'template_string'
}
Expand Down Expand Up @@ -4970,6 +4977,7 @@ describe('parser', function () {
}
}
],
loc: loc(9, 23, 9, 51),
'tokenRange': [29, 34],
'type': 'template_string'
}
Expand Down Expand Up @@ -5113,6 +5121,7 @@ describe('parser', function () {
}
}
],
loc: loc(9, 23, 9, 51),
'tokenRange': [29, 34],
'type': 'template_string'
}
Expand Down Expand Up @@ -5198,6 +5207,7 @@ describe('parser', function () {
}
}
],
loc: loc(9, 23, 9, 51),
'tokenRange': [29, 34],
'type': 'template_string'
}
Expand Down Expand Up @@ -5378,6 +5388,7 @@ describe('parser', function () {
}
}
],
loc: loc(9, 23, 9, 51),
'tokenRange': [29, 34],
'type': 'template_string'
}
Expand Down Expand Up @@ -5463,6 +5474,7 @@ describe('parser', function () {
}
}
],
loc: loc(9, 23, 9, 51),
'tokenRange': [29, 34],
'type': 'template_string'
}
Expand Down
Loading

0 comments on commit 1fd597b

Please sign in to comment.