Skip to content

Commit dc824bb

Browse files
committed
fix the model keyword
1 parent 90d101f commit dc824bb

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

lib/generator.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const fs = require('fs');
88
const DSL = require('@darabonba/parser');
99
const {
1010
_name, _string, _type, _subModelName, _vid,
11-
_isBinaryOp, _escape, _snakeCase, _camelCase,
11+
_isBinaryOp, _escape, _snakeCase, _camelCase, _avoidModel,
1212
REQUEST, RESPONSE, CORE, _upperFirst, _removeFilesInDirectory
1313
} = require('./helper');
1414
const getBuiltin = require('./builtin');
@@ -203,7 +203,7 @@ class Visitor {
203203
// this.emit(`\n`);
204204

205205
[...new Set(this.subModelUsed)].map(modelName => {
206-
this.header += `import { ${modelName} } from "./${_camelCase(_snakeCase(modelName))}";\n`;
206+
this.header += `import { ${modelName} } from "./${_avoidModel(_camelCase(_snakeCase(modelName)))}";\n`;
207207
});
208208

209209
[...new Set(this.exceptionUsed)].map(ExceptionName => {
@@ -1240,7 +1240,7 @@ Client.main(process.argv.slice(2));`);
12401240
for (let i = 0; i < models.length; i++) {
12411241
const ast = models[i];
12421242
const modelName = _subModelName(_name(ast.modelName));
1243-
this.emit(`export { ${modelName} } from './${_camelCase(_snakeCase(modelName))}';\n`);
1243+
this.emit(`export { ${modelName} } from './${_avoidModel(_camelCase(_snakeCase(modelName)))}';\n`);
12441244
}
12451245
this.save(path.join(this.modelPath, 'model.ts'), true);
12461246
}
@@ -1259,7 +1259,7 @@ Client.main(process.argv.slice(2));`);
12591259
this.subModelUsed = this.subModelUsed.filter(name => {
12601260
return name !== modelName;
12611261
});
1262-
this.saveBuffer(path.join(this.modelPath, `${_camelCase(_snakeCase(modelName))}.ts`));
1262+
this.saveBuffer(path.join(this.modelPath, `${_avoidModel(_camelCase(_snakeCase(modelName)))}.ts`));
12631263
this.usedTypes = [];
12641264
this.modelUsed = [];
12651265
this.moudleUsed = [];

lib/helper.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ function _name(str) {
2222
return str.lexeme || str.name;
2323
}
2424

25+
function _avoidModel(name) {
26+
if(name.toLowerCase() === 'model') {
27+
return `${name}_`;
28+
}
29+
return name;
30+
}
31+
2532
function _snakeCase(str) {
2633
if (!str) {
2734
return '';
@@ -223,5 +230,5 @@ function _removeFilesInDirectory(directoryPath) {
223230
module.exports = {
224231
_name, _string, _type, _subModelName, _vid, _upperFirst,
225232
_isBinaryOp, _escape, _isDefault, _snakeCase, _camelCase,
226-
REQUEST, RESPONSE, CORE, _removeFilesInDirectory
233+
REQUEST, RESPONSE, CORE, _removeFilesInDirectory, _avoidModel
227234
};

test/fixtures/model/main.dara

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ model M {
55
self: M,
66
}
77

8+
model Model {
9+
stringfield: string,
10+
}
11+
812
model MyModel {
913
stringfield: string,
1014
bytesfield: bytes,

test/fixtures/model/models/Model_.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This file is auto-generated, don't edit it
2+
import * as $dara from '@darabonba/typescript';
3+
4+
5+
export class Model extends $dara.Model {
6+
stringfield: string;
7+
static names(): { [key: string]: string } {
8+
return {
9+
stringfield: 'stringfield',
10+
};
11+
}
12+
13+
static types(): { [key: string]: any } {
14+
return {
15+
stringfield: 'string',
16+
};
17+
}
18+
19+
validate() {
20+
$dara.Model.validateRequired("stringfield", this.stringfield);
21+
super.validate();
22+
}
23+
24+
constructor(map?: { [key: string]: any }) {
25+
super(map);
26+
}
27+
}
28+

test/fixtures/model/models/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export { MSubM } from './MsubM';
22
export { MyModelSubmodel } from './MyModelSubmodel';
33
export { MyModelSubarraymodel } from './MyModelSubarraymodel';
44
export { M } from './M';
5+
export { Model } from './Model_';
56
export { MyModel } from './MyModel';
67
export { INT } from './Int';

0 commit comments

Comments
 (0)