|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var esprima = require("esprima"); |
| 5 | +var esprima = require("esprima-fb"); |
6 | 6 | var Tapable = require("tapable"); |
7 | 7 | var BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); |
8 | 8 |
|
@@ -446,6 +446,75 @@ Parser.prototype.walkFunctionDeclaration = function walkFunctionDeclaration(stat |
446 | 446 | }.bind(this)); |
447 | 447 | }; |
448 | 448 |
|
| 449 | +Parser.prototype.walkImportDeclaration = function walkImportDeclaration(statement) { |
| 450 | + var source = statement.source.value; |
| 451 | + this.applyPluginsBailResult("import", statement, source); |
| 452 | + statement.specifiers.forEach(function(specifier) { |
| 453 | + switch(specifier.type) { |
| 454 | + case "ImportDefaultSpecifier": |
| 455 | + var name = specifier.id.name; |
| 456 | + this.scope.renames["$"+name] = undefined; |
| 457 | + this.scope.definitions.push(name); |
| 458 | + this.applyPluginsBailResult("import specifier", statement, source, "default", name); |
| 459 | + break; |
| 460 | + case "ImportSpecifier": |
| 461 | + var name = (specifier.name || specifier.id).name; |
| 462 | + this.scope.renames["$"+name] = undefined; |
| 463 | + this.scope.definitions.push(name); |
| 464 | + this.applyPluginsBailResult("import specifier", statement, source, specifier.id.name, name); |
| 465 | + break; |
| 466 | + case "ImportNamespaceSpecifier": |
| 467 | + var name = specifier.id.name; |
| 468 | + this.scope.renames["$"+name] = undefined; |
| 469 | + this.scope.definitions.push(name); |
| 470 | + this.applyPluginsBailResult("import specifier", statement, source, null, name); |
| 471 | + break; |
| 472 | + } |
| 473 | + }, this); |
| 474 | +}; |
| 475 | + |
| 476 | +Parser.prototype.walkExportDeclaration = function walkExportDeclaration(statement) { |
| 477 | + if(statement.source) { |
| 478 | + var source = statement.source.value; |
| 479 | + this.applyPluginsBailResult("export import", statement, source); |
| 480 | + } else { |
| 481 | + this.applyPluginsBailResult("export", statement); |
| 482 | + } |
| 483 | + if(statement.declaration) { |
| 484 | + if(/Expression$/.test(statement.declaration.type)) { |
| 485 | + if(!this.applyPluginsBailResult("export expression", statement, statement.declaration)) { |
| 486 | + this.walkExpression(statement.declaration); |
| 487 | + this.applyPluginsBailResult("export specifier", statement, statement.declaration, "default"); |
| 488 | + } |
| 489 | + } else { |
| 490 | + if(!this.applyPluginsBailResult("export declaration", statement, statement.declaration)) { |
| 491 | + var pos = this.scope.definitions.length; |
| 492 | + this.walkStatement(statement.declaration); |
| 493 | + var newDefs = this.scope.definitions.slice(pos); |
| 494 | + newDefs.forEach(function(def) { |
| 495 | + this.applyPluginsBailResult("export specifier", statement, def, statement["default"] ? "default" : def); |
| 496 | + }, this); |
| 497 | + } |
| 498 | + } |
| 499 | + } |
| 500 | + if(statement.specifiers) { |
| 501 | + statement.specifiers.forEach(function(specifier) { |
| 502 | + switch(specifier.type) { |
| 503 | + case "ExportSpecifier": |
| 504 | + var name = (specifier.name || specifier.id).name; |
| 505 | + if(source) |
| 506 | + this.applyPluginsBailResult("export import specifier", statement, source, specifier.id.name, name); |
| 507 | + else |
| 508 | + this.applyPluginsBailResult("export specifier", statement, specifier.id.name, name); |
| 509 | + break; |
| 510 | + case "ExportBatchSpecifier": |
| 511 | + this.applyPluginsBailResult("export import specifier", statement, source, null, null); |
| 512 | + break; |
| 513 | + } |
| 514 | + }, this); |
| 515 | + } |
| 516 | +}; |
| 517 | + |
449 | 518 | Parser.prototype.walkVariableDeclaration = function walkVariableDeclaration(statement) { |
450 | 519 | if(statement.declarations) |
451 | 520 | this.walkVariableDeclarators(statement.declarations); |
@@ -774,7 +843,7 @@ Parser.prototype.parseCalculatedString = function parseCalculatedString(expressi |
774 | 843 | }); |
775 | 844 |
|
776 | 845 | Parser.prototype.parse = function parse(source, initialState) { |
777 | | - var ast = esprima.parse(source, {range: true, loc: true, raw: true}); |
| 846 | + var ast = esprima.parse(source, {range: true, loc: true, raw: true, sourceType: "module"}); |
778 | 847 | if(!ast || typeof ast !== "object") |
779 | 848 | throw new Error("Source couldn't be parsed"); |
780 | 849 | var oldScope = this.scope; |
|
0 commit comments