Skip to content

Commit e4c0312

Browse files
authored
Fix undefined bug for template strings. (#45)
1 parent fca943e commit e4c0312

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

__tests__/src/getPropLiteralValue-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ describe('getLiteralPropValue', () => {
158158
assert.equal(expected, actual);
159159
});
160160

161-
it('should drop variables in template literals that are literally undefined', () => {
161+
it('should return string "undefined" for expressions that evaluate to undefined', () => {
162162
const prop = extractProp('<div foo={`bar ${undefined}`} />');
163163

164-
const expected = 'bar ';
164+
const expected = 'bar undefined';
165165
const actual = getLiteralPropValue(prop);
166166

167167
assert.equal(expected, actual);
@@ -178,10 +178,10 @@ describe('getLiteralPropValue', () => {
178178
assert.equal(expected, actual);
179179
});
180180

181-
it('should drop variables in template literals that are literally undefined', () => {
181+
it('should return string "undefined" for expressions that evaluate to undefined', () => {
182182
const prop = extractProp('<div foo={noop`bar ${undefined}`} />');
183183

184-
const expected = 'bar ';
184+
const expected = 'bar undefined';
185185
const actual = getLiteralPropValue(prop);
186186

187187
assert.equal(expected, actual);

__tests__/src/getPropValue-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ describe('getPropValue', () => {
194194
assert.equal(expected, actual);
195195
});
196196

197-
it('should drop variables in template literals that are literally undefined', () => {
197+
it('should return string "undefined" for expressions that evaluate to undefined', () => {
198198
const prop = extractProp('<div foo={`bar ${undefined}`} />');
199199

200-
const expected = 'bar ';
200+
const expected = 'bar undefined';
201201
const actual = getPropValue(prop);
202202

203203
assert.equal(expected, actual);
@@ -232,10 +232,10 @@ describe('getPropValue', () => {
232232
assert.equal(expected, actual);
233233
});
234234

235-
it('should drop variables in template literals that are literally undefined', () => {
235+
it('should return string "undefined" for expressions that evaluate to undefined', () => {
236236
const prop = extractProp('<div foo={noop`bar ${undefined}`} />');
237237

238-
const expected = 'bar ';
238+
const expected = 'bar undefined';
239239
const actual = getPropValue(prop);
240240

241241
assert.equal(expected, actual);

src/values/expressions/TemplateLiteral.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* prop. For instance `This is a ${prop}` will return 'This is a {prop}'.
55
*
66
* If the template literal builds to undefined (`${undefined}`), then
7-
* this should return "".
7+
* this should return "undefined".
88
*/
99
export default function extractValueFromTemplateLiteral(value) {
1010
const {
@@ -20,7 +20,7 @@ export default function extractValueFromTemplateLiteral(value) {
2020
if (type === 'TemplateElement') {
2121
return raw + part.value.raw;
2222
} else if (type === 'Identifier') {
23-
return part.name === 'undefined' ? raw : `${raw}{${part.name}}`;
23+
return part.name === 'undefined' ? `${raw}${part.name}` : `${raw}{${part.name}}`;
2424
} else if (type.indexOf('Expression') > -1) {
2525
return `${raw}{${type}}`;
2626
}

0 commit comments

Comments
 (0)