Skip to content

Commit a4a79ab

Browse files
authored
Merge pull request #194 from smalruby/fix_list_index
fix list index.
2 parents afad290 + 6febef1 commit a4a79ab

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/lib/ruby-generator/data.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ export default function (Generator) {
3636
return `list(${Generator.quote_(list)})`;
3737
};
3838

39+
const getListIndex = function (block) {
40+
const index = Generator.valueToCode(block, 'INDEX', Generator.ORDER_NONE) || 1;
41+
if (index === '0') {
42+
return 1;
43+
}
44+
return index;
45+
};
46+
3947
Generator.data_listcontents = function (block) {
4048
const list = getListName(block);
4149
return [list, Generator.ORDER_COLLECTION];
@@ -48,7 +56,7 @@ export default function (Generator) {
4856
};
4957

5058
Generator.data_deleteoflist = function (block) {
51-
const index = Generator.valueToCode(block, 'INDEX', Generator.ORDER_NONE) - 1 || 0;
59+
const index = getListIndex(block);
5260
const list = getListName(block);
5361
return `${list}.delete_at(${Generator.nosToCode(index)})\n`;
5462
};
@@ -59,21 +67,21 @@ export default function (Generator) {
5967
};
6068

6169
Generator.data_insertatlist = function (block) {
62-
const index = Generator.valueToCode(block, 'INDEX', Generator.ORDER_NONE) - 1 || 0;
70+
const index = getListIndex(block);
6371
const item = Generator.valueToCode(block, 'ITEM', Generator.ORDER_NONE) || '0';
6472
const list = getListName(block);
6573
return `${list}.insert(${index}, ${Generator.nosToCode(item)})\n`;
6674
};
6775

6876
Generator.data_replaceitemoflist = function (block) {
69-
const index = Generator.valueToCode(block, 'INDEX', Generator.ORDER_INDEX) - 1 || 0;
77+
const index = getListIndex(block);
7078
const item = Generator.valueToCode(block, 'ITEM', Generator.ORDER_NONE) || '0';
7179
const list = getListName(block);
7280
return `${list}[${index}] = ${Generator.nosToCode(item)}\n`;
7381
};
7482

7583
Generator.data_itemoflist = function (block) {
76-
const index = Generator.valueToCode(block, 'INDEX', Generator.ORDER_INDEX) - 1 || 0;
84+
const index = getListIndex(block);
7785
const list = getListName(block);
7886
return [`${list}[${index}]`, Generator.ORDER_FUNCTION_CAL];
7987
};

0 commit comments

Comments
 (0)