Skip to content

Commit

Permalink
Do not check over parameters when finding possible star in default ex…
Browse files Browse the repository at this point in the history
…ported function (#3522)
  • Loading branch information
lukastaegert committed Apr 29, 2020
1 parent 7cf6f98 commit f9a6042
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/ast/nodes/ExportDefaultDeclaration.ts
Expand Up @@ -23,10 +23,18 @@ function getDeclarationStart(code: string, start: number) {
return start;
}

function getIdInsertPosition(code: string, declarationKeyword: string, start: number) {
function getIdInsertPosition(
code: string,
declarationKeyword: string,
endMarker: string,
start: number
) {
const declarationEnd =
findFirstOccurrenceOutsideComment(code, declarationKeyword, start) + declarationKeyword.length;
code = code.slice(declarationEnd, findFirstOccurrenceOutsideComment(code, '{', declarationEnd));
code = code.slice(
declarationEnd,
findFirstOccurrenceOutsideComment(code, endMarker, declarationEnd)
);
const generatorStarPos = findFirstOccurrenceOutsideComment(code, '*');
if (generatorStarPos === -1) {
return declarationEnd;
Expand Down Expand Up @@ -71,6 +79,7 @@ export default class ExportDefaultDeclaration extends NodeBase {
code,
declarationStart,
'function',
'(',
this.declaration.id === null,
options
);
Expand All @@ -79,6 +88,7 @@ export default class ExportDefaultDeclaration extends NodeBase {
code,
declarationStart,
'class',
'{',
this.declaration.id === null,
options
);
Expand Down Expand Up @@ -106,6 +116,7 @@ export default class ExportDefaultDeclaration extends NodeBase {
code: MagicString,
declarationStart: number,
declarationKeyword: string,
endMarker: string,
needsId: boolean,
options: RenderOptions
) {
Expand All @@ -115,7 +126,7 @@ export default class ExportDefaultDeclaration extends NodeBase {

if (needsId) {
code.appendLeft(
getIdInsertPosition(code.original, declarationKeyword, declarationStart),
getIdInsertPosition(code.original, declarationKeyword, endMarker, declarationStart),
` ${name}`
);
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions test/form/samples/slash-in-function-parameters/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles slashes in function parameters and correctly inserts missing ids'
};
@@ -0,0 +1,9 @@
define(function () { 'use strict';

class someClass {}

function someFunction (text = '/') {}

console.log(someClass, someFunction);

});
@@ -0,0 +1,7 @@
'use strict';

class someClass {}

function someFunction (text = '/') {}

console.log(someClass, someFunction);
@@ -0,0 +1,5 @@
class someClass {}

function someFunction (text = '/') {}

console.log(someClass, someFunction);
10 changes: 10 additions & 0 deletions test/form/samples/slash-in-function-parameters/_expected/iife.js
@@ -0,0 +1,10 @@
(function () {
'use strict';

class someClass {}

function someFunction (text = '/') {}

console.log(someClass, someFunction);

}());
14 changes: 14 additions & 0 deletions test/form/samples/slash-in-function-parameters/_expected/system.js
@@ -0,0 +1,14 @@
System.register([], function () {
'use strict';
return {
execute: function () {

class someClass {}

function someFunction (text = '/') {}

console.log(someClass, someFunction);

}
};
});
12 changes: 12 additions & 0 deletions test/form/samples/slash-in-function-parameters/_expected/umd.js
@@ -0,0 +1,12 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}((function () { 'use strict';

class someClass {}

function someFunction (text = '/') {}

console.log(someClass, someFunction);

})));
3 changes: 3 additions & 0 deletions test/form/samples/slash-in-function-parameters/class.js
@@ -0,0 +1,3 @@
export default class {}

function* doesNotMatter() {}
3 changes: 3 additions & 0 deletions test/form/samples/slash-in-function-parameters/function.js
@@ -0,0 +1,3 @@
export default function (text = '/') {}

function* doesNotMatter() {}
4 changes: 4 additions & 0 deletions test/form/samples/slash-in-function-parameters/main.js
@@ -0,0 +1,4 @@
import someClass from './class.js';
import someFunction from './function';

console.log(someClass, someFunction);

0 comments on commit f9a6042

Please sign in to comment.