UNPKG

6.43 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const paths_1 = require("../utils/paths");
4const index_1 = require("../models/index");
5const type_parameter_1 = require("./factories/type-parameter");
6const converter_1 = require("./converter");
7class Context {
8 constructor(converter, fileNames, checker, program) {
9 this.symbolID = -1024;
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 getLogger() {
46 return this.converter.application.logger;
47 }
48 getSymbolID(symbol) {
49 if (!symbol) {
50 return;
51 }
52 if (!symbol.id) {
53 symbol.id = this.symbolID--;
54 }
55 return symbol.id;
56 }
57 registerReflection(reflection, node, symbol) {
58 this.project.reflections[reflection.id] = reflection;
59 const id = this.getSymbolID(symbol ? symbol : (node ? node.symbol : undefined));
60 if (!this.isInherit && id && !this.project.symbolMapping[id]) {
61 this.project.symbolMapping[id] = reflection.id;
62 }
63 }
64 trigger(name, reflection, node) {
65 this.converter.trigger(name, this, reflection, node);
66 }
67 withSourceFile(node, callback) {
68 let isExternal = !this.fileNames.includes(node.fileName);
69 if (!isExternal && this.externalPattern) {
70 isExternal = this.externalPattern.some(mm => mm.match(node.fileName));
71 }
72 if (isExternal && this.converter.excludeExternals) {
73 return;
74 }
75 let isDeclaration = node.isDeclarationFile;
76 if (isDeclaration) {
77 const lib = this.converter.getDefaultLib();
78 const isLib = node.fileName.substr(-lib.length) === lib;
79 if (!this.converter.includeDeclarations || isLib) {
80 return;
81 }
82 }
83 this.isExternal = isExternal;
84 this.isDeclaration = isDeclaration;
85 this.trigger(converter_1.Converter.EVENT_FILE_BEGIN, this.project, node);
86 callback();
87 this.isExternal = false;
88 this.isDeclaration = false;
89 }
90 withScope(scope, ...args) {
91 if (!scope || !args.length) {
92 return;
93 }
94 const callback = args.pop();
95 const parameters = args.shift();
96 const oldScope = this.scope;
97 const oldTypeArguments = this.typeArguments;
98 const oldTypeParameters = this.typeParameters;
99 this.scope = scope;
100 this.typeParameters = parameters ? this.extractTypeParameters(parameters, args.length > 0) : this.typeParameters;
101 this.typeArguments = undefined;
102 callback();
103 this.scope = oldScope;
104 this.typeParameters = oldTypeParameters;
105 this.typeArguments = oldTypeArguments;
106 }
107 inherit(baseNode, typeArguments) {
108 const wasInherit = this.isInherit;
109 const oldInherited = this.inherited;
110 const oldInheritParent = this.inheritParent;
111 const oldTypeArguments = this.typeArguments;
112 this.isInherit = true;
113 this.inheritParent = baseNode;
114 this.inherited = [];
115 const target = this.scope;
116 if (!(target instanceof index_1.ContainerReflection)) {
117 throw new Error('Expected container reflection');
118 }
119 if (baseNode.symbol) {
120 const id = this.getSymbolID(baseNode.symbol);
121 if (this.inheritedChildren && this.inheritedChildren.includes(id)) {
122 return target;
123 }
124 else {
125 this.inheritedChildren = this.inheritedChildren || [];
126 this.inheritedChildren.push(id);
127 }
128 }
129 if (target.children) {
130 this.inherited = target.children.map((c) => c.name);
131 }
132 else {
133 this.inherited = [];
134 }
135 if (typeArguments) {
136 this.typeArguments = this.converter.convertTypes(this, typeArguments);
137 }
138 else {
139 this.typeArguments = undefined;
140 }
141 this.converter.convertNode(this, baseNode);
142 this.isInherit = wasInherit;
143 this.inherited = oldInherited;
144 this.inheritParent = oldInheritParent;
145 this.typeArguments = oldTypeArguments;
146 if (!this.isInherit) {
147 delete this.inheritedChildren;
148 }
149 return target;
150 }
151 extractTypeParameters(parameters, preserve) {
152 const typeParameters = {};
153 if (preserve) {
154 Object.keys(this.typeParameters || {}).forEach(key => {
155 typeParameters[key] = this.typeParameters[key];
156 });
157 }
158 parameters.forEach((declaration, index) => {
159 if (!declaration.symbol) {
160 return;
161 }
162 const name = declaration.symbol.name;
163 if (this.typeArguments && this.typeArguments[index]) {
164 typeParameters[name] = this.typeArguments[index];
165 }
166 else {
167 const param = type_parameter_1.createTypeParameter(this, declaration);
168 if (param) {
169 typeParameters[name] = param;
170 }
171 }
172 });
173 return typeParameters;
174 }
175}
176exports.Context = Context;
177//# sourceMappingURL=context.js.map
\No newline at end of file