refactor HarmonyImportSpecifierDependency to es6 (#3734)

This commit is contained in:
timse 2017-01-05 05:50:35 +11:00 committed by Sean Larkin
parent 61251de9c0
commit 8fd2957417
1 changed files with 94 additions and 64 deletions

View File

@ -2,79 +2,109 @@
MIT License http://www.opensource.org/licenses/mit-license.php MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra Author Tobias Koppers @sokra
*/ */
var NullDependency = require("./NullDependency"); "use strict";
const NullDependency = require("./NullDependency");
function HarmonyImportSpecifierDependency(importDependency, importedVar, id, name, range) { class HarmonyImportSpecifierDependency extends NullDependency {
NullDependency.call(this); constructor(importDependency, importedVar, id, name, range) {
this.importDependency = importDependency; super();
this.importedVar = importedVar; this.importDependency = importDependency;
this.id = id; this.importedVar = importedVar;
this.name = name; this.id = id;
this.range = range; this.name = name;
this.range = range;
}
get type() {
return "harmony import specifier";
}
getReference() {
if(!this.importDependency.module) return null;
return {
module: this.importDependency.module,
importedNames: this.id ? [this.id] : true
};
}
getWarnings() {
const importedModule = this.importDependency.module;
if(!importedModule || !importedModule.meta || !importedModule.meta.harmonyModule) {
return;
}
if(!this.id) {
return;
}
if(importedModule.isProvided(this.id) !== false) {
return;
}
const idIsNotNameMessage = this.id !== this.name ? ` (imported as '${this.name}')` : "";
const errorMessage = `"export '${this.id}'${idIsNotNameMessage} was not found in '${this.importDependency.userRequest}'`;
const err = new Error(errorMessage);
err.hideStack = true;
return [err];
}
updateHash(hash) {
super.updateHash(hash);
const importedModule = this.importDependency.module;
hash.update((importedModule && importedModule.id) + "");
hash.update((importedModule && this.id) + "");
hash.update((importedModule && this.importedVar) + "");
hash.update((importedModule && this.id && importedModule.isUsed(this.id)) + "");
hash.update((importedModule && (!importedModule.meta || importedModule.meta.harmonyModule)) + "");
hash.update((importedModule && (importedModule.used + JSON.stringify(importedModule.usedExports))) + "");
}
} }
module.exports = HarmonyImportSpecifierDependency;
HarmonyImportSpecifierDependency.prototype = Object.create(NullDependency.prototype); HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate {
HarmonyImportSpecifierDependency.prototype.constructor = HarmonyImportSpecifierDependency; apply(dep, source) {
HarmonyImportSpecifierDependency.prototype.type = "harmony import specifier"; const content = this.getContent(dep);
source.replace(dep.range[0], dep.range[1] - 1, content);
}
HarmonyImportSpecifierDependency.prototype.getReference = function() { getContent(dep) {
if(!this.importDependency.module) return null; const importedModule = dep.importDependency.module;
return { const defaultImport = dep.directImport && dep.id === "default" && !(importedModule && (!importedModule.meta || importedModule.meta.harmonyModule));
module: this.importDependency.module, const shortHandPrefix = this.getShortHandPrefix(dep);
importedNames: this.id ? [this.id] : true const importedVar = dep.importedVar;
}; const importedVarSuffix = this.getImportVarSuffix(dep, defaultImport, importedModule);
};
HarmonyImportSpecifierDependency.prototype.getWarnings = function() { if(dep.call && defaultImport) {
var importedModule = this.importDependency.module; return `${shortHandPrefix}${importedVar}_default()`;
if(importedModule && importedModule.meta && importedModule.meta.harmonyModule) {
if(this.id && importedModule.isProvided(this.id) === false) {
var err = new Error("export '" + this.id + "'" +
(this.id !== this.name ? " (imported as '" + this.name + "')" : "") +
" was not found in '" + this.importDependency.userRequest + "'");
err.hideStack = true;
return [
err
];
} }
if(dep.call && dep.id) {
return `${shortHandPrefix}__webpack_require__.i(${importedVar}${importedVarSuffix})`
}
return `${shortHandPrefix}${importedVar}${importedVarSuffix}`;
} }
};
HarmonyImportSpecifierDependency.prototype.updateHash = function(hash) { getImportVarSuffix(dep, defaultImport, importedModule) {
NullDependency.prototype.updateHash.call(this, hash);
var importedModule = this.importDependency.module;
hash.update((importedModule && importedModule.id) + "");
hash.update((importedModule && this.id) + "");
hash.update((importedModule && this.importedVar) + "");
hash.update((importedModule && this.id && importedModule.isUsed(this.id)) + "");
hash.update((importedModule && (!importedModule.meta || importedModule.meta.harmonyModule)) + "");
hash.update((importedModule && (importedModule.used + JSON.stringify(importedModule.usedExports))) + "");
};
HarmonyImportSpecifierDependency.Template = function HarmonyImportSpecifierDependencyTemplate() {};
HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source) {
var content;
var importedModule = dep.importDependency.module;
var defaultImport = dep.directImport && dep.id === "default" && !(importedModule && (!importedModule.meta || importedModule.meta.harmonyModule));
if(defaultImport) {
content = dep.importedVar + "_default.a";
} else if(dep.id) {
var used = importedModule ? importedModule.isUsed(dep.id) : dep.id;
content = dep.importedVar + "[" + JSON.stringify(used) + (dep.id !== used ? " /* " + dep.id + " */" : "") + "]";
} else {
content = dep.importedVar;
}
if(dep.call) {
if(defaultImport) { if(defaultImport) {
content = dep.importedVar + "_default()"; return "_default.a";
} else if(dep.id) {
content = "__webpack_require__.i(" + content + ")";
} }
if(dep.id) {
const used = importedModule ? importedModule.isUsed(dep.id) : dep.id;
const optionalComment = dep.id !== used ? " /* " + dep.id + " */" : "";
return `[${JSON.stringify(used)}${optionalComment}]`;
}
return "";
} }
if(dep.shorthand) {
content = dep.name + ": " + content; getShortHandPrefix(dep) {
if(!dep.shorthand) {
return "";
}
return dep.name + ": ";
} }
source.replace(dep.range[0], dep.range[1] - 1, content); }
};
module.exports = HarmonyImportSpecifierDependency;