Skip to content

Commit d35ea83

Browse files
committed
compatible with object field
1 parent ecda59b commit d35ea83

File tree

2 files changed

+145
-2
lines changed

2 files changed

+145
-2
lines changed

lib/parser.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,24 @@ class Parser extends BaseParser {
11541154
};
11551155
}
11561156

1157+
fieldName() {
1158+
let fieldName = this.look;
1159+
while(this.isID() || this.is('-')) {
1160+
this.move();
1161+
const id = this.look;
1162+
if(this.is(Tag.NUMBER)) {
1163+
fieldName.lexeme += id.value;
1164+
} else if(this.isID()){
1165+
fieldName.lexeme += id.lexeme;
1166+
} else if(this.is('-')){
1167+
fieldName.lexeme += '-';
1168+
} else {
1169+
return fieldName;
1170+
}
1171+
}
1172+
return fieldName;
1173+
}
1174+
11571175
objectField() {
11581176
// objectField = objectFieldName "=" expr
11591177
const begin = this.getIndex();
@@ -1172,8 +1190,7 @@ class Parser extends BaseParser {
11721190
}
11731191

11741192
if (this.isID()) {
1175-
const fieldName = this.look;
1176-
this.move();
1193+
const fieldName = this.fieldName();
11771194
this.match('=');
11781195
const expr = this.expr();
11791196
const end = this.getIndex();

test/parser.test.js

+126
Original file line numberDiff line numberDiff line change
@@ -8936,6 +8936,130 @@ describe('parser', function () {
89368936
}).to.not.throwException();
89378937
});
89388938

8939+
it('string field and ids field should ok', function () {
8940+
var ast = parse(`
8941+
static function main(str: [string]): void {
8942+
var m = {
8943+
"number-test" = "123",
8944+
number-2 = "123",
8945+
number-2-test = "123",
8946+
number-test = "123",
8947+
};
8948+
}`, '__filename');
8949+
const mapFields = ast.moduleBody.nodes[0].functionBody.stmts.stmts[0].expr.fields;
8950+
console.log('%j', mapFields);
8951+
expect(mapFields).to.be.eql([
8952+
{
8953+
'type': 'objectField',
8954+
'fieldName': {
8955+
'tag': 1,
8956+
'loc': loc(4, 12, 4, 23),
8957+
'string': 'number-test',
8958+
'index': 18
8959+
},
8960+
'expr': {
8961+
'type': 'string',
8962+
'value': {
8963+
'tag': 1,
8964+
'loc': loc(4, 28, 4, 31),
8965+
'string': '123',
8966+
'index': 20
8967+
},
8968+
'loc': loc(4, 28, 4, 31),
8969+
'tokenRange': [
8970+
20,
8971+
21
8972+
]
8973+
},
8974+
'tokenRange': [
8975+
18,
8976+
21
8977+
]
8978+
},
8979+
{
8980+
'type': 'objectField',
8981+
'fieldName': {
8982+
'tag': 8,
8983+
'loc': loc(5, 11, 5, 17),
8984+
'lexeme': 'number-2',
8985+
'index': 22
8986+
},
8987+
'expr': {
8988+
'type': 'string',
8989+
'value': {
8990+
'tag': 1,
8991+
'loc': loc(5, 23, 5, 26),
8992+
'string': '123',
8993+
'index': 25
8994+
},
8995+
'loc': loc(5, 23, 5, 26),
8996+
'tokenRange': [
8997+
25,
8998+
26
8999+
]
9000+
},
9001+
'tokenRange': [
9002+
22,
9003+
26
9004+
]
9005+
},
9006+
{
9007+
'type': 'objectField',
9008+
'fieldName': {
9009+
'tag': 8,
9010+
'loc': loc(6, 11, 6, 17),
9011+
'lexeme': 'number-2-test',
9012+
'index': 27
9013+
},
9014+
'expr': {
9015+
'type': 'string',
9016+
'value': {
9017+
'tag': 1,
9018+
'loc': loc(6, 28, 6, 31),
9019+
'string': '123',
9020+
'index': 32
9021+
},
9022+
'loc': loc(6, 28, 6, 31),
9023+
'tokenRange': [
9024+
32,
9025+
33
9026+
]
9027+
},
9028+
'tokenRange': [
9029+
27,
9030+
33
9031+
]
9032+
},
9033+
{
9034+
'type': 'objectField',
9035+
'fieldName': {
9036+
'tag': 2,
9037+
'loc': loc(7, 11, 7, 22),
9038+
'lexeme': 'number-test',
9039+
'index': 34
9040+
},
9041+
'expr': {
9042+
'type': 'string',
9043+
'value': {
9044+
'tag': 1,
9045+
'loc': loc(7, 26, 7, 29),
9046+
'string': '123',
9047+
'index': 36
9048+
},
9049+
'loc': loc(7, 26, 7, 29),
9050+
'tokenRange': [
9051+
36,
9052+
37
9053+
]
9054+
},
9055+
'tokenRange': [
9056+
34,
9057+
37
9058+
]
9059+
}
9060+
]);
9061+
});
9062+
89399063
it('typedef should ok', function () {
89409064
var ast = parse(`
89419065
typedef HttpRequest;
@@ -10354,4 +10478,6 @@ describe('parser', function () {
1035410478
]
1035510479
});
1035610480
});
10481+
10482+
1035710483
});

0 commit comments

Comments
 (0)