2015-01-13 00:45:30 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-01-03 05:30:08 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDependency");
|
|
|
|
const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
|
|
|
|
const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
|
|
|
|
const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
|
|
|
|
const HarmonyImportDependency = require("./HarmonyImportDependency");
|
|
|
|
const HarmonyModulesHelpers = require("./HarmonyModulesHelpers");
|
2015-01-13 00:45:30 +08:00
|
|
|
|
2017-01-03 05:30:08 +08:00
|
|
|
module.exports = class HarmonyExportDependencyParserPlugin {
|
|
|
|
apply(parser) {
|
|
|
|
parser.plugin("export", statement => {
|
|
|
|
const dep = new HarmonyExportHeaderDependency(statement.declaration && statement.declaration.range, statement.range);
|
2016-12-14 19:03:24 +08:00
|
|
|
dep.loc = Object.create(statement.loc);
|
|
|
|
dep.loc.index = -1;
|
2017-01-03 05:30:08 +08:00
|
|
|
parser.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
parser.plugin("export import", (statement, source) => {
|
|
|
|
const dep = new HarmonyImportDependency(source, HarmonyModulesHelpers.getNewModuleVar(parser.state, source), statement.range);
|
2016-12-14 19:03:24 +08:00
|
|
|
dep.loc = Object.create(statement.loc);
|
|
|
|
dep.loc.index = -1;
|
2017-01-03 05:30:08 +08:00
|
|
|
parser.state.current.addDependency(dep);
|
2017-03-18 20:40:34 +08:00
|
|
|
parser.state.lastHarmonyImport = dep;
|
2017-01-03 05:30:08 +08:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
parser.plugin("export expression", (statement, expr) => {
|
|
|
|
const dep = new HarmonyExportExpressionDependency(parser.state.module, expr.range, statement.range);
|
2016-12-14 19:03:24 +08:00
|
|
|
dep.loc = Object.create(statement.loc);
|
|
|
|
dep.loc.index = -1;
|
2017-01-03 05:30:08 +08:00
|
|
|
parser.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
parser.plugin("export declaration", statement => {});
|
2016-12-14 19:03:24 +08:00
|
|
|
parser.plugin("export specifier", (statement, id, name, idx) => {
|
2017-01-03 05:30:08 +08:00
|
|
|
const rename = parser.scope.renames[`$${id}`];
|
|
|
|
let dep;
|
|
|
|
if(rename === "imported var") {
|
|
|
|
const settings = parser.state.harmonySpecifier[`$${id}`];
|
|
|
|
dep = new HarmonyExportImportedSpecifierDependency(parser.state.module, settings[0], settings[1], settings[2], name);
|
|
|
|
} else {
|
|
|
|
const immutable = statement.declaration && isImmutableStatement(statement.declaration);
|
|
|
|
const hoisted = statement.declaration && isHoistedStatement(statement.declaration);
|
2017-05-04 18:56:31 +08:00
|
|
|
dep = new HarmonyExportSpecifierDependency(parser.state.module, id, name, !immutable || hoisted ? -2 : (statement.range[1] + 0.5), immutable);
|
2017-01-03 05:30:08 +08:00
|
|
|
}
|
2016-12-14 19:03:24 +08:00
|
|
|
dep.loc = Object.create(statement.loc);
|
|
|
|
dep.loc.index = idx;
|
2017-01-03 05:30:08 +08:00
|
|
|
parser.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
2016-12-14 19:03:24 +08:00
|
|
|
parser.plugin("export import specifier", (statement, source, id, name, idx) => {
|
2017-03-18 20:40:34 +08:00
|
|
|
const dep = new HarmonyExportImportedSpecifierDependency(parser.state.module, parser.state.lastHarmonyImport, HarmonyModulesHelpers.getModuleVar(parser.state, source), id, name);
|
2016-12-14 19:03:24 +08:00
|
|
|
dep.loc = Object.create(statement.loc);
|
|
|
|
dep.loc.index = idx;
|
2017-01-03 05:30:08 +08:00
|
|
|
parser.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
});
|
2015-01-13 00:45:30 +08:00
|
|
|
}
|
2017-01-03 05:30:08 +08:00
|
|
|
};
|
2015-10-31 21:43:44 +08:00
|
|
|
|
|
|
|
function isImmutableStatement(statement) {
|
|
|
|
if(statement.type === "FunctionDeclaration") return true;
|
|
|
|
if(statement.type === "ClassDeclaration") return true;
|
2016-06-04 21:06:10 +08:00
|
|
|
if(statement.type === "VariableDeclaration" && statement.kind === "const") return true;
|
2015-10-31 21:43:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isHoistedStatement(statement) {
|
|
|
|
if(statement.type === "FunctionDeclaration") return true;
|
|
|
|
return false;
|
|
|
|
}
|