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
Author Tobias Koppers @sokra
*/
var NullDependency = require("./NullDependency");
"use strict";
const NullDependency = require("./NullDependency");
function HarmonyImportSpecifierDependency(importDependency, importedVar, id, name, range) {
NullDependency.call(this);
this.importDependency = importDependency;
this.importedVar = importedVar;
this.id = id;
this.name = name;
this.range = range;
class HarmonyImportSpecifierDependency extends NullDependency {
constructor(importDependency, importedVar, id, name, range) {
super();
this.importDependency = importDependency;
this.importedVar = importedVar;
this.id = id;
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.prototype.constructor = HarmonyImportSpecifierDependency;
HarmonyImportSpecifierDependency.prototype.type = "harmony import specifier";
HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate {
apply(dep, source) {
const content = this.getContent(dep);
source.replace(dep.range[0], dep.range[1] - 1, content);
}
HarmonyImportSpecifierDependency.prototype.getReference = function() {
if(!this.importDependency.module) return null;
return {
module: this.importDependency.module,
importedNames: this.id ? [this.id] : true
};
};
getContent(dep) {
const importedModule = dep.importDependency.module;
const defaultImport = dep.directImport && dep.id === "default" && !(importedModule && (!importedModule.meta || importedModule.meta.harmonyModule));
const shortHandPrefix = this.getShortHandPrefix(dep);
const importedVar = dep.importedVar;
const importedVarSuffix = this.getImportVarSuffix(dep, defaultImport, importedModule);
HarmonyImportSpecifierDependency.prototype.getWarnings = function() {
var importedModule = this.importDependency.module;
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 && defaultImport) {
return `${shortHandPrefix}${importedVar}_default()`;
}
if(dep.call && dep.id) {
return `${shortHandPrefix}__webpack_require__.i(${importedVar}${importedVarSuffix})`
}
return `${shortHandPrefix}${importedVar}${importedVarSuffix}`;
}
};
HarmonyImportSpecifierDependency.prototype.updateHash = function(hash) {
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) {
getImportVarSuffix(dep, defaultImport, importedModule) {
if(defaultImport) {
content = dep.importedVar + "_default()";
} else if(dep.id) {
content = "__webpack_require__.i(" + content + ")";
return "_default.a";
}
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;