@zzzen/pyright-internal 1.2.0-dev.20230924 → 1.2.0-dev.20231008

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/dist/analyzer/checker.d.ts +3 -1
  2. package/dist/analyzer/checker.js +59 -18
  3. package/dist/analyzer/checker.js.map +1 -1
  4. package/dist/analyzer/codeFlowEngine.js +17 -3
  5. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  6. package/dist/analyzer/decorators.d.ts +6 -1
  7. package/dist/analyzer/decorators.js +34 -24
  8. package/dist/analyzer/decorators.js.map +1 -1
  9. package/dist/analyzer/operations.js +4 -2
  10. package/dist/analyzer/operations.js.map +1 -1
  11. package/dist/analyzer/parseTreeUtils.d.ts +1 -0
  12. package/dist/analyzer/parseTreeUtils.js +21 -2
  13. package/dist/analyzer/parseTreeUtils.js.map +1 -1
  14. package/dist/analyzer/patternMatching.js +47 -16
  15. package/dist/analyzer/patternMatching.js.map +1 -1
  16. package/dist/analyzer/program.d.ts +2 -3
  17. package/dist/analyzer/program.js +6 -6
  18. package/dist/analyzer/program.js.map +1 -1
  19. package/dist/analyzer/properties.js +4 -0
  20. package/dist/analyzer/properties.js.map +1 -1
  21. package/dist/analyzer/protocols.js +7 -1
  22. package/dist/analyzer/protocols.js.map +1 -1
  23. package/dist/analyzer/scopeUtils.js +3 -2
  24. package/dist/analyzer/scopeUtils.js.map +1 -1
  25. package/dist/analyzer/service.d.ts +4 -4
  26. package/dist/analyzer/service.js +4 -18
  27. package/dist/analyzer/service.js.map +1 -1
  28. package/dist/analyzer/sourceFile.d.ts +3 -1
  29. package/dist/analyzer/sourceFile.js +9 -5
  30. package/dist/analyzer/sourceFile.js.map +1 -1
  31. package/dist/analyzer/typeEvaluator.d.ts +2 -1
  32. package/dist/analyzer/typeEvaluator.js +211 -233
  33. package/dist/analyzer/typeEvaluator.js.map +1 -1
  34. package/dist/analyzer/typeEvaluatorTypes.d.ts +7 -0
  35. package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
  36. package/dist/analyzer/typeGuards.d.ts +1 -1
  37. package/dist/analyzer/typeGuards.js +55 -43
  38. package/dist/analyzer/typeGuards.js.map +1 -1
  39. package/dist/analyzer/typeUtils.d.ts +1 -0
  40. package/dist/analyzer/typeUtils.js +22 -3
  41. package/dist/analyzer/typeUtils.js.map +1 -1
  42. package/dist/analyzer/types.d.ts +2 -1
  43. package/dist/analyzer/types.js +9 -3
  44. package/dist/analyzer/types.js.map +1 -1
  45. package/dist/backgroundAnalysisBase.js +0 -22
  46. package/dist/backgroundAnalysisBase.js.map +1 -1
  47. package/dist/commands/dumpFileDebugInfoCommand.js +1 -1
  48. package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
  49. package/dist/common/configOptions.d.ts +1 -0
  50. package/dist/common/configOptions.js +4 -0
  51. package/dist/common/configOptions.js.map +1 -1
  52. package/dist/common/diagnosticRules.d.ts +1 -0
  53. package/dist/common/diagnosticRules.js +1 -0
  54. package/dist/common/diagnosticRules.js.map +1 -1
  55. package/dist/common/extensibility.d.ts +26 -28
  56. package/dist/common/extensibility.js +6 -97
  57. package/dist/common/extensibility.js.map +1 -1
  58. package/dist/common/pathUtils.d.ts +3 -0
  59. package/dist/common/pathUtils.js +13 -8
  60. package/dist/common/pathUtils.js.map +1 -1
  61. package/dist/common/pythonVersion.d.ts +1 -1
  62. package/dist/common/pythonVersion.js +1 -1
  63. package/dist/common/realFileSystem.js +9 -16
  64. package/dist/common/realFileSystem.js.map +1 -1
  65. package/dist/common/serviceProvider.d.ts +22 -3
  66. package/dist/common/serviceProvider.js +89 -9
  67. package/dist/common/serviceProvider.js.map +1 -1
  68. package/dist/common/serviceProviderExtensions.d.ts +5 -1
  69. package/dist/common/serviceProviderExtensions.js +5 -2
  70. package/dist/common/serviceProviderExtensions.js.map +1 -1
  71. package/dist/languageServerBase.d.ts +2 -0
  72. package/dist/languageServerBase.js +28 -4
  73. package/dist/languageServerBase.js.map +1 -1
  74. package/dist/languageService/callHierarchyProvider.d.ts +0 -1
  75. package/dist/languageService/callHierarchyProvider.js +33 -16
  76. package/dist/languageService/callHierarchyProvider.js.map +1 -1
  77. package/dist/languageService/definitionProvider.d.ts +3 -2
  78. package/dist/languageService/definitionProvider.js +13 -11
  79. package/dist/languageService/definitionProvider.js.map +1 -1
  80. package/dist/languageService/documentHighlightProvider.js +5 -2
  81. package/dist/languageService/documentHighlightProvider.js.map +1 -1
  82. package/dist/languageService/documentSymbolCollector.d.ts +32 -23
  83. package/dist/languageService/documentSymbolCollector.js +171 -266
  84. package/dist/languageService/documentSymbolCollector.js.map +1 -1
  85. package/dist/languageService/hoverProvider.js +5 -9
  86. package/dist/languageService/hoverProvider.js.map +1 -1
  87. package/dist/languageService/referencesProvider.d.ts +6 -6
  88. package/dist/languageService/referencesProvider.js +23 -8
  89. package/dist/languageService/referencesProvider.js.map +1 -1
  90. package/dist/languageService/renameProvider.js +3 -3
  91. package/dist/languageService/renameProvider.js.map +1 -1
  92. package/dist/localization/localize.d.ts +29 -1
  93. package/dist/localization/localize.js +11 -1
  94. package/dist/localization/localize.js.map +1 -1
  95. package/dist/localization/package.nls.cs.json +8 -1
  96. package/dist/localization/package.nls.de.json +8 -1
  97. package/dist/localization/package.nls.en-us.json +11 -1
  98. package/dist/localization/package.nls.es.json +8 -1
  99. package/dist/localization/package.nls.fr.json +8 -1
  100. package/dist/localization/package.nls.it.json +8 -1
  101. package/dist/localization/package.nls.ja.json +8 -1
  102. package/dist/localization/package.nls.ko.json +8 -1
  103. package/dist/localization/package.nls.pl.json +8 -1
  104. package/dist/localization/package.nls.pt-br.json +8 -1
  105. package/dist/localization/package.nls.qps-ploc.json +8 -1
  106. package/dist/localization/package.nls.ru.json +8 -1
  107. package/dist/localization/package.nls.tr.json +8 -1
  108. package/dist/localization/package.nls.zh-cn.json +8 -1
  109. package/dist/localization/package.nls.zh-tw.json +8 -1
  110. package/dist/parser/stringTokenUtils.js +9 -5
  111. package/dist/parser/stringTokenUtils.js.map +1 -1
  112. package/dist/parser/tokenizer.d.ts +1 -0
  113. package/dist/parser/tokenizer.js +5 -5
  114. package/dist/parser/tokenizer.js.map +1 -1
  115. package/dist/tests/checker.test.js +8 -0
  116. package/dist/tests/checker.test.js.map +1 -1
  117. package/dist/tests/documentSymbolCollector.test.js +8 -363
  118. package/dist/tests/documentSymbolCollector.test.js.map +1 -1
  119. package/dist/tests/importResolver.test.js +654 -481
  120. package/dist/tests/importResolver.test.js.map +1 -1
  121. package/dist/tests/pathUtils.test.js +39 -1
  122. package/dist/tests/pathUtils.test.js.map +1 -1
  123. package/dist/tests/sourceFile.test.js +2 -1
  124. package/dist/tests/sourceFile.test.js.map +1 -1
  125. package/dist/tests/testStateUtils.d.ts +3 -0
  126. package/dist/tests/testStateUtils.js +25 -1
  127. package/dist/tests/testStateUtils.js.map +1 -1
  128. package/dist/tests/typeEvaluator1.test.js +1 -1
  129. package/dist/tests/typeEvaluator2.test.js +12 -6
  130. package/dist/tests/typeEvaluator2.test.js.map +1 -1
  131. package/dist/tests/typeEvaluator3.test.js +14 -4
  132. package/dist/tests/typeEvaluator3.test.js.map +1 -1
  133. package/dist/tests/typeEvaluator4.test.js +9 -8
  134. package/dist/tests/typeEvaluator4.test.js.map +1 -1
  135. package/dist/tests/typeEvaluator5.test.js +4 -2
  136. package/dist/tests/typeEvaluator5.test.js.map +1 -1
  137. package/dist/workspaceFactory.d.ts +2 -1
  138. package/dist/workspaceFactory.js +3 -1
  139. package/dist/workspaceFactory.js.map +1 -1
  140. package/package.json +3 -2
  141. package/dist/tests/fourslash/showcallhierarchy.incomingCalls.overriddenFunction.fourslash.d.ts +0 -1
  142. package/dist/tests/fourslash/showcallhierarchy.incomingCalls.overriddenFunction.fourslash.js +0 -40
  143. package/dist/tests/fourslash/showcallhierarchy.incomingCalls.overriddenFunction.fourslash.js.map +0 -1
