Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Share argument rendering logic between call and new expression
Solves an issue where trying to render a non-included argument crashes
- Loading branch information
1 parent
0da6a80
commit aea0df3
Showing
6 changed files
with
68 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import MagicString from 'magic-string'; | ||
import CallExpression from '../ast/nodes/CallExpression'; | ||
import NewExpression from '../ast/nodes/NewExpression'; | ||
import { findFirstOccurrenceOutsideComment, RenderOptions } from './renderHelpers'; | ||
|
||
export function renderCallArguments( | ||
code: MagicString, | ||
options: RenderOptions, | ||
node: CallExpression | NewExpression | ||
): void { | ||
if (node.arguments.length > 0) { | ||
if (node.arguments[node.arguments.length - 1].included) { | ||
for (const arg of node.arguments) { | ||
arg.render(code, options); | ||
} | ||
} else { | ||
let lastIncludedIndex = node.arguments.length - 2; | ||
while (lastIncludedIndex >= 0 && !node.arguments[lastIncludedIndex].included) { | ||
lastIncludedIndex--; | ||
} | ||
if (lastIncludedIndex >= 0) { | ||
for (let index = 0; index <= lastIncludedIndex; index++) { | ||
node.arguments[index].render(code, options); | ||
} | ||
code.remove( | ||
findFirstOccurrenceOutsideComment( | ||
code.original, | ||
',', | ||
node.arguments[lastIncludedIndex].end | ||
), | ||
node.end - 1 | ||
); | ||
} else { | ||
code.remove( | ||
findFirstOccurrenceOutsideComment(code.original, '(', node.callee.end) + 1, | ||
node.end - 1 | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
test/function/samples/unused-constructor-argument-fallback/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const assert = require('assert'); | ||
|
||
module.exports = { | ||
description: 'handles unused logical expressions as constructor arguments (#4517)', | ||
exports({ create }) { | ||
assert.strictEqual(create().foo, 'foo'); | ||
} | ||
}; |
7 changes: 7 additions & 0 deletions
7
test/function/samples/unused-constructor-argument-fallback/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function Note() { | ||
this.foo = 'foo'; | ||
} | ||
|
||
export function create(data) { | ||
return new Note(data || {}); | ||
} |