Skip to content

Commit 752cc94

Browse files
committed
fixed bug, ruby_statement_with_block with move(arg1).
1 parent c6df129 commit 752cc94

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

src/lib/ruby-to-blocks-converter/index.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,31 @@ class RubyToBlocksConverter {
139139

140140
_saveContext () {
141141
return {
142-
blocks: JSON.stringify(this._context.blocks),
143-
localVariables: JSON.stringify(this._context.localVariables),
144-
variables: JSON.stringify(this._context.variables),
145-
lists: JSON.stringify(this._context.lists)
142+
blocks: Object.assign({}, this._context.blocks),
143+
localVariables: Object.assign({}, this._context.localVariables),
144+
variables: Object.assign({}, this._context.variables),
145+
lists: Object.assign({}, this._context.lists)
146146
};
147147
}
148148

149+
// could not restore attributes.
149150
_restoreContext (saved) {
150151
if (!saved) {
151152
return;
152153
}
153154

154155
Object.keys(saved).forEach(key => {
155156
if (this._context.hasOwnProperty(key)) {
156-
this._context[key] = JSON.parse(saved[key]);
157+
Object.keys(this._context[key]).forEach(id => {
158+
if (!saved[key].hasOwnProperty(id)) {
159+
delete this._context[key][id];
160+
}
161+
});
162+
Object.keys(saved[key]).forEach(id => {
163+
if (!this._context[key].hasOwnProperty(id)) {
164+
this._context[key][id] = saved[key][id];
165+
}
166+
});
157167
}
158168
});
159169
}
@@ -1046,8 +1056,8 @@ class RubyToBlocksConverter {
10461056

10471057
if (rubyBlockNode) {
10481058
block = this._createBlock('ruby_statement_with_block', 'statement');
1049-
this._addInput(block, 'STATEMENT', this._createTextBlock(this._getSource(node)));
1050-
this._addInput(block, 'ARGS', this._createTextBlock(this._getSource(rubyBlockArgsNode)));
1059+
this._addTextInput(block, 'STATEMENT', this._getSource(node));
1060+
this._addTextInput(block, 'ARGS', this._getSource(rubyBlockArgsNode));
10511061
this._addSubstack(block, this._process(rubyBlockNode));
10521062
} else {
10531063
block = this._createRubyStatementBlock(this._getSource(node));

test/unit/lib/ruby-to-blocks-converter/ruby.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import RubyToBlocksConverter from '../../../../src/lib/ruby-to-blocks-converter'
22
import {
33
convertAndExpectToEqualBlocks,
44
convertAndExpectToEqualRubyStatement,
5+
rubyToExpected,
56
expectedInfo
67
} from '../../../helpers/expect-to-equal-blocks';
78

@@ -64,4 +65,51 @@ describe('RubyToBlocksConverter/Ruby', () => {
6465
convertAndExpectToEqualRubyStatement(converter, target, s, 'wait');
6566
});
6667
});
68+
69+
test('ruby_statement_with_block', () => {
70+
const code = `
71+
method_call(1) do |arg1, arg2|
72+
move(arg1)
73+
move(arg2)
74+
move(10)
75+
end
76+
`;
77+
const expected = [
78+
{
79+
opcode: 'ruby_statement_with_block',
80+
inputs: [
81+
{
82+
name: 'STATEMENT',
83+
block: expectedInfo.makeText('method_call(1)')
84+
},
85+
{
86+
name: 'ARGS',
87+
block: expectedInfo.makeText('|arg1, arg2|')
88+
}
89+
],
90+
branches: [
91+
{
92+
opcode: 'ruby_statement',
93+
inputs: [
94+
{
95+
name: 'STATEMENT',
96+
block: expectedInfo.makeText('move(arg1)')
97+
}
98+
],
99+
next: {
100+
opcode: 'ruby_statement',
101+
inputs: [
102+
{
103+
name: 'STATEMENT',
104+
block: expectedInfo.makeText('move(arg2)')
105+
}
106+
],
107+
next: rubyToExpected(converter, target, 'move(10)')[0]
108+
}
109+
}
110+
]
111+
}
112+
];
113+
convertAndExpectToEqualBlocks(converter, target, code, expected);
114+
});
67115
});

0 commit comments

Comments
 (0)