UNPKG

8.66 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16 return new (P || (P = Promise))(function (resolve, reject) {
17 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
20 step((generator = generator.apply(thisArg, _arguments || [])).next());
21 });
22};
23Object.defineProperty(exports, "__esModule", { value: true });
24const whatwg_url_1 = require("whatwg-url");
25const model_1 = require("../model/model");
26const esutil = require("./esutil");
27const resolve_specifier_node_1 = require("./resolve-specifier-node");
28const isWindows = require("is-windows");
29const isPathSpecifier = (s) => /^\.{0,2}\//.test(s);
30class ScannedJavascriptImport extends model_1.ScannedImport {
31 constructor(url, sourceRange, urlSourceRange, ast, astNodePath, lazy, originalSpecifier, statementAst) {
32 super('js-import', url, sourceRange, urlSourceRange, ast, lazy);
33 this.specifier = originalSpecifier;
34 this.statementAst = statementAst;
35 this.astNode = ast;
36 this.astNodePath = astNodePath;
37 }
38 constructImport(resolvedUrl, relativeUrl, importedDocument, _containingDocument) {
39 return new JavascriptImport(resolvedUrl, relativeUrl, this.type, importedDocument, this.sourceRange, this.urlSourceRange, this.astNode, this.astNodePath, this.warnings, this.lazy, this.specifier, this.statementAst);
40 }
41}
42exports.ScannedJavascriptImport = ScannedJavascriptImport;
43class JavascriptImport extends model_1.Import {
44 constructor(url, originalUrl, type, document, sourceRange, urlSourceRange, ast, astNodePath, warnings, lazy, specifier, statementAst) {
45 super(url, originalUrl, type, document, sourceRange, urlSourceRange, ast, warnings, lazy);
46 this.specifier = specifier;
47 this.statementAst = statementAst;
48 this.astNode = ast;
49 this.astNodePath = astNodePath;
50 }
51}
52exports.JavascriptImport = JavascriptImport;
53class JavaScriptImportScanner {
54 constructor(options) {
55 this.moduleResolution = options && options.moduleResolution;
56 }
57 scan(document, visit) {
58 return __awaiter(this, void 0, void 0, function* () {
59 const imports = [];
60 const warnings = [];
61 const scanner = this;
62 yield visit({
63 enterCallExpression(node, _, path) {
64 // TODO(usergenic): There's no babel.Import type or babel.isImport()
65 // function right now, we have to just check the type property
66 // here until there is; please change to use babel.isImport(node.callee)
67 // once it is a thing.
68 if (node.callee.type !== 'Import') {
69 return;
70 }
71 if (node.arguments.length !== 1) {
72 warnings.push(new model_1.Warning({
73 message: 'Malformed import',
74 sourceRange: document.sourceRangeForNode(node),
75 severity: model_1.Severity.WARNING,
76 code: 'malformed-import',
77 parsedDocument: document,
78 }));
79 return;
80 }
81 const arg = node.arguments[0];
82 if (arg.type !== 'StringLiteral') {
83 warnings.push(new model_1.Warning({
84 message: 'Cannot analyze dynamic imports with non-literal arguments',
85 sourceRange: document.sourceRangeForNode(node),
86 severity: model_1.Severity.WARNING,
87 code: 'non-literal-import',
88 parsedDocument: document,
89 }));
90 return;
91 }
92 const specifier = arg.value;
93 imports.push(new ScannedJavascriptImport(scanner._resolveSpecifier(specifier, document, node, warnings), document.sourceRangeForNode(node), document.sourceRangeForNode(node.callee), { language: 'js', node, containingDocument: document }, path, true, specifier, esutil.getCanonicalStatement(path)));
94 },
95 enterImportDeclaration(node, _, path) {
96 const specifier = node.source.value;
97 imports.push(new ScannedJavascriptImport(scanner._resolveSpecifier(specifier, document, node, warnings), document.sourceRangeForNode(node), document.sourceRangeForNode(node.source), { language: 'js', node, containingDocument: document }, path, false, specifier, esutil.getCanonicalStatement(path)));
98 },
99 enterExportAllDeclaration(node, _parent, path) {
100 const specifier = node.source.value;
101 imports.push(new ScannedJavascriptImport(scanner._resolveSpecifier(specifier, document, node, warnings), document.sourceRangeForNode(node), document.sourceRangeForNode(node.source), { language: 'js', node, containingDocument: document }, path, false, specifier, esutil.getCanonicalStatement(path)));
102 },
103 enterExportNamedDeclaration(node, _parent, path) {
104 if (node.source == null) {
105 return;
106 }
107 const specifier = node.source.value;
108 imports.push(new ScannedJavascriptImport(scanner._resolveSpecifier(specifier, document, node, warnings), document.sourceRangeForNode(node), document.sourceRangeForNode(node.source), { language: 'js', node, containingDocument: document }, path, false, specifier, esutil.getCanonicalStatement(path)));
109 }
110 });
111 return { features: imports, warnings };
112 });
113 }
114 _resolveSpecifier(specifier, document, node, warnings) {
115 if (whatwg_url_1.parseURL(specifier) !== null) {
116 return specifier;
117 }
118 const documentURL = new whatwg_url_1.URL(document.baseUrl);
119 if (documentURL.protocol !== 'file:') {
120 warnings.push(new model_1.Warning({
121 message: 'Cannot resolve module specifier in non-local document',
122 sourceRange: document.sourceRangeForNode(node),
123 severity: model_1.Severity.WARNING,
124 code: 'cant-resolve-module-specifier',
125 parsedDocument: document,
126 }));
127 return undefined;
128 }
129 let documentPath = decodeURIComponent(documentURL.pathname);
130 if (isWindows() && documentPath.startsWith('/')) {
131 documentPath = documentPath.substring(1);
132 }
133 if (this.moduleResolution === 'node') {
134 try {
135 // TODO (justinfagnani): we need to pass packageName and componentDir,
136 // or maybe the UrlResolver, to get correct relative URLs for
137 // named imports from the root package to its dependencies
138 return resolve_specifier_node_1.resolve(specifier, documentPath);
139 }
140 catch (e) {
141 // If the specifier was a name, we'll emit a warning below.
142 }
143 }
144 if (isPathSpecifier(specifier)) {
145 return specifier;
146 }
147 warnings.push(new model_1.Warning({
148 message: 'Cannot resolve module specifier with resolution algorithm: ' +
149 (this.moduleResolution || 'none'),
150 sourceRange: document.sourceRangeForNode(node),
151 severity: model_1.Severity.WARNING,
152 code: 'cant-resolve-module-specifier',
153 parsedDocument: document,
154 }));
155 return undefined;
156 }
157}
158exports.JavaScriptImportScanner = JavaScriptImportScanner;
159//# sourceMappingURL=javascript-import-scanner.js.map
\No newline at end of file