From 528b692a38ce06ba753632381ed1b8b647b1936b Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 7 Jun 2016 11:03:14 -0400 Subject: [PATCH] ensure name suggestions are legal identifiers --- src/Declaration.js | 8 ++++++-- test/function/legal-suggested-names/_config.js | 3 +++ test/function/legal-suggested-names/bar.js | 5 +++++ test/function/legal-suggested-names/foo.js | 5 +++++ test/function/legal-suggested-names/helpers.js | 5 +++++ test/function/legal-suggested-names/main.js | 8 ++++++++ 6 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 test/function/legal-suggested-names/_config.js create mode 100644 test/function/legal-suggested-names/bar.js create mode 100644 test/function/legal-suggested-names/foo.js create mode 100644 test/function/legal-suggested-names/helpers.js create mode 100644 test/function/legal-suggested-names/main.js diff --git a/src/Declaration.js b/src/Declaration.js index 0601606..7bf906a 100644 --- a/src/Declaration.js +++ b/src/Declaration.js @@ -1,4 +1,5 @@ import { blank, forOwn, keys } from './utils/object.js'; +import makeLegalIdentifier from './utils/makeLegalIdentifier.js'; import run from './utils/run.js'; import { SyntheticReference } from './Reference.js'; @@ -17,7 +18,7 @@ export default class Declaration { } this.statement = statement; - this.name = null; + this.name = node.id ? node.id.name : node.name; this.exportName = null; this.isParam = isParam; @@ -33,7 +34,10 @@ export default class Declaration { addReference ( reference ) { reference.declaration = this; - this.name = reference.name; // TODO handle differences of opinion + + if ( reference.name !== this.name ) { + this.name = makeLegalIdentifier( reference.name ); // TODO handle differences of opinion + } if ( reference.isReassignment ) this.isReassigned = true; } diff --git a/test/function/legal-suggested-names/_config.js b/test/function/legal-suggested-names/_config.js new file mode 100644 index 0000000..d68f4e7 --- /dev/null +++ b/test/function/legal-suggested-names/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'illegal name suggestions are ignored' +}; diff --git a/test/function/legal-suggested-names/bar.js b/test/function/legal-suggested-names/bar.js new file mode 100644 index 0000000..a6071df --- /dev/null +++ b/test/function/legal-suggested-names/bar.js @@ -0,0 +1,5 @@ +import * as helpers from './helpers.js'; + +export default function bar ( a ) { + return helpers.typeof( a ); +} diff --git a/test/function/legal-suggested-names/foo.js b/test/function/legal-suggested-names/foo.js new file mode 100644 index 0000000..91a1fb7 --- /dev/null +++ b/test/function/legal-suggested-names/foo.js @@ -0,0 +1,5 @@ +import * as helpers from './helpers.js'; + +export default function foo ( a ) { + return helpers.typeof( a ); +} diff --git a/test/function/legal-suggested-names/helpers.js b/test/function/legal-suggested-names/helpers.js new file mode 100644 index 0000000..1938dee --- /dev/null +++ b/test/function/legal-suggested-names/helpers.js @@ -0,0 +1,5 @@ +var _typeof = function ( thing ) { + return typeof thing; +}; + +export { _typeof as typeof }; diff --git a/test/function/legal-suggested-names/main.js b/test/function/legal-suggested-names/main.js new file mode 100644 index 0000000..7c47804 --- /dev/null +++ b/test/function/legal-suggested-names/main.js @@ -0,0 +1,8 @@ +import * as helpers from './helpers.js'; +import foo from './foo.js'; +import bar from './bar.js'; + +assert.equal( helpers.typeof( foo ), 'function' ); +assert.equal( helpers.typeof( bar ), 'function' ); +assert.equal( foo( 1 ), 'number' ); +assert.equal( bar( 2 ), 'number' );