UNPKG

7.26 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const ts = require("typescript");
4const paths_1 = require("../utils/paths");
5const index_1 = require("../models/index");
6const type_parameter_1 = require("./factories/type-parameter");
7const converter_1 = require("./converter");
8class Context {
9 constructor(converter, fileNames, checker, program) {
10 this.converter = converter;
11 this.fileNames = fileNames;
12 this.checker = checker;
13 this.program = program;
14 this.visitStack = [];
15 const project = new index_1.ProjectReflection(converter.name);
16 this.project = project;
17 this.scope = project;
18 if (converter.externalPattern) {
19 this.externalPattern = paths_1.createMinimatch(converter.externalPattern);
20 }
21 }
22 getCompilerOptions() {
23 return this.converter.application.options.getCompilerOptions();
24 }
25 getTypeAtLocation(node) {
26 let nodeType;
27 try {
28 nodeType = this.checker.getTypeAtLocation(node);
29 }
30 catch (error) {
31 }
32 if (!nodeType) {
33 if (node.symbol) {
34 nodeType = this.checker.getDeclaredTypeOfSymbol(node.symbol);
35 }
36 else if (node.parent && node.parent.symbol) {
37 nodeType = this.checker.getDeclaredTypeOfSymbol(node.parent.symbol);
38 }
39 else if (node.parent && node.parent.parent && node.parent.parent.symbol) {
40 nodeType = this.checker.getDeclaredTypeOfSymbol(node.parent.parent.symbol);
41 }
42 }
43 return nodeType;
44 }
45 getSymbolAtLocation(node) {
46 let symbol = this.checker.getSymbolAtLocation(node);
47 if (!symbol && isNamedNode(node)) {
48 symbol = this.checker.getSymbolAtLocation(node.name);
49 }
50 return symbol;
51 }
52 expectSymbolAtLocation(node) {
53 const symbol = this.getSymbolAtLocation(node);
54 if (!symbol) {
55 throw new Error(`Expected a symbol for node with kind ${ts.SyntaxKind[node.kind]}`);
56 }
57 return symbol;
58 }
59 resolveAliasedSymbol(symbol) {
60 return (symbol && ts.SymbolFlags.Alias & symbol.flags) ? this.checker.getAliasedSymbol(symbol) : symbol;
61 }
62 getLogger() {
63 return this.converter.application.logger;
64 }
65 registerReflection(reflection, symbol) {
66 if (symbol) {
67 this.project.registerReflection(reflection, this.checker.getFullyQualifiedName(symbol));
68 }
69 else {
70 this.project.registerReflection(reflection);
71 }
72 }
73 trigger(name, reflection, node) {
74 this.converter.trigger(name, this, reflection, node);
75 }
76 withSourceFile(node, callback) {
77 const isExternal = this.isExternalFile(node.fileName);
78 if (this.isOutsideDocumentation(node.fileName, isExternal)) {
79 return;
80 }
81 const isDeclaration = node.isDeclarationFile;
82 if (isDeclaration) {
83 const lib = this.converter.getDefaultLib();
84 const isLib = node.fileName.substr(-lib.length) === lib;
85 if (!this.converter.includeDeclarations || isLib) {
86 return;
87 }
88 }
89 this.isExternal = isExternal;
90 this.isDeclaration = isDeclaration;
91 this.trigger(converter_1.Converter.EVENT_FILE_BEGIN, this.project, node);
92 callback();
93 this.isExternal = false;
94 this.isDeclaration = false;
95 }
96 withScope(scope, ...args) {
97 if (!scope || !args.length) {
98 return;
99 }
100 const callback = args.pop();
101 const parameters = args.shift();
102 const oldScope = this.scope;
103 const oldTypeArguments = this.typeArguments;
104 const oldTypeParameters = this.typeParameters;
105 this.scope = scope;
106 this.typeParameters = parameters ? this.extractTypeParameters(parameters, args.length > 0) : this.typeParameters;
107 this.typeArguments = undefined;
108 callback();
109 this.scope = oldScope;
110 this.typeParameters = oldTypeParameters;
111 this.typeArguments = oldTypeArguments;
112 }
113 inherit(baseNode, typeArguments) {
114 const wasInherit = this.isInherit;
115 const oldInherited = this.inherited;
116 const oldInheritParent = this.inheritParent;
117 const oldTypeArguments = this.typeArguments;
118 this.isInherit = true;
119 this.inheritParent = baseNode;
120 this.inherited = [];
121 const target = this.scope;
122 if (!(target instanceof index_1.ContainerReflection)) {
123 throw new Error('Expected container reflection');
124 }
125 if (baseNode.symbol) {
126 const id = this.checker.getFullyQualifiedName(baseNode.symbol);
127 if (this.inheritedChildren && this.inheritedChildren.includes(id)) {
128 return target;
129 }
130 else {
131 this.inheritedChildren = this.inheritedChildren || [];
132 this.inheritedChildren.push(id);
133 }
134 }
135 if (target.children) {
136 this.inherited = target.children.map((c) => c.name);
137 }
138 else {
139 this.inherited = [];
140 }
141 if (typeArguments) {
142 this.typeArguments = this.converter.convertTypes(this, typeArguments);
143 }
144 else {
145 this.typeArguments = undefined;
146 }
147 this.converter.convertNode(this, baseNode);
148 this.isInherit = wasInherit;
149 this.inherited = oldInherited;
150 this.inheritParent = oldInheritParent;
151 this.typeArguments = oldTypeArguments;
152 if (!this.isInherit) {
153 delete this.inheritedChildren;
154 }
155 return target;
156 }
157 isOutsideDocumentation(fileName, isExternal = this.isExternalFile(fileName)) {
158 return isExternal && this.converter.excludeExternals;
159 }
160 isExternalFile(fileName) {
161 let isExternal = !this.fileNames.includes(fileName);
162 if (!isExternal && this.externalPattern) {
163 isExternal = this.externalPattern.some(mm => mm.match(fileName));
164 }
165 return isExternal;
166 }
167 extractTypeParameters(parameters, preserve) {
168 const typeParameters = {};
169 if (preserve) {
170 Object.keys(this.typeParameters || {}).forEach(key => {
171 typeParameters[key] = this.typeParameters[key];
172 });
173 }
174 parameters.forEach((declaration, index) => {
175 if (!declaration.symbol) {
176 return;
177 }
178 const name = declaration.symbol.name;
179 if (this.typeArguments && this.typeArguments[index]) {
180 typeParameters[name] = this.typeArguments[index];
181 }
182 else {
183 const param = type_parameter_1.createTypeParameter(this, declaration);
184 if (param) {
185 typeParameters[name] = param;
186 }
187 }
188 });
189 return typeParameters;
190 }
191}
192exports.Context = Context;
193function isNamedNode(node) {
194 return node['name'] && (ts.isIdentifierOrPrivateIdentifier(node['name']) ||
195 ts.isComputedPropertyName(node['name']));
196}
197//# sourceMappingURL=context.js.map
\No newline at end of file