Skip to content

Commit

Permalink
Refactored the code to satisfy changed tests (new default iife consis…
Browse files Browse the repository at this point in the history
…tent behaviour). Still 5 failing tests - all of them have illegal moduleNames. --extends will make them work.
  • Loading branch information
Andarist committed Jun 30, 2017
1 parent 9d122ad commit 269949f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Declaration.js
@@ -1,5 +1,5 @@
import { blank, forOwn, keys } from './utils/object.js';
import makeLegalIdentifier, { reservedWords } from './utils/makeLegalIdentifier.js';
import { makeLegal, reservedWords } from './utils/identifier-helpers.js';
import { UNKNOWN } from './ast/values.js';

export default class Declaration {
Expand All @@ -25,7 +25,7 @@ export default class Declaration {
reference.declaration = this;

if ( reference.name !== this.name ) {
this.name = makeLegalIdentifier( reference.name ); // TODO handle differences of opinion
this.name = makeLegal( reference.name ); // TODO handle differences of opinion
}

if ( reference.isReassignment ) this.isReassigned = true;
Expand Down
4 changes: 2 additions & 2 deletions src/ExternalModule.js
@@ -1,13 +1,13 @@
import { blank } from './utils/object.js';
import makeLegalIdentifier from './utils/makeLegalIdentifier.js';
import { makeLegal } from './utils/identifier-helpers.js';
import { ExternalDeclaration } from './Declaration.js';

export default class ExternalModule {
constructor ( id, relativePath ) {
this.id = id;
this.path = relativePath;

this.name = makeLegalIdentifier( relativePath );
this.name = makeLegal( relativePath );

this.nameSuggestions = blank();
this.mostCommonSuggestion = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/Module.js
Expand Up @@ -4,7 +4,7 @@ import { locate } from 'locate-character';
import { timeStart, timeEnd } from './utils/flushTime.js';
import { assign, blank, keys } from './utils/object.js';
import { basename, extname } from './utils/path.js';
import makeLegalIdentifier from './utils/makeLegalIdentifier.js';
import { makeLegal } from './utils/identifier-helpers.js';
import getCodeFrame from './utils/getCodeFrame.js';
import { SOURCEMAPPING_URL_RE } from './utils/sourceMappingURL.js';
import error from './utils/error.js';
Expand Down Expand Up @@ -251,7 +251,7 @@ export default class Module {
const base = basename( this.id );
const ext = extname( this.id );

return makeLegalIdentifier( ext ? base.slice( 0, -ext.length ) : base );
return makeLegal( ext ? base.slice( 0, -ext.length ) : base );
}

bindImportSpecifiers () {
Expand Down
16 changes: 14 additions & 2 deletions src/finalisers/iife.js
Expand Up @@ -7,6 +7,7 @@ import getGlobalNameMaker from './shared/getGlobalNameMaker.js';
import { property, keypath } from './shared/sanitize.js';
import warnOnBuiltins from './shared/warnOnBuiltins.js';
import trimEmptyImports from './shared/trimEmptyImports.js';
import { isLegal } from '../utils/identifier-helpers.js';

function setupNamespace ( keypath ) {
const parts = keypath.split( '.' );
Expand All @@ -26,6 +27,14 @@ export default function iife ( bundle, magicString, { exportMode, indentString,
const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle, 'null' );

const name = options.moduleName;

if ( name && !isLegal(name) ) {
error({
code: 'ILLEGAL_IDENTIFIER_AS_NAME',
message: `Given moduleName - ${ name } - is not legal JS identifier.`
})
}

const isNamespaced = name && ~name.indexOf( '.' );

warnOnBuiltins( bundle );
Expand All @@ -41,17 +50,20 @@ export default function iife ( bundle, magicString, { exportMode, indentString,
});
}

if ( exportMode === 'named' ) {
if ( isNamespaced ) {
dependencies.unshift( `(${thisProp(name)} = ${thisProp(name)} || {})` );
args.unshift( 'exports' );
} else if ( exportMode === 'named' ) {
dependencies.unshift( '{}' );
args.unshift( 'exports' );
}

const useStrict = options.useStrict !== false ? `${indentString}'use strict';\n\n` : ``;

let wrapperIntro = `(function (${args}) {\n${useStrict}`;
const wrapperOutro = `\n\n}(${dependencies}));`;

if ( exportMode === 'default' ) {
if ( exportMode !== 'none' ) {
wrapperIntro = ( isNamespaced ? thisProp(name) : `${bundle.varOrConst} ${name}` ) + ` = ${wrapperIntro}`;
}

Expand Down
Expand Up @@ -6,13 +6,26 @@ const builtins = 'Infinity NaN undefined null true false eval uneval isFinite is
const blacklisted = blank();
reservedWords.concat( builtins ).forEach( word => blacklisted[ word ] = true );

const illegalCharacters = /[^$_a-zA-Z0-9]/g;

export default function makeLegalIdentifier ( str ) {
const startsWithDigit = str => /\d/.test( str[0] );

export function isLegal ( str ) {
if ( startsWithDigit(str) || blacklisted[ str ] ) {
return false;
}
if ( illegalCharacters.test(str) ) {
return false;
}
return true;
}

export function makeLegal ( str ) {
str = str
.replace( /-(\w)/g, ( _, letter ) => letter.toUpperCase() )
.replace( /[^$_a-zA-Z0-9]/g, '_' );
.replace( illegalCharacters, '_' );

if ( /\d/.test( str[0] ) || blacklisted[ str ] ) str = `_${str}`;
if ( startsWithDigit(str) || blacklisted[ str ] ) str = `_${str}`;

return str;
}

0 comments on commit 269949f

Please sign in to comment.