@@ -1,32 +1,44 @@
1
1
  import { CancellationToken } from 'vscode-languageserver';
2
2
  import { Declaration } from '../analyzer/declaration';
3
3
  import { ParseTreeWalker } from '../analyzer/parseTreeWalker';
4
- import { ProgramView } from '../common/extensibility';
4
+ import { TypeEvaluator } from '../analyzer/typeEvaluatorTypes';
5
+ import { ProgramView, ReferenceUseCase, SymbolUsageProvider } from '../common/extensibility';
5
6
  import { TextRange } from '../common/textRange';
6
7
  import { NameNode, ParseNode, StringListNode, StringNode } from '../parser/parseNodes';
7
8
  export declare type CollectionResult = {
8
9
  node: NameNode | StringNode;
9
10
  range: TextRange;
10
11
  };
11
- export declare enum DocumentSymbolCollectorUseCase {
12
- Rename = 0,
13
- Reference = 1
12
+ export interface DocumentSymbolCollectorOptions {
13
+ readonly treatModuleInImportAndFromImportSame?: boolean;
14
+ readonly skipUnreachableCode?: boolean;
15
+ readonly useCase?: ReferenceUseCase;
16
+ /**
17
+ * If `providers` are set, `collector` will assume
18
+ * `appendSymbolNamesTo` and `appendDeclarationsTo` have already
19
+ * been handled and will not call them again.
20
+ *
21
+ * If `collector` will result in the same `providers`, `symbolNames`, and `decls` for
22
+ * all files, set `providers` so that `collector` doesn't need to perform the same work
23
+ * repeatedly for all files.
24
+ */
25
+ readonly providers?: readonly SymbolUsageProvider[];
14
26
  }
15
27
  export declare class DocumentSymbolCollector extends ParseTreeWalker {
16
- private _program;
17
- private _declarations;
18
- private _cancellationToken;
19
- private _startingNode;
20
- private _treatModuleInImportAndFromImportSame;
21
- private _skipUnreachableCode;
22
- private _useCase;
23
- private _results;
24
- private _dunderAllNameNodes;
25
- private _initFunction;
26
- private _symbolNames;
27
- constructor(_program: ProgramView, symbolNames: string[], _declarations: Declaration[], _cancellationToken: CancellationToken, _startingNode: ParseNode, _treatModuleInImportAndFromImportSame?: boolean, _skipUnreachableCode?: boolean, _useCase?: DocumentSymbolCollectorUseCase);
28
- static collectFromNode(program: ProgramView, node: NameNode, cancellationToken: CancellationToken, startingNode?: ParseNode, treatModuleInImportAndFromImportSame?: boolean, skipUnreachableCode?: boolean, useCase?: DocumentSymbolCollectorUseCase): CollectionResult[];
29
- static getDeclarationsForNode(program: ProgramView, node: NameNode, resolveLocalName: boolean, useCase: DocumentSymbolCollectorUseCase, token: CancellationToken): Declaration[];
28
+ private readonly _program;
29
+ private readonly _startingNode;
30
+ private readonly _cancellationToken;
31
+ private readonly _results;
32
+ private readonly _dunderAllNameNodes;
33
+ private readonly _symbolNames;
34
+ private readonly _declarations;
35
+ private readonly _usageProviders;
36
+ private readonly _treatModuleInImportAndFromImportSame;
37
+ private readonly _skipUnreachableCode;
38
+ private readonly _useCase;
39
+ constructor(_program: ProgramView, symbolNames: string[], declarations: Declaration[], _startingNode: ParseNode, _cancellationToken: CancellationToken, options?: DocumentSymbolCollectorOptions);
40
+ static collectFromNode(program: ProgramView, node: NameNode, cancellationToken: CancellationToken, startingNode?: ParseNode, options?: DocumentSymbolCollectorOptions): CollectionResult[];
41
+ static getDeclarationsForNode(program: ProgramView, node: NameNode, resolveLocalName: boolean, token: CancellationToken): Declaration[];
30
42
  collect(): CollectionResult[];
31
43
  walk(node: ParseNode): void;
32
44
  visitName(node: NameNode): boolean;
@@ -38,9 +50,6 @@ export declare class DocumentSymbolCollector extends ParseTreeWalker {
38
50
  private _resultsContainsDeclaration;
39
51
  private _getResolveAliasDeclaration;
40
52
  private _setDunderAllNodes;
41
- private static _addIfUnique;
42
- private static _getDeclarationsForInitNode;
43
- private static _getDeclarationsForNode;
44
- private static _getDeclarationsForNonModuleNameNode;
45
- private static _getDeclarationsForModuleNameNode;
46
53
  }
54
+ export declare function getDeclarationsForNameNode(evaluator: TypeEvaluator, node: NameNode, skipUnreachableCode?: boolean): Declaration[];
55
+ export declare function addDeclarationIfUnique(declarations: Declaration[], itemToAdd: Declaration): void;
@@ -28,90 +28,84 @@ var __importStar = (this && this.__importStar) || function (mod) {
28
28
  return result;
29
29
  };
30
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
- exports.DocumentSymbolCollector = exports.DocumentSymbolCollectorUseCase = void 0;
31
+ exports.addDeclarationIfUnique = exports.getDeclarationsForNameNode = exports.DocumentSymbolCollector = void 0;
32
32
  const AnalyzerNodeInfo = __importStar(require("../analyzer/analyzerNodeInfo"));
33
33
  const declaration_1 = require("../analyzer/declaration");
34
34
  const declarationUtils_1 = require("../analyzer/declarationUtils");
35
- const ParseTreeUtils = __importStar(require("../analyzer/parseTreeUtils"));
36
35
  const parseTreeUtils_1 = require("../analyzer/parseTreeUtils");
37
36
  const parseTreeWalker_1 = require("../analyzer/parseTreeWalker");
38
37
  const ScopeUtils = __importStar(require("../analyzer/scopeUtils"));
39
38
  const sourceFile_1 = require("../analyzer/sourceFile");
40
39
  const sourceFileInfoUtils_1 = require("../analyzer/sourceFileInfoUtils");
41
40
  const sourceMapper_1 = require("../analyzer/sourceMapper");
42
- const typeUtils_1 = require("../analyzer/typeUtils");
43
- const types_1 = require("../analyzer/types");
44
41
  const cancellationUtils_1 = require("../common/cancellationUtils");
45
42
  const collectionUtils_1 = require("../common/collectionUtils");
46
43
  const debug_1 = require("../common/debug");
47
44
  const extensibility_1 = require("../common/extensibility");
48
- var DocumentSymbolCollectorUseCase;
49
- (function (DocumentSymbolCollectorUseCase) {
50
- DocumentSymbolCollectorUseCase[DocumentSymbolCollectorUseCase["Rename"] = 0] = "Rename";
51
- DocumentSymbolCollectorUseCase[DocumentSymbolCollectorUseCase["Reference"] = 1] = "Reference";
52
- })(DocumentSymbolCollectorUseCase = exports.DocumentSymbolCollectorUseCase || (exports.DocumentSymbolCollectorUseCase = {}));
45
+ const serviceProviderExtensions_1 = require("../common/serviceProviderExtensions");
46
+ const core_1 = require("../common/core");
53
47
  // This walker looks for symbols that are semantically equivalent
54
48
  // to the requested symbol.
55
49
  class DocumentSymbolCollector extends parseTreeWalker_1.ParseTreeWalker {
56
- constructor(_program, symbolNames, _declarations, _cancellationToken, _startingNode, _treatModuleInImportAndFromImportSame = false, _skipUnreachableCode = true, _useCase = DocumentSymbolCollectorUseCase.Reference) {
50
+ constructor(_program, symbolNames, declarations, _startingNode, _cancellationToken, options) {
51
+ var _a, _b, _c, _d, _e;
57
52
  super();
58
53
  this._program = _program;
59
- this._declarations = _declarations;
60
- this._cancellationToken = _cancellationToken;
61
54
  this._startingNode = _startingNode;
62
- this._treatModuleInImportAndFromImportSame = _treatModuleInImportAndFromImportSame;
63
- this._skipUnreachableCode = _skipUnreachableCode;
64
- this._useCase = _useCase;
55
+ this._cancellationToken = _cancellationToken;
65
56
  this._results = [];
66
57
  this._dunderAllNameNodes = new Set();
67
58
  this._symbolNames = new Set();
59
+ this._declarations = [];
68
60
  // Start with the symbols passed in
69
61
  symbolNames.forEach((s) => this._symbolNames.add(s));
62
+ this._declarations.push(...declarations);
63
+ this._treatModuleInImportAndFromImportSame = (_a = options === null || options === void 0 ? void 0 : options.treatModuleInImportAndFromImportSame) !== null && _a !== void 0 ? _a : false;
64
+ this._skipUnreachableCode = (_b = options === null || options === void 0 ? void 0 : options.skipUnreachableCode) !== null && _b !== void 0 ? _b : true;
65
+ this._useCase = (_c = options === null || options === void 0 ? void 0 : options.useCase) !== null && _c !== void 0 ? _c : extensibility_1.ReferenceUseCase.References;
66
+ this._usageProviders =
67
+ (_d = options === null || options === void 0 ? void 0 : options.providers) !== null && _d !== void 0 ? _d : ((_e = this._program.serviceProvider.tryGet(serviceProviderExtensions_1.ServiceKeys.symbolUsageProviderFactory)) !== null && _e !== void 0 ? _e : [])
68
+ .map((f) => f.tryCreateProvider(this._useCase, declarations, this._cancellationToken))
69
+ .filter(core_1.isDefined);
70
+ if ((options === null || options === void 0 ? void 0 : options.providers) === undefined) {
71
+ // Check whether we need to add new symbol names and declarations.
72
+ this._usageProviders.forEach((p) => {
73
+ p.appendSymbolNamesTo(this._symbolNames);
74
+ p.appendDeclarationsTo(this._declarations);
75
+ });
76
+ }
70
77
  // Don't report strings in __all__ right away, that will
71
78
  // break the assumption on the result ordering.
72
79
  this._setDunderAllNodes(this._startingNode);
73
- // Check if one of our symbols is __init__ and we
74
- // have a class declaration in the list and we are
75
- // computing symbols for references and not rename.
76
- const initDeclaration = _declarations.find((d) => d.type === 5 /* Function */ && d.node.name.value === '__init__');
77
- if (initDeclaration && _useCase === DocumentSymbolCollectorUseCase.Reference) {
78
- const classDeclaration = _declarations.find((d) => d.type === 6 /* Class */);
79
- if (classDeclaration) {
80
- this._initFunction = initDeclaration.node;
81
- this._symbolNames.add(classDeclaration.node.name.value);
82
- }
83
- }
84
80
  }
85
- static collectFromNode(program, node, cancellationToken, startingNode, treatModuleInImportAndFromImportSame = false, skipUnreachableCode = true, useCase = DocumentSymbolCollectorUseCase.Reference) {
86
- const declarations = this.getDeclarationsForNode(program, node,
87
- /* resolveLocalName */ true, useCase, cancellationToken);
81
+ static collectFromNode(program, node, cancellationToken, startingNode, options) {
82
+ const declarations = this.getDeclarationsForNode(program, node, /* resolveLocalName */ true, cancellationToken);
88
83
  startingNode = startingNode !== null && startingNode !== void 0 ? startingNode : (0, parseTreeUtils_1.getModuleNode)(node);
89
84
  if (!startingNode) {
90
85
  return [];
91
86
  }
92
- const collector = new DocumentSymbolCollector(program, [node.value], declarations, cancellationToken, startingNode, treatModuleInImportAndFromImportSame, skipUnreachableCode, useCase);
87
+ const collector = new DocumentSymbolCollector(program, [node.value], declarations, startingNode, cancellationToken, options);
93
88
  return collector.collect();
94
89
  }
95
- static getDeclarationsForNode(program, node, resolveLocalName, useCase, token) {
90
+ static getDeclarationsForNode(program, node, resolveLocalName, token) {
96
91
  (0, cancellationUtils_1.throwIfCancellationRequested)(token);
97
92
  const evaluator = program.evaluator;
98
93
  if (!evaluator) {
99
94
  return [];
100
95
  }
101
- const declarations = this._getDeclarationsForNode(node, useCase, evaluator, token,
102
- /* skipUnreachableCode */ false);
96
+ const declarations = getDeclarationsForNameNode(evaluator, node, /* skipUnreachableCode */ false);
103
97
  const fileInfo = AnalyzerNodeInfo.getFileInfo(node);
104
98
  const resolvedDeclarations = [];
105
99
  const sourceMapper = program.getSourceMapper(fileInfo.filePath, token);
106
100
  declarations.forEach((decl) => {
107
101
  const resolvedDecl = evaluator.resolveAliasDeclaration(decl, resolveLocalName);
108
102
  if (resolvedDecl) {
109
- this._addIfUnique(resolvedDeclarations, resolvedDecl);
103
+ addDeclarationIfUnique(resolvedDeclarations, resolvedDecl);
110
104
  if (sourceMapper && (0, sourceMapper_1.isStubFile)(resolvedDecl.path)) {
111
105
  const implDecls = sourceMapper.findDeclarations(resolvedDecl);
112
106
  for (const implDecl of implDecls) {
113
107
  if (implDecl && implDecl.path) {
114
- this._addIfUnique(resolvedDeclarations, implDecl);
108
+ addDeclarationIfUnique(resolvedDeclarations, implDecl);
115
109
  }
116
110
  }
117
111
  }
@@ -143,7 +137,7 @@ class DocumentSymbolCollector extends parseTreeWalker_1.ParseTreeWalker {
143
137
  symbol === null || symbol === void 0 ? void 0 : symbol.getDeclarations().filter((d) => !(0, declaration_1.isAliasDeclaration)(d)).forEach((decl) => {
144
138
  const resolvedDecl = evaluator.resolveAliasDeclaration(decl, resolveLocalName);
145
139
  if (resolvedDecl) {
146
- DocumentSymbolCollector._addIfUnique(declarations, resolvedDecl);
140
+ addDeclarationIfUnique(declarations, resolvedDecl);
147
141
  }
148
142
  });
149
143
  }
@@ -164,10 +158,10 @@ class DocumentSymbolCollector extends parseTreeWalker_1.ParseTreeWalker {
164
158
  return false;
165
159
  }
166
160
  if (this._declarations.length > 0) {
167
- const declarations = DocumentSymbolCollector._getDeclarationsForNode(node, this._useCase, this._evaluator, this._cancellationToken, this._skipUnreachableCode);
161
+ const declarations = getDeclarationsForNameNode(this._evaluator, node, this._skipUnreachableCode);
168
162
  if (declarations && declarations.length > 0) {
169
163
  // Does this name share a declaration with the symbol of interest?
170
- if (declarations.some((decl) => this._resultsContainsDeclaration(decl, node))) {
164
+ if (this._resultsContainsDeclaration(node, declarations)) {
171
165
  this._addResult(node);
172
166
  }
173
167
  }
@@ -203,52 +197,32 @@ class DocumentSymbolCollector extends parseTreeWalker_1.ParseTreeWalker {
203
197
  const range = node.nodeType === 38 /* Name */ ? node : (0, parseTreeUtils_1.getStringNodeValueRange)(node);
204
198
  this._results.push({ node, range });
205
199
  }
206
- _isDeclarationAllowed(resolvedDecl, referenceNode) {
207
- var _a;
208
- // Declaration is allowed if:
209
- // Matches one of our declarations.
210
- // and --
211
- // That match is the right kind.
212
- const match = this._declarations.find((decl) => (0, declarationUtils_1.areDeclarationsSame)(decl, resolvedDecl, this._treatModuleInImportAndFromImportSame,
200
+ _isDeclarationAllowed(resolvedDecl) {
201
+ return this._declarations.some((decl) => (0, declarationUtils_1.areDeclarationsSame)(decl, resolvedDecl, this._treatModuleInImportAndFromImportSame,
213
202
  /* skipRangeForAliases */ true));
214
- if (match) {
215
- // Special case for __init__ being one of our symbol names and we
216
- // have a class name as the other.
217
- if (this._initFunction) {
218
- // If this is a method, must be an __init__ reference.
219
- if (match.type === 5 /* Function */) {
220
- return true;
221
- }
222
- else if (match.type === 6 /* Class */) {
223
- // If this is a class type match, only match on class calls.
224
- // Meaning something like so:
225
- // a = ClassA()
226
- return ((_a = referenceNode.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 9 /* Call */;
227
- }
203
+ }
204
+ _resultsContainsDeclaration(usage, declarations) {
205
+ const results = [...declarations];
206
+ this._usageProviders.forEach((p) => p.appendDeclarationsAt(usage, declarations, results));
207
+ return results.some((declaration) => {
208
+ // Resolve the declaration.
209
+ const resolvedDecl = this._evaluator.resolveAliasDeclaration(declaration, /* resolveLocalNames */ false);
210
+ if (!resolvedDecl) {
228
211
  return false;
229
212
  }
230
- return true;
231
- }
232
- return false;
233
- }
234
- _resultsContainsDeclaration(declaration, referenceNode) {
235
- // Resolve the declaration.
236
- const resolvedDecl = this._evaluator.resolveAliasDeclaration(declaration, /* resolveLocalNames */ false);
237
- if (!resolvedDecl) {
238
- return false;
239
- }
240
- // The reference results declarations are already resolved, so we don't
241
- // need to call resolveAliasDeclaration on them.
242
- if (this._isDeclarationAllowed(resolvedDecl, referenceNode)) {
243
- return true;
244
- }
245
- // We didn't find the declaration using local-only alias resolution. Attempt
246
- // it again by fully resolving the alias.
247
- const resolvedDeclNonlocal = this._getResolveAliasDeclaration(resolvedDecl);
248
- if (!resolvedDeclNonlocal || resolvedDeclNonlocal === resolvedDecl) {
249
- return false;
250
- }
251
- return this._isDeclarationAllowed(resolvedDeclNonlocal, referenceNode);
213
+ // The reference results declarations are already resolved, so we don't
214
+ // need to call resolveAliasDeclaration on them.
215
+ if (this._isDeclarationAllowed(resolvedDecl)) {
216
+ return true;
217
+ }
218
+ // We didn't find the declaration using local-only alias resolution. Attempt
219
+ // it again by fully resolving the alias.
220
+ const resolvedDeclNonlocal = this._getResolveAliasDeclaration(resolvedDecl);
221
+ if (!resolvedDeclNonlocal || resolvedDeclNonlocal === resolvedDecl) {
222
+ return false;
223
+ }
224
+ return this._isDeclarationAllowed(resolvedDeclNonlocal);
225
+ });
252
226
  }
253
227
  _getResolveAliasDeclaration(declaration) {
254
228
  // TypeEvaluator.resolveAliasDeclaration only resolve alias in AliasDeclaration in the form of
@@ -289,209 +263,140 @@ class DocumentSymbolCollector extends parseTreeWalker_1.ParseTreeWalker {
289
263
  if (!symbolInScope) {
290
264
  return;
291
265
  }
292
- if (!symbolInScope.symbol.getDeclarations().some((d) => this._resultsContainsDeclaration(d, stringNode))) {
266
+ if (!this._resultsContainsDeclaration(stringNode, symbolInScope.symbol.getDeclarations())) {
293
267
  return;
294
268
  }
295
269
  this._dunderAllNameNodes.add(stringNode);
296
270
  });
297
271
  }
298
- static _addIfUnique(declarations, itemToAdd) {
299
- for (const def of declarations) {
300
- if ((0, declarationUtils_1.areDeclarationsSame)(def, itemToAdd,
301
- /* treatModuleInImportAndFromImportSame */ false,
302
- /* skipRangeForAliases */ true)) {
303
- return;
304
- }
305
- }
306
- declarations.push(itemToAdd);
272
+ }
273
+ exports.DocumentSymbolCollector = DocumentSymbolCollector;
274
+ function getDeclarationsForNameNode(evaluator, node, skipUnreachableCode = true) {
275
+ var _a;
276
+ // This can handle symbols brought in by wildcard (import *) as long as the declarations that the symbol collector
277
+ // compares against point to the actual alias declaration, not one that uses local name (ex, import alias)
278
+ if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) !== 37 /* ModuleName */) {
279
+ return _getDeclarationsForNonModuleNameNode(evaluator, node, skipUnreachableCode);
307
280
  }
308
- static _getDeclarationsForInitNode(node, evaluator) {
309
- var _a;
310
- const parent = ParseTreeUtils.getEnclosingClassOrFunction(node.parent);
311
- // See what type of __init__ we're at.
312
- if ((parent === null || parent === void 0 ? void 0 : parent.nodeType) === 10 /* Class */) {
313
- // This is a def for '__init__'. We should include the class name too.
314
- return this._getDeclarationsForNonModuleNameNode(parent.name, evaluator);
315
- }
316
- else if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 35 /* MemberAccess */ &&
317
- ((node.parent.leftExpression.nodeType === 9 /* Call */ &&
318
- node.parent.leftExpression.leftExpression.nodeType === 38 /* Name */ &&
319
- node.parent.leftExpression.leftExpression.value === 'super') ||
320
- (node.parent.leftExpression.nodeType === 38 /* Name */ &&
321
- node.parent.leftExpression.value === 'super'))) {
322
- // We're on the 'super().__init__' call.
323
- const decls = evaluator.getDeclarationsForNameNode(node, /* skipUnreachableCode */ true);
324
- if (decls && decls.length > 0 && decls[0].node.parent) {
325
- // Parent node of the decl should be the class
326
- const classNode = ParseTreeUtils.getEnclosingClass(decls[0].node.parent);
327
- if (classNode) {
328
- return this._getDeclarationsForNonModuleNameNode(classNode.name, evaluator);
329
- }
330
- }
281
+ return _getDeclarationsForModuleNameNode(evaluator, node);
282
+ }
283
+ exports.getDeclarationsForNameNode = getDeclarationsForNameNode;
284
+ function addDeclarationIfUnique(declarations, itemToAdd) {
285
+ for (const def of declarations) {
286
+ if ((0, declarationUtils_1.areDeclarationsSame)(def, itemToAdd,
287
+ /* treatModuleInImportAndFromImportSame */ false,
288
+ /* skipRangeForAliases */ true)) {
289
+ return;
331
290
  }
332
- return [];
333
291
  }
334
- static _getDeclarationsForNode(node, useCase, evaluator, token, skipUnreachableCode = true) {
335
- var _a;
336
- let result = [];
337
- // This can handle symbols brought in by wildcard (import *) as long as the declarations that the symbol collector
338
- // compares against point to the actual alias declaration, not one that uses local name (ex, import alias)
339
- if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) !== 37 /* ModuleName */) {
340
- result = this._getDeclarationsForNonModuleNameNode(node, evaluator, skipUnreachableCode);
341
- // Special case for __init__. Might be __init__ on a class.
342
- if (node.value === '__init__' && useCase === DocumentSymbolCollectorUseCase.Reference && node.parent) {
343
- (0, collectionUtils_1.appendArray)(result, this._getDeclarationsForInitNode(node, evaluator));
344
- }
345
- }
346
- else {
347
- result = this._getDeclarationsForModuleNameNode(node, evaluator);
348
- }
349
- // Let extensions also add declarations.
350
- extensibility_1.Extensions.getProgramExtensions(node).forEach((e) => {
351
- var _a;
352
- const declUseCase = useCase === DocumentSymbolCollectorUseCase.Rename
353
- ? extensibility_1.DeclarationUseCase.Rename
354
- : extensibility_1.DeclarationUseCase.References;
355
- const extras = (_a = e.declarationProviderExtension) === null || _a === void 0 ? void 0 : _a.tryGetDeclarations(evaluator, node, node.start, declUseCase, token);
356
- if (extras && extras.length > 0) {
357
- (0, collectionUtils_1.appendArray)(result, extras);
358
- }
359
- });
360
- return result;
292
+ declarations.push(itemToAdd);
293
+ }
294
+ exports.addDeclarationIfUnique = addDeclarationIfUnique;
295
+ function _getDeclarationsForNonModuleNameNode(evaluator, node, skipUnreachableCode = true) {
296
+ var _a, _b;
297
+ (0, debug_1.assert)(((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) !== 37 /* ModuleName */);
298
+ let decls = evaluator.getDeclarationsForNameNode(node, skipUnreachableCode) || [];
299
+ if (((_b = node.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 23 /* ImportFromAs */) {
300
+ // Make sure we get the decl for this specific "from import" statement
301
+ decls = decls.filter((d) => d.node === node.parent);
361
302
  }
362
- static _getDeclarationsForNonModuleNameNode(node, evaluator, skipUnreachableCode = true) {
363
- var _a, _b;
364
- (0, debug_1.assert)(((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) !== 37 /* ModuleName */);
365
- let decls = evaluator.getDeclarationsForNameNode(node, skipUnreachableCode) || [];
366
- if (((_b = node.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 23 /* ImportFromAs */) {
367
- // Make sure we get the decl for this specific "from import" statement
368
- decls = decls.filter((d) => d.node === node.parent);
369
- }
370
- // If we can't get decl, see whether we can get type from the node.
371
- // Some might have synthesized type for the node such as subModule in import X.Y statement.
372
- if (decls.length === 0) {
373
- const type = evaluator.getType(node);
374
- if ((type === null || type === void 0 ? void 0 : type.category) === 8 /* Module */) {
375
- // Synthesize decl for the module.
376
- return [(0, declarationUtils_1.createSynthesizedAliasDeclaration)(type.filePath)];
377
- }
378
- }
379
- // We would like to make X in import X and import X.Y as Y to match, but path for
380
- // X in import X and one in import X.Y as Y might not match since path in X.Y will point
381
- // to X.Y rather than X if import statement has an alias.
382
- // so, for such case, we put synthesized one so we can treat X in both statement same.
383
- for (const aliasDecl of decls.filter((d) => (0, declaration_1.isAliasDeclaration)(d) && !d.loadSymbolsFromPath)) {
384
- const node = aliasDecl.node;
385
- if (node.nodeType === 23 /* ImportFromAs */) {
386
- // from ... import X case, decl in the submodule fallback has the path.
387
- continue;
388
- }
389
- (0, collectionUtils_1.appendArray)(decls, evaluator.getDeclarationsForNameNode(node.module.nameParts[0], skipUnreachableCode) || []);
303
+ // If we can't get decl, see whether we can get type from the node.
304
+ // Some might have synthesized type for the node such as subModule in import X.Y statement.
305
+ if (decls.length === 0) {
306
+ const type = evaluator.getType(node);
307
+ if ((type === null || type === void 0 ? void 0 : type.category) === 8 /* Module */) {
308
+ // Synthesize decl for the module.
309
+ return [(0, declarationUtils_1.createSynthesizedAliasDeclaration)(type.filePath)];
390
310
  }
391
- // For now, we only support function overriding.
392
- for (const decl of decls.filter((d) => (0, declaration_1.isFunctionDeclaration)(d) && d.isMethod && d.node.name.value.length > 0)) {
393
- const methodDecl = decl;
394
- const enclosingClass = ParseTreeUtils.getEnclosingClass(methodDecl.node);
395
- const classResults = enclosingClass ? evaluator.getTypeOfClass(enclosingClass) : undefined;
396
- if (!classResults) {
397
- continue;
398
- }
399
- // Skip init and new as being overloads. They're not really overloads.
400
- if (methodDecl.node.name.value === '__init__' || methodDecl.node.name.value === '__new__') {
401
- continue;
402
- }
403
- for (const mroClass of classResults.classType.details.mro) {
404
- if ((0, types_1.isInstantiableClass)(mroClass)) {
405
- const currentMember = (0, typeUtils_1.lookUpClassMember)(mroClass, methodDecl.node.name.value);
406
- const baseMember = (0, typeUtils_1.lookUpClassMember)(mroClass, methodDecl.node.name.value, 1 /* SkipOriginalClass */);
407
- if (currentMember && !baseMember) {
408
- // Found base decl of the overridden method. Hold onto the decls.
409
- currentMember.symbol
410
- .getDeclarations()
411
- .filter((d) => (0, declaration_1.isFunctionDeclaration)(d) && d.isMethod)
412
- .forEach((d) => this._addIfUnique(decls, d));
413
- }
414
- }
415
- }
311
+ }
312
+ // We would like to make X in import X and import X.Y as Y to match, but path for
313
+ // X in import X and one in import X.Y as Y might not match since path in X.Y will point
314
+ // to X.Y rather than X if import statement has an alias.
315
+ // so, for such case, we put synthesized one so we can treat X in both statement same.
316
+ for (const aliasDecl of decls.filter((d) => (0, declaration_1.isAliasDeclaration)(d) && !d.loadSymbolsFromPath)) {
317
+ const node = aliasDecl.node;
318
+ if (node.nodeType === 23 /* ImportFromAs */) {
319
+ // from ... import X case, decl in the submodule fallback has the path.
320
+ continue;
416
321
  }
417
- return decls;
322
+ (0, collectionUtils_1.appendArray)(decls, evaluator.getDeclarationsForNameNode(node.module.nameParts[0], skipUnreachableCode) || []);
418
323
  }
419
- static _getDeclarationsForModuleNameNode(node, evaluator) {
420
- var _a, _b, _c, _d, _e;
421
- (0, debug_1.assert)(((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 37 /* ModuleName */);
422
- // We don't have symbols corresponding to ModuleName in our system since those
423
- // are not referenceable. but in "find all reference", we want to match those
424
- // if it refers to the same module file. Code below handles different kind of
425
- // ModuleName cases.
426
- const moduleName = node.parent;
427
- if (((_b = moduleName.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 21 /* ImportAs */ ||
428
- ((_c = moduleName.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 22 /* ImportFrom */) {
429
- const index = moduleName.nameParts.findIndex((n) => n === node);
430
- // Special case, first module name part.
431
- if (index === 0) {
432
- // 1. import X or from X import ...
433
- const decls = [];
434
- // First, we need to put decls for module names type evaluator synthesized so that
435
- // we can match both "import X" and "from X import ..."
436
- (0, collectionUtils_1.appendArray)(decls, ((_d = evaluator
437
- .getDeclarationsForNameNode(moduleName.nameParts[0])) === null || _d === void 0 ? void 0 : _d.filter((d) => (0, declaration_1.isAliasDeclaration)(d))) || []);
438
- if (decls.length === 0 || moduleName.parent.nodeType !== 21 /* ImportAs */) {
439
- return decls;
324
+ return decls;
325
+ }
326
+ function _getDeclarationsForModuleNameNode(evaluator, node) {
327
+ var _a, _b, _c, _d, _e;
328
+ (0, debug_1.assert)(((_a = node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 37 /* ModuleName */);
329
+ // We don't have symbols corresponding to ModuleName in our system since those
330
+ // are not referenceable. but in "find all reference", we want to match those
331
+ // if it refers to the same module file. Code below handles different kind of
332
+ // ModuleName cases.
333
+ const moduleName = node.parent;
334
+ if (((_b = moduleName.parent) === null || _b === void 0 ? void 0 : _b.nodeType) === 21 /* ImportAs */ ||
335
+ ((_c = moduleName.parent) === null || _c === void 0 ? void 0 : _c.nodeType) === 22 /* ImportFrom */) {
336
+ const index = moduleName.nameParts.findIndex((n) => n === node);
337
+ // Special case, first module name part.
338
+ if (index === 0) {
339
+ // 1. import X or from X import ...
340
+ const decls = [];
341
+ // First, we need to put decls for module names type evaluator synthesized so that
342
+ // we can match both "import X" and "from X import ..."
343
+ (0, collectionUtils_1.appendArray)(decls, ((_d = evaluator.getDeclarationsForNameNode(moduleName.nameParts[0])) === null || _d === void 0 ? void 0 : _d.filter((d) => (0, declaration_1.isAliasDeclaration)(d))) ||
344
+ []);
345
+ if (decls.length === 0 || moduleName.parent.nodeType !== 21 /* ImportAs */) {
346
+ return decls;
347
+ }
348
+ // If module name belong to "import xxx" not "from xxx", then see whether
349
+ // we can get regular decls (decls created from binder, not synthesized from type eval)
350
+ // from symbol as well.
351
+ // ex, import X as x
352
+ const isImportAsWithAlias = moduleName.nameParts.length === 1 &&
353
+ moduleName.parent.nodeType === 21 /* ImportAs */ &&
354
+ !!moduleName.parent.alias;
355
+ // if "import" has alias, symbol is assigned to alias, not the module.
356
+ const importName = isImportAsWithAlias
357
+ ? moduleName.parent.alias.value
358
+ : moduleName.nameParts[0].value;
359
+ // And we also need to re-use "decls for X" binder has created
360
+ // so that it matches with decls type evaluator returns for "references for X".
361
+ // ex) import X or from .X import ... in init file and etc.
362
+ const symbolWithScope = (_e = ScopeUtils.getScopeForNode(node)) === null || _e === void 0 ? void 0 : _e.lookUpSymbolRecursive(importName);
363
+ if (symbolWithScope && moduleName.nameParts.length === 1) {
364
+ let declsFromSymbol = [];
365
+ (0, collectionUtils_1.appendArray)(declsFromSymbol, symbolWithScope.symbol.getDeclarations().filter((d) => (0, declaration_1.isAliasDeclaration)(d)));
366
+ // If symbols are re-used, then find one that belong to this import statement.
367
+ if (declsFromSymbol.length > 1) {
368
+ declsFromSymbol = declsFromSymbol.filter((d) => {
369
+ d = d;
370
+ if (d.firstNamePart !== undefined) {
371
+ // For multiple import statements with sub modules, decl can be re-used.
372
+ // ex) import X.Y and import X.Z or from .X import ... in init file.
373
+ // Decls for X will be reused for both import statements, and node will point
374
+ // to first import statement. For those case, use firstNamePart instead to check.
375
+ return d.firstNamePart === moduleName.nameParts[0].value;
376
+ }
377
+ return d.node === moduleName.parent;
378
+ });
440
379
  }
441
- // If module name belong to "import xxx" not "from xxx", then see whether
442
- // we can get regular decls (decls created from binder, not synthesized from type eval)
443
- // from symbol as well.
444
380
  // ex, import X as x
445
- const isImportAsWithAlias = moduleName.nameParts.length === 1 &&
446
- moduleName.parent.nodeType === 21 /* ImportAs */ &&
447
- !!moduleName.parent.alias;
448
- // if "import" has alias, symbol is assigned to alias, not the module.
449
- const importName = isImportAsWithAlias
450
- ? moduleName.parent.alias.value
451
- : moduleName.nameParts[0].value;
452
- // And we also need to re-use "decls for X" binder has created
453
- // so that it matches with decls type evaluator returns for "references for X".
454
- // ex) import X or from .X import ... in init file and etc.
455
- const symbolWithScope = (_e = ScopeUtils.getScopeForNode(node)) === null || _e === void 0 ? void 0 : _e.lookUpSymbolRecursive(importName);
456
- if (symbolWithScope && moduleName.nameParts.length === 1) {
457
- let declsFromSymbol = [];
458
- (0, collectionUtils_1.appendArray)(declsFromSymbol, symbolWithScope.symbol.getDeclarations().filter((d) => (0, declaration_1.isAliasDeclaration)(d)));
459
- // If symbols are re-used, then find one that belong to this import statement.
460
- if (declsFromSymbol.length > 1) {
461
- declsFromSymbol = declsFromSymbol.filter((d) => {
462
- d = d;
463
- if (d.firstNamePart !== undefined) {
464
- // For multiple import statements with sub modules, decl can be re-used.
465
- // ex) import X.Y and import X.Z or from .X import ... in init file.
466
- // Decls for X will be reused for both import statements, and node will point
467
- // to first import statement. For those case, use firstNamePart instead to check.
468
- return d.firstNamePart === moduleName.nameParts[0].value;
469
- }
470
- return d.node === moduleName.parent;
471
- });
472
- }
473
- // ex, import X as x
474
- // We have decls for the alias "x" not the module name "X". Convert decls for the "X"
475
- if (isImportAsWithAlias) {
476
- declsFromSymbol = (0, declarationUtils_1.getDeclarationsWithUsesLocalNameRemoved)(declsFromSymbol);
477
- }
478
- (0, collectionUtils_1.appendArray)(decls, declsFromSymbol);
381
+ // We have decls for the alias "x" not the module name "X". Convert decls for the "X"
382
+ if (isImportAsWithAlias) {
383
+ declsFromSymbol = (0, declarationUtils_1.getDeclarationsWithUsesLocalNameRemoved)(declsFromSymbol);
479
384
  }
480
- return decls;
481
- }
482
- if (index > 0) {
483
- // 2. import X.Y or from X.Y import ....
484
- // For submodule "Y", we just use synthesized decls from type evaluator.
485
- // Decls for these sub module don't actually exist in the system. Instead, symbol for Y in
486
- // "import X.Y" hold onto synthesized module type (without any decl).
487
- // And "from X.Y import ..." doesn't have any symbol associated module names.
488
- // they can't be referenced in the module.
489
- return evaluator.getDeclarationsForNameNode(moduleName.nameParts[index]) || [];
385
+ (0, collectionUtils_1.appendArray)(decls, declsFromSymbol);
490
386
  }
491
- return [];
387
+ return decls;
388
+ }
389
+ if (index > 0) {
390
+ // 2. import X.Y or from X.Y import ....
391
+ // For submodule "Y", we just use synthesized decls from type evaluator.
392
+ // Decls for these sub module don't actually exist in the system. Instead, symbol for Y in
393
+ // "import X.Y" hold onto synthesized module type (without any decl).
394
+ // And "from X.Y import ..." doesn't have any symbol associated module names.
395
+ // they can't be referenced in the module.
396
+ return evaluator.getDeclarationsForNameNode(moduleName.nameParts[index]) || [];
492
397
  }
493
398
  return [];
494
399
  }
400
+ return [];
495
401
  }
496
- exports.DocumentSymbolCollector = DocumentSymbolCollector;
497
402
  //# sourceMappingURL=documentSymbolCollector.js.map