Fix #5112: A string of ', ' in an array should not be detected as an elision (#5113)

This commit is contained in:
Geoffrey Booth 2018-10-05 06:45:54 -07:00 committed by GitHub
parent 6e86b67818
commit 294bb4754e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 7 deletions

View File

@ -2469,10 +2469,10 @@
return [this.makeCode('[]')]; return [this.makeCode('[]')];
} }
o.indent += TAB; o.indent += TAB;
fragmentIsElision = function(fragment) { fragmentIsElision = function([fragment]) {
return fragmentsToText(fragment).trim() === ','; return fragment.type === 'Elision' && fragment.code.trim() === ',';
}; };
// Detect if `Elisions` at the beginning of the array are processed (e.g. [, , , a]). // Detect if `Elision`s at the beginning of the array are processed (e.g. [, , , a]).
passedElision = false; passedElision = false;
answer = []; answer = [];
ref1 = this.objects; ref1 = this.objects;
@ -2536,7 +2536,7 @@
fragment = answer[fragmentIndex]; fragment = answer[fragmentIndex];
if (fragment.isHereComment) { if (fragment.isHereComment) {
fragment.code = `${multident(fragment.code, o.indent, false)}\n${o.indent}`; fragment.code = `${multident(fragment.code, o.indent, false)}\n${o.indent}`;
} else if (fragment.code === ', ' && !(fragment != null ? fragment.isElision : void 0)) { } else if (fragment.code === ', ' && !(fragment != null ? fragment.isElision : void 0) && fragment.type !== 'StringLiteral') {
fragment.code = `,\n${o.indent}`; fragment.code = `,\n${o.indent}`;
} }
} }

View File

@ -1644,8 +1644,9 @@ exports.Arr = class Arr extends Base
compileNode: (o) -> compileNode: (o) ->
return [@makeCode '[]'] unless @objects.length return [@makeCode '[]'] unless @objects.length
o.indent += TAB o.indent += TAB
fragmentIsElision = (fragment) -> fragmentsToText(fragment).trim() is ',' fragmentIsElision = ([ fragment ]) ->
# Detect if `Elisions` at the beginning of the array are processed (e.g. [, , , a]). fragment.type is 'Elision' and fragment.code.trim() is ','
# Detect if `Elision`s at the beginning of the array are processed (e.g. [, , , a]).
passedElision = no passedElision = no
answer = [] answer = []
@ -1687,7 +1688,7 @@ exports.Arr = class Arr extends Base
for fragment, fragmentIndex in answer for fragment, fragmentIndex in answer
if fragment.isHereComment if fragment.isHereComment
fragment.code = "#{multident(fragment.code, o.indent, no)}\n#{o.indent}" fragment.code = "#{multident(fragment.code, o.indent, no)}\n#{o.indent}"
else if fragment.code is ', ' and not fragment?.isElision else if fragment.code is ', ' and not fragment?.isElision and fragment.type isnt 'StringLiteral'
fragment.code = ",\n#{o.indent}" fragment.code = ",\n#{o.indent}"
answer.unshift @makeCode "[\n#{o.indent}" answer.unshift @makeCode "[\n#{o.indent}"
answer.push @makeCode "\n#{@tab}]" answer.push @makeCode "\n#{@tab}]"

View File

@ -115,6 +115,12 @@ test "array elisions nested destructuring", ->
deepEqual d, {x:2} deepEqual d, {x:2}
arrayEq w, [1,2,4] arrayEq w, [1,2,4]
test "#5112: array elisions not detected inside strings", ->
arr = [
str: ", #{3}"
]
eq arr[0].str, ', 3'
# Splats in Array Literals # Splats in Array Literals
test "array splat expansions with assignments", -> test "array splat expansions with assignments", ->