1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const utils_1 = require("../core/utils");
|
17 | const document_1 = require("./document");
|
18 | const source_range_1 = require("./source-range");
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | const MATCHES_EXTERNAL = /(^|\/)(bower_components|node_modules($|\/))/;
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | class Analysis {
|
34 | constructor(results, context) {
|
35 | this.context = context;
|
36 | workAroundDuplicateJsScriptsBecauseOfHtmlScriptTags(results);
|
37 | this._results = results;
|
38 | const documents = Array.from(results.values()).filter((r) => r instanceof document_1.Document);
|
39 | const potentialRoots = new Set(documents);
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | for (const doc of potentialRoots) {
|
45 | for (const imprt of doc.getFeatures({ kind: 'import', imported: true })) {
|
46 |
|
47 |
|
48 | if (imprt.document !== undefined && imprt.document !== doc) {
|
49 | potentialRoots.delete(imprt.document);
|
50 | }
|
51 | }
|
52 | }
|
53 | this._searchRoots = potentialRoots;
|
54 | }
|
55 | static isExternal(path) {
|
56 | return MATCHES_EXTERNAL.test(path);
|
57 | }
|
58 | getDocument(packageRelativeUrl) {
|
59 | const url = this.context.resolver.resolve(packageRelativeUrl);
|
60 | if (url === undefined) {
|
61 | return { successful: false, error: undefined };
|
62 | }
|
63 | const result = this._results.get(url);
|
64 | if (result != null) {
|
65 | if (result instanceof document_1.Document) {
|
66 | return { successful: true, value: result };
|
67 | }
|
68 | else {
|
69 | return { successful: false, error: result };
|
70 | }
|
71 | }
|
72 | const documents = Array
|
73 | .from(this.getFeatures({ kind: 'document', id: url, externalPackages: true }))
|
74 | .filter((d) => !d.isInline);
|
75 | if (documents.length !== 1) {
|
76 | return { successful: false, error: undefined };
|
77 | }
|
78 | return { successful: true, value: documents[0] };
|
79 | }
|
80 | getFeatures(query = {}) {
|
81 | const result = new Set();
|
82 | const docQuery = this._getDocumentQuery(query);
|
83 | for (const doc of this._searchRoots) {
|
84 | utils_1.addAll(result, doc.getFeatures(docQuery));
|
85 | }
|
86 | return result;
|
87 | }
|
88 | |
89 |
|
90 |
|
91 | getWarnings(options) {
|
92 | const warnings = Array.from(this._results.values())
|
93 | .filter((r) => !(r instanceof document_1.Document));
|
94 | const result = new Set(warnings);
|
95 | const docQuery = this._getDocumentQuery(options);
|
96 | for (const doc of this._searchRoots) {
|
97 | utils_1.addAll(result, new Set(doc.getWarnings(docQuery)));
|
98 | }
|
99 | return Array.from(result);
|
100 | }
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | getDocumentContaining(sourceRange) {
|
109 | if (!sourceRange) {
|
110 | return undefined;
|
111 | }
|
112 | let mostSpecificDocument = undefined;
|
113 | const [outerDocument] = this.getFeatures({ kind: 'document', id: sourceRange.file });
|
114 | if (!outerDocument) {
|
115 | return undefined;
|
116 | }
|
117 | for (const doc of outerDocument.getFeatures({ kind: 'document' })) {
|
118 | if (source_range_1.isPositionInsideRange(sourceRange.start, doc.sourceRange)) {
|
119 | if (!mostSpecificDocument ||
|
120 | source_range_1.isPositionInsideRange(doc.sourceRange.start, mostSpecificDocument.sourceRange)) {
|
121 | mostSpecificDocument = doc;
|
122 | }
|
123 | }
|
124 | }
|
125 | return mostSpecificDocument;
|
126 | }
|
127 | _getDocumentQuery(query = {}) {
|
128 | return {
|
129 | kind: query.kind,
|
130 | id: query.id,
|
131 | externalPackages: query.externalPackages,
|
132 | imported: true,
|
133 | excludeBackreferences: query.excludeBackreferences,
|
134 | noLazyImports: query.noLazyImports,
|
135 | };
|
136 | }
|
137 | }
|
138 | exports.Analysis = Analysis;
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | function workAroundDuplicateJsScriptsBecauseOfHtmlScriptTags(results) {
|
153 | const documents = Array.from(results.values()).filter((r) => r instanceof document_1.Document);
|
154 |
|
155 |
|
156 | const potentialDuplicates = new Set(documents.filter((r) => r.kinds.has('js-document')));
|
157 | const canonicalOverrides = new Set();
|
158 | for (const doc of documents) {
|
159 | if (potentialDuplicates.has(doc)) {
|
160 | continue;
|
161 | }
|
162 | for (const potentialDupe of potentialDuplicates) {
|
163 | const potentialCanonicalDocs = doc.getFeatures({ kind: 'js-document', id: potentialDupe.url, imported: true });
|
164 | for (const potentialCanonicalDoc of potentialCanonicalDocs) {
|
165 | if (!potentialCanonicalDoc.isInline) {
|
166 | canonicalOverrides.add(potentialCanonicalDoc);
|
167 | }
|
168 | }
|
169 | }
|
170 | }
|
171 | for (const canonicalDoc of canonicalOverrides) {
|
172 | results.set(canonicalDoc.url, canonicalDoc);
|
173 | }
|
174 | }
|
175 |
|
\ | No newline at end of file |