UNPKG

4.58 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2016 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 */
15Object.defineProperty(exports, "__esModule", { value: true });
16const document_1 = require("./document");
17const warning_1 = require("./warning");
18/**
19 * Represents an import, such as an HTML import, an external script or style
20 * tag, or an JavaScript import.
21 *
22 * @template N The AST node type
23 */
24class ScannedImport {
25 constructor(type, url, sourceRange, urlSourceRange, ast, lazy) {
26 this.warnings = [];
27 this.type = type;
28 this.url = url;
29 this.sourceRange = sourceRange;
30 this.urlSourceRange = urlSourceRange;
31 this.astNode = ast;
32 this.lazy = lazy;
33 }
34 resolve(document) {
35 const resolvedUrl = this.getLoadableUrlOrWarn(document);
36 if (this.url === undefined || resolvedUrl === undefined) {
37 // Warning will already have been added to the document if necessary, so
38 // we can just return here.
39 return undefined;
40 }
41 const importedDocumentOrWarning = document._analysisContext.getDocument(resolvedUrl);
42 let importedDocument;
43 if (!(importedDocumentOrWarning instanceof document_1.Document)) {
44 this.addCouldNotLoadWarning(document, importedDocumentOrWarning);
45 importedDocument = undefined;
46 }
47 else {
48 importedDocument = importedDocumentOrWarning;
49 }
50 return this.constructImport(resolvedUrl, this.url, importedDocument, document);
51 }
52 constructImport(resolvedUrl, relativeUrl, importedDocument, _containingDocument) {
53 return new Import(resolvedUrl, relativeUrl, this.type, importedDocument, this.sourceRange, this.urlSourceRange, this.astNode, this.warnings, this.lazy);
54 }
55 addCouldNotLoadWarning(document, warning) {
56 const error = this.error && this.error.message || this.error ||
57 warning && warning.message || '';
58 if (error) {
59 document.warnings.push(new warning_1.Warning({
60 code: 'could-not-load',
61 message: `Unable to load import: ${error}`,
62 sourceRange: (this.urlSourceRange || this.sourceRange),
63 severity: warning_1.Severity.ERROR,
64 parsedDocument: document.parsedDocument,
65 }));
66 }
67 }
68 /**
69 * Resolve the URL for this import and return it if the analyzer
70 */
71 getLoadableUrlOrWarn(document) {
72 if (this.url === undefined) {
73 return;
74 }
75 const resolvedUrl = document._analysisContext.resolver.resolve(document.parsedDocument.baseUrl, this.url, this);
76 if (!resolvedUrl) {
77 return;
78 }
79 if (!document._analysisContext.loader.canLoad(resolvedUrl)) {
80 // We have no way to load this resolved URL, so we will warn.
81 document.warnings.push(new warning_1.Warning({
82 code: 'not-loadable',
83 message: `URL loader not configured to load '${resolvedUrl}'`,
84 sourceRange: (this.urlSourceRange || this.sourceRange),
85 severity: warning_1.Severity.WARNING,
86 parsedDocument: document.parsedDocument,
87 }));
88 return undefined;
89 }
90 return resolvedUrl;
91 }
92}
93exports.ScannedImport = ScannedImport;
94class Import {
95 constructor(url, originalUrl, type, document, sourceRange, urlSourceRange, ast, warnings, lazy) {
96 this.identifiers = new Set();
97 this.kinds = new Set(['import']);
98 this.url = url;
99 this.originalUrl = originalUrl;
100 this.type = type;
101 this.document = document;
102 this.kinds.add(this.type);
103 this.sourceRange = sourceRange;
104 this.urlSourceRange = urlSourceRange;
105 this.astNode = ast;
106 this.warnings = warnings;
107 this.lazy = lazy;
108 if (lazy) {
109 this.kinds.add('lazy-import');
110 }
111 }
112 toString() {
113 return `<Import type="${this.type}" url="${this.url}">`;
114 }
115}
116exports.Import = Import;
117//# sourceMappingURL=import.js.map
\No newline at end of file