Skip to content

Commit 3da1f63

Browse files
committed
Merge pull request #132 from esperantojs/gh-119
Prevent function arguments shadowing imports
2 parents 057f3c1 + 9aea3d2 commit 3da1f63

File tree

7 files changed

+50
-3
lines changed

7 files changed

+50
-3
lines changed

src/standalone/getModule.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default function getStandaloneModule ( options ) {
1919
sourceType: 'module',
2020
onComment ( block, text, start, end ) {
2121
// sourceMappingURL comments should be removed
22-
if ( !block && /^# sourceMappingURL=/.test( text ) ) {
22+
if ( !block && SOURCEMAPPINGURL_REGEX.test( text ) ) {
2323
toRemove.push({ start, end });
2424
}
2525
}
@@ -73,7 +73,7 @@ function determineImportNames ( imports, userFn, usedNames ) {
7373

7474
if ( hasOwnProp.call( usedNames, name ) ) {
7575
// TODO write a test for this
76-
throw new Error( 'Naming collision: module ' + moduleId + ' cannot be called ' + name );
76+
throw new Error( `Naming collision: module ${moduleId} cannot be called ${name}` );
7777
}
7878
}
7979

src/utils/ast/annotate.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import walk from './walk';
8+
import { getName } from '../mappers';
89

910
var Scope = function ( options ) {
1011
options = options || {};
@@ -69,9 +70,13 @@ export default function annotateAst ( ast ) {
6970
}
7071
}
7172

73+
let names = node.params.map( getName );
74+
75+
names.forEach( name => declared[ name ] = true );
76+
7277
scope = node._scope = new Scope({
7378
parent: scope,
74-
params: node.params.map( x => x.name ) // TODO rest params?
79+
params: names // TODO rest params?
7580
});
7681

7782
break;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
description: 'prevents function arguments from shadowing imports',
3+
strict: true
4+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { foo } from 'x/y/z';
2+
3+
export function bar ( z ) {
4+
return foo( z );
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
define(['exports', 'x/y/z'], function (exports, y__z) {
2+
3+
'use strict';
4+
5+
exports.bar = bar;
6+
7+
function bar ( z ) {
8+
return y__z.foo( z );
9+
}
10+
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
exports.bar = bar;
4+
5+
var y__z = require('x/y/z');
6+
7+
function bar ( z ) {
8+
return y__z.foo( z );
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(function (global, factory) {
2+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('x/y/z')) :
3+
typeof define === 'function' && define.amd ? define(['exports', 'x/y/z'], factory) :
4+
factory((global.myModule = {}), global.y__z)
5+
}(this, function (exports, y__z) { 'use strict';
6+
7+
exports.bar = bar;
8+
9+
function bar ( z ) {
10+
return y__z.foo( z );
11+
}
12+
13+
}));

0 commit comments

Comments
 (0)