UNPKG

1.99 kBPlain TextView Raw
1/**
2 * @license
3 * Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at
5 * http://polymer.github.io/LICENSE.txt The complete set of authors may be found
6 * at http://polymer.github.io/AUTHORS.txt The complete set of contributors may
7 * be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by
8 * Google as part of the polymer project is also subject to an additional IP
9 * rights grant found at http://polymer.github.io/PATENTS.txt
10 */
11
12import * as analyzer from 'polymer-analyzer';
13
14/**
15 * Return whether an Analyzer document is a JavaScript document which was parsed
16 * as a module.
17 */
18export function isEsModuleDocument(doc: analyzer.Document):
19 doc is analyzer.Document<analyzer.ParsedJavaScriptDocument> {
20 return doc.type === 'js' &&
21 (doc.parsedDocument as analyzer.ParsedJavaScriptDocument)
22 .parsedAsSourceType === 'module';
23}
24
25/**
26 * Resolve an identifier being imported or exported to the feature it refers to.
27 */
28export function resolveImportExportFeature(
29 feature: analyzer.JavascriptImport|analyzer.Export,
30 identifier: string,
31 doc: analyzer.Document): analyzer.Reference<ResolvedTypes>|undefined {
32 for (const kind of resolveKinds) {
33 const reference = new analyzer.ScannedReference(
34 kind,
35 identifier,
36 feature.sourceRange,
37 feature.astNode,
38 feature.astNodePath);
39 const resolved = reference.resolve(doc);
40 if (resolved.feature !== undefined) {
41 return resolved as analyzer.Reference<ResolvedTypes>;
42 }
43 }
44 return undefined;
45}
46
47const resolveKinds: Array<keyof analyzer.FeatureKindMap> = [
48 'element',
49 'behavior',
50 'element-mixin',
51 'class',
52 'function',
53 'namespace',
54];
55
56export type ResolvedTypes = analyzer.Element|analyzer.PolymerBehavior|
57 analyzer.ElementMixin|analyzer.Class|
58 analyzer.Function|analyzer.Namespace;