Skip to content

Commit bc46247

Browse files
authored
Merge pull request #105 from smalruby/issue/97
変数名として使えない文字を _ にエスケープする closes #97
2 parents 07042e2 + 23e368b commit bc46247

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/lib/ruby-generator/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ export default function (Blockly) {
452452
return null;
453453
};
454454

455+
const escapeIdentityRegexp =
456+
/[\x00-\x1f\x7f-\x9f !"#$%&'()*+,-./:;<=>?@[\\\]^`{|}~]/g; // eslint-disable-line no-control-regex
457+
455458
Blockly.Ruby.variableName = function (id, type = SCALAR_TYPE) {
456459
let currVar;
457460
let prefix;
@@ -472,7 +475,7 @@ export default function (Blockly) {
472475
}
473476
}
474477
if (currVar && currVar.type === type) {
475-
return `${prefix}${currVar.name}`;
478+
return `${prefix}${currVar.name.replace(escapeIdentityRegexp, '_')}`;
476479
}
477480
return null;
478481
};

test/unit/lib/ruby-generator.test.jsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ describe('RubyGenerator', () => {
6969
id4: {
7070
name: 'List2',
7171
type: LIST_TYPE
72+
},
73+
id2_1: {
74+
name: 'Avg(Total / Count)',
75+
type: SCALAR_TYPE
76+
},
77+
id2_2: {
78+
name: 'List of Symbols.',
79+
type: LIST_TYPE
80+
},
81+
id2_3: {
82+
name: ' !"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~]',
83+
type: SCALAR_TYPE
84+
},
85+
id2_4: {
86+
name: '平均(合計 / 件数)',
87+
type: SCALAR_TYPE
88+
},
89+
id2_5: {
90+
name: 'シンボル 配列。',
91+
type: LIST_TYPE
7292
}
7393
},
7494
runtime: {
@@ -126,6 +146,26 @@ describe('RubyGenerator', () => {
126146
expect(Ruby.variableName('id5')).toEqual(null);
127147
expect(Ruby.listName('id6')).toEqual(null);
128148
});
149+
150+
test('escape except alphabet, number and _ to _', () => {
151+
expect(Ruby.variableName('id2_1')).toEqual('@Avg_Total___Count_');
152+
expect(Ruby.listName('id2_2')).toEqual('@List_of_Symbols_');
153+
expect(Ruby.variableName('id2_3')).toEqual('@_________________________________');
154+
155+
renderedTarget.isStage = true;
156+
expect(Ruby.variableName('id2_1')).toEqual('$Avg_Total___Count_');
157+
expect(Ruby.listName('id2_2')).toEqual('$List_of_Symbols_');
158+
expect(Ruby.variableName('id2_3')).toEqual('$_________________________________');
159+
});
160+
161+
test('do not escape multibyte character like Japanese', () => {
162+
expect(Ruby.variableName('id2_4')).toEqual('@平均_合計___件数_');
163+
expect(Ruby.listName('id2_5')).toEqual('@シンボル 配列。');
164+
165+
renderedTarget.isStage = true;
166+
expect(Ruby.variableName('id2_4')).toEqual('$平均_合計___件数_');
167+
expect(Ruby.listName('id2_5')).toEqual('$シンボル 配列。');
168+
});
129169
});
130170

131171
describe('spriteNew', () => {

0 commit comments

Comments
 (0)