@zzzen/pyright-internal 1.2.0-dev.20230430 → 1.2.0-dev.20230514
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.
- package/dist/analyzer/backgroundAnalysisProgram.d.ts +4 -1
- package/dist/analyzer/backgroundAnalysisProgram.js +12 -0
- package/dist/analyzer/backgroundAnalysisProgram.js.map +1 -1
- package/dist/analyzer/checker.d.ts +1 -0
- package/dist/analyzer/checker.js +89 -6
- package/dist/analyzer/checker.js.map +1 -1
- package/dist/analyzer/constraintSolver.js +14 -15
- package/dist/analyzer/constraintSolver.js.map +1 -1
- package/dist/analyzer/constructors.d.ts +6 -0
- package/dist/analyzer/constructors.js +513 -0
- package/dist/analyzer/constructors.js.map +1 -0
- package/dist/analyzer/dataClasses.js +86 -2
- package/dist/analyzer/dataClasses.js.map +1 -1
- package/dist/analyzer/docStringConversion.js +1 -1
- package/dist/analyzer/docStringConversion.js.map +1 -1
- package/dist/analyzer/enums.js +62 -8
- package/dist/analyzer/enums.js.map +1 -1
- package/dist/analyzer/importResolver.js +47 -29
- package/dist/analyzer/importResolver.js.map +1 -1
- package/dist/analyzer/importStatementUtils.d.ts +2 -2
- package/dist/analyzer/importStatementUtils.js.map +1 -1
- package/dist/analyzer/namedTuples.js +3 -6
- package/dist/analyzer/namedTuples.js.map +1 -1
- package/dist/analyzer/operations.d.ts +16 -0
- package/dist/analyzer/operations.js +749 -0
- package/dist/analyzer/operations.js.map +1 -0
- package/dist/analyzer/parseTreeUtils.d.ts +4 -2
- package/dist/analyzer/parseTreeUtils.js +32 -1
- package/dist/analyzer/parseTreeUtils.js.map +1 -1
- package/dist/analyzer/patternMatching.js +16 -0
- package/dist/analyzer/patternMatching.js.map +1 -1
- package/dist/analyzer/program.d.ts +11 -33
- package/dist/analyzer/program.js +73 -735
- package/dist/analyzer/program.js.map +1 -1
- package/dist/analyzer/protocols.js +1 -1
- package/dist/analyzer/protocols.js.map +1 -1
- package/dist/analyzer/service.d.ts +5 -21
- package/dist/analyzer/service.js +26 -33
- package/dist/analyzer/service.js.map +1 -1
- package/dist/analyzer/sourceFile.d.ts +9 -41
- package/dist/analyzer/sourceFile.js +219 -238
- package/dist/analyzer/sourceFile.js.map +1 -1
- package/dist/analyzer/sourceFileInfoUtils.d.ts +3 -9
- package/dist/analyzer/sourceFileInfoUtils.js.map +1 -1
- package/dist/analyzer/symbol.d.ts +3 -1
- package/dist/analyzer/symbol.js +5 -0
- package/dist/analyzer/symbol.js.map +1 -1
- package/dist/analyzer/typeEvaluator.js +460 -1425
- package/dist/analyzer/typeEvaluator.js.map +1 -1
- package/dist/analyzer/typeEvaluatorTypes.d.ts +42 -7
- package/dist/analyzer/typeEvaluatorTypes.js +33 -1
- package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
- package/dist/analyzer/typeGuards.js +2 -8
- package/dist/analyzer/typeGuards.js.map +1 -1
- package/dist/analyzer/typePrinter.d.ts +3 -3
- package/dist/analyzer/typePrinter.js +247 -100
- package/dist/analyzer/typePrinter.js.map +1 -1
- package/dist/analyzer/typeUtils.d.ts +14 -7
- package/dist/analyzer/typeUtils.js +204 -49
- package/dist/analyzer/typeUtils.js.map +1 -1
- package/dist/analyzer/typeVarContext.d.ts +6 -7
- package/dist/analyzer/typeVarContext.js +21 -32
- package/dist/analyzer/typeVarContext.js.map +1 -1
- package/dist/analyzer/typedDicts.js +2 -2
- package/dist/analyzer/typedDicts.js.map +1 -1
- package/dist/analyzer/types.d.ts +7 -4
- package/dist/analyzer/types.js +20 -10
- package/dist/analyzer/types.js.map +1 -1
- package/dist/backgroundAnalysisBase.d.ts +1 -1
- package/dist/backgroundAnalysisBase.js +16 -0
- package/dist/backgroundAnalysisBase.js.map +1 -1
- package/dist/commands/dumpFileDebugInfoCommand.js +0 -1
- package/dist/commands/dumpFileDebugInfoCommand.js.map +1 -1
- package/dist/common/extensibility.d.ts +28 -4
- package/dist/common/extensibility.js.map +1 -1
- package/dist/common/logTracker.d.ts +2 -0
- package/dist/common/logTracker.js +8 -1
- package/dist/common/logTracker.js.map +1 -1
- package/dist/common/lspUtils.d.ts +4 -1
- package/dist/common/lspUtils.js +38 -1
- package/dist/common/lspUtils.js.map +1 -1
- package/dist/common/pathUtils.d.ts +11 -11
- package/dist/common/pathUtils.js.map +1 -1
- package/dist/common/pythonVersion.d.ts +2 -1
- package/dist/common/pythonVersion.js +1 -0
- package/dist/common/pythonVersion.js.map +1 -1
- package/dist/common/workspaceEditUtils.d.ts +8 -8
- package/dist/common/workspaceEditUtils.js +10 -10
- package/dist/common/workspaceEditUtils.js.map +1 -1
- package/dist/languageServerBase.d.ts +3 -7
- package/dist/languageServerBase.js +41 -73
- package/dist/languageServerBase.js.map +1 -1
- package/dist/languageService/autoImporter.d.ts +50 -51
- package/dist/languageService/autoImporter.js +125 -210
- package/dist/languageService/autoImporter.js.map +1 -1
- package/dist/languageService/callHierarchyProvider.d.ts +1 -1
- package/dist/languageService/callHierarchyProvider.js +11 -37
- package/dist/languageService/callHierarchyProvider.js.map +1 -1
- package/dist/languageService/completionProvider.d.ts +39 -81
- package/dist/languageService/completionProvider.js +572 -801
- package/dist/languageService/completionProvider.js.map +1 -1
- package/dist/languageService/documentHighlightProvider.js +1 -1
- package/dist/languageService/documentHighlightProvider.js.map +1 -1
- package/dist/languageService/documentSymbolCollector.d.ts +6 -7
- package/dist/languageService/documentSymbolCollector.js +47 -28
- package/dist/languageService/documentSymbolCollector.js.map +1 -1
- package/dist/languageService/documentSymbolProvider.d.ts +13 -35
- package/dist/languageService/documentSymbolProvider.js +52 -264
- package/dist/languageService/documentSymbolProvider.js.map +1 -1
- package/dist/languageService/hoverProvider.d.ts +5 -6
- package/dist/languageService/hoverProvider.js +40 -132
- package/dist/languageService/hoverProvider.js.map +1 -1
- package/dist/languageService/referencesProvider.d.ts +6 -11
- package/dist/languageService/referencesProvider.js +23 -17
- package/dist/languageService/referencesProvider.js.map +1 -1
- package/dist/languageService/renameProvider.d.ts +16 -0
- package/dist/languageService/renameProvider.js +139 -0
- package/dist/languageService/renameProvider.js.map +1 -0
- package/dist/languageService/symbolIndexer.d.ts +31 -0
- package/dist/languageService/symbolIndexer.js +105 -0
- package/dist/languageService/symbolIndexer.js.map +1 -0
- package/dist/languageService/tooltipUtils.d.ts +8 -1
- package/dist/languageService/tooltipUtils.js +102 -1
- package/dist/languageService/tooltipUtils.js.map +1 -1
- package/dist/languageService/workspaceSymbolProvider.d.ts +17 -0
- package/dist/languageService/workspaceSymbolProvider.js +133 -0
- package/dist/languageService/workspaceSymbolProvider.js.map +1 -0
- package/dist/localization/localize.d.ts +33 -15
- package/dist/localization/localize.js +13 -7
- package/dist/localization/localize.js.map +1 -1
- package/dist/localization/package.nls.en-us.json +14 -7
- package/dist/parser/parser.js +3 -0
- package/dist/parser/parser.js.map +1 -1
- package/dist/pyright.js +26 -4
- package/dist/pyright.js.map +1 -1
- package/dist/tests/chainedSourceFiles.test.js +15 -20
- package/dist/tests/chainedSourceFiles.test.js.map +1 -1
- package/dist/tests/checker.test.js +14 -0
- package/dist/tests/checker.test.js.map +1 -1
- package/dist/tests/completions.test.js +11 -236
- package/dist/tests/completions.test.js.map +1 -1
- package/dist/tests/docStringConversion.test.js +36 -2
- package/dist/tests/docStringConversion.test.js.map +1 -1
- package/dist/tests/documentSymbolCollector.test.js +3 -3
- package/dist/tests/documentSymbolCollector.test.js.map +1 -1
- package/dist/tests/fourslash/completions.override2.fourslash.js +1 -16
- package/dist/tests/fourslash/completions.override2.fourslash.js.map +1 -1
- package/dist/tests/fourslash/fourslash.d.ts +4 -4
- package/dist/tests/fourslash/missingTypeStub.codeAction.fourslash.js +1 -1
- package/dist/tests/fourslash/missingTypeStub.codeAction.fourslash.js.map +1 -1
- package/dist/tests/harness/fourslash/testState.d.ts +17 -11
- package/dist/tests/harness/fourslash/testState.js +39 -50
- package/dist/tests/harness/fourslash/testState.js.map +1 -1
- package/dist/tests/importResolver.test.js +81 -1
- package/dist/tests/importResolver.test.js.map +1 -1
- package/dist/tests/sourceFile.test.js +1 -1
- package/dist/tests/sourceFile.test.js.map +1 -1
- package/dist/tests/testStateUtils.d.ts +2 -2
- package/dist/tests/testStateUtils.js +38 -8
- package/dist/tests/testStateUtils.js.map +1 -1
- package/dist/tests/typeEvaluator2.test.js +13 -1
- package/dist/tests/typeEvaluator2.test.js.map +1 -1
- package/dist/tests/typeEvaluator3.test.js +5 -1
- package/dist/tests/typeEvaluator3.test.js.map +1 -1
- package/dist/tests/typeEvaluator4.test.js +9 -1
- package/dist/tests/typeEvaluator4.test.js.map +1 -1
- package/dist/tests/typeEvaluator5.test.js +25 -9
- package/dist/tests/typeEvaluator5.test.js.map +1 -1
- package/dist/tests/workspaceEditUtils.test.js +95 -6
- package/dist/tests/workspaceEditUtils.test.js.map +1 -1
- package/package.json +4 -4
- package/dist/languageService/importAdder.d.ts +0 -40
- package/dist/languageService/importAdder.js +0 -388
- package/dist/languageService/importAdder.js.map +0 -1
- package/dist/languageService/indentationUtils.d.ts +0 -16
- package/dist/languageService/indentationUtils.js +0 -727
- package/dist/languageService/indentationUtils.js.map +0 -1
- package/dist/languageService/insertionPointUtils.d.ts +0 -9
- package/dist/languageService/insertionPointUtils.js +0 -132
- package/dist/languageService/insertionPointUtils.js.map +0 -1
- package/dist/languageService/renameModuleProvider.d.ts +0 -65
- package/dist/languageService/renameModuleProvider.js +0 -939
- package/dist/languageService/renameModuleProvider.js.map +0 -1
- package/dist/tests/fourslash/completions.commitChars.fourslash.d.ts +0 -1
- package/dist/tests/fourslash/completions.commitChars.fourslash.js +0 -81
- package/dist/tests/fourslash/completions.commitChars.fourslash.js.map +0 -1
- package/dist/tests/importAdder.test.d.ts +0 -1
- package/dist/tests/importAdder.test.js +0 -1325
- package/dist/tests/importAdder.test.js.map +0 -1
- package/dist/tests/indentationUtils.ptvs.test.d.ts +0 -1
- package/dist/tests/indentationUtils.ptvs.test.js +0 -324
- package/dist/tests/indentationUtils.ptvs.test.js.map +0 -1
- package/dist/tests/indentationUtils.reindent.test.d.ts +0 -1
- package/dist/tests/indentationUtils.reindent.test.js +0 -372
- package/dist/tests/indentationUtils.reindent.test.js.map +0 -1
- package/dist/tests/indentationUtils.test.d.ts +0 -1
- package/dist/tests/indentationUtils.test.js +0 -502
- package/dist/tests/indentationUtils.test.js.map +0 -1
- package/dist/tests/insertionPointUtils.test.d.ts +0 -1
- package/dist/tests/insertionPointUtils.test.js +0 -154
- package/dist/tests/insertionPointUtils.test.js.map +0 -1
- package/dist/tests/moveSymbol.importAdder.test.d.ts +0 -1
- package/dist/tests/moveSymbol.importAdder.test.js +0 -298
- package/dist/tests/moveSymbol.importAdder.test.js.map +0 -1
- package/dist/tests/moveSymbol.insertion.test.d.ts +0 -1
- package/dist/tests/moveSymbol.insertion.test.js +0 -537
- package/dist/tests/moveSymbol.insertion.test.js.map +0 -1
- package/dist/tests/moveSymbol.misc.test.d.ts +0 -1
- package/dist/tests/moveSymbol.misc.test.js +0 -169
- package/dist/tests/moveSymbol.misc.test.js.map +0 -1
- package/dist/tests/moveSymbol.updateReference.test.d.ts +0 -1
- package/dist/tests/moveSymbol.updateReference.test.js +0 -1071
- package/dist/tests/moveSymbol.updateReference.test.js.map +0 -1
- package/dist/tests/renameModule.folder.test.d.ts +0 -1
- package/dist/tests/renameModule.folder.test.js +0 -229
- package/dist/tests/renameModule.folder.test.js.map +0 -1
- package/dist/tests/renameModule.fromImports.test.d.ts +0 -1
- package/dist/tests/renameModule.fromImports.test.js +0 -790
- package/dist/tests/renameModule.fromImports.test.js.map +0 -1
- package/dist/tests/renameModule.imports.test.d.ts +0 -1
- package/dist/tests/renameModule.imports.test.js +0 -380
- package/dist/tests/renameModule.imports.test.js.map +0 -1
- package/dist/tests/renameModule.misc.test.d.ts +0 -1
- package/dist/tests/renameModule.misc.test.js +0 -615
- package/dist/tests/renameModule.misc.test.js.map +0 -1
- package/dist/tests/renameModule.relativePath.test.d.ts +0 -1
- package/dist/tests/renameModule.relativePath.test.js +0 -231
- package/dist/tests/renameModule.relativePath.test.js.map +0 -1
- package/dist/tests/renameModuleTestUtils.d.ts +0 -4
- package/dist/tests/renameModuleTestUtils.js +0 -76
- package/dist/tests/renameModuleTestUtils.js.map +0 -1
@@ -1,939 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
/*
|
3
|
-
* renameModuleProvider.ts
|
4
|
-
* Copyright (c) Microsoft Corporation.
|
5
|
-
* Licensed under the MIT license.
|
6
|
-
*
|
7
|
-
* Logic that updates affected references of a module rename/move.
|
8
|
-
*/
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
10
|
-
exports.RenameModuleProvider = void 0;
|
11
|
-
const vscode_languageserver_1 = require("vscode-languageserver");
|
12
|
-
const analyzerNodeInfo_1 = require("../analyzer/analyzerNodeInfo");
|
13
|
-
const declaration_1 = require("../analyzer/declaration");
|
14
|
-
const declarationUtils_1 = require("../analyzer/declarationUtils");
|
15
|
-
const importResolver_1 = require("../analyzer/importResolver");
|
16
|
-
const importStatementUtils_1 = require("../analyzer/importStatementUtils");
|
17
|
-
const parseTreeUtils_1 = require("../analyzer/parseTreeUtils");
|
18
|
-
const parseTreeWalker_1 = require("../analyzer/parseTreeWalker");
|
19
|
-
const sourceMapper_1 = require("../analyzer/sourceMapper");
|
20
|
-
const symbolNameUtils_1 = require("../analyzer/symbolNameUtils");
|
21
|
-
const collectionUtils_1 = require("../common/collectionUtils");
|
22
|
-
const configOptions_1 = require("../common/configOptions");
|
23
|
-
const debug_1 = require("../common/debug");
|
24
|
-
const pathUtils_1 = require("../common/pathUtils");
|
25
|
-
const positionUtils_1 = require("../common/positionUtils");
|
26
|
-
const textEditTracker_1 = require("../common/textEditTracker");
|
27
|
-
const textRange_1 = require("../common/textRange");
|
28
|
-
const parseNodes_1 = require("../parser/parseNodes");
|
29
|
-
const documentSymbolCollector_1 = require("./documentSymbolCollector");
|
30
|
-
var UpdateType;
|
31
|
-
(function (UpdateType) {
|
32
|
-
UpdateType[UpdateType["File"] = 0] = "File";
|
33
|
-
UpdateType[UpdateType["Folder"] = 1] = "Folder";
|
34
|
-
UpdateType[UpdateType["Symbol"] = 2] = "Symbol";
|
35
|
-
})(UpdateType || (UpdateType = {}));
|
36
|
-
class RenameModuleProvider {
|
37
|
-
constructor(_fs, _evaluator, _moduleFilePath, newModuleFilePath, _moduleNameAndType, _newModuleNameAndType, _type, declarations, _token) {
|
38
|
-
this._fs = _fs;
|
39
|
-
this._evaluator = _evaluator;
|
40
|
-
this._moduleFilePath = _moduleFilePath;
|
41
|
-
this._moduleNameAndType = _moduleNameAndType;
|
42
|
-
this._newModuleNameAndType = _newModuleNameAndType;
|
43
|
-
this._type = _type;
|
44
|
-
this.declarations = declarations;
|
45
|
-
this._token = _token;
|
46
|
-
this._aliasIntroduced = new Set();
|
47
|
-
this._textEditTracker = new textEditTracker_1.TextEditTracker();
|
48
|
-
// moduleName and newModuleName are always in the absolute path form.
|
49
|
-
this._newModuleFilePath = (0, pathUtils_1.resolvePaths)(newModuleFilePath);
|
50
|
-
this._moduleNames = this._moduleName.split('.');
|
51
|
-
this._newModuleNames = this._newModuleName.split('.');
|
52
|
-
this._onlyNameChanged = (0, importStatementUtils_1.haveSameParentModule)(this._moduleNames, this._newModuleNames);
|
53
|
-
(0, debug_1.assert)(this._type !== UpdateType.Folder || this._onlyNameChanged, 'We only support simple rename for folder');
|
54
|
-
}
|
55
|
-
get lastModuleName() {
|
56
|
-
return this._moduleNames[this._moduleNames.length - 1];
|
57
|
-
}
|
58
|
-
get textEditTracker() {
|
59
|
-
return this._textEditTracker;
|
60
|
-
}
|
61
|
-
static createForModule(importResolver, configOptions, evaluator, path, newPath, token) {
|
62
|
-
if (!importResolver.fileSystem.existsSync(path)) {
|
63
|
-
return undefined;
|
64
|
-
}
|
65
|
-
if ((0, pathUtils_1.isFile)(importResolver.fileSystem, path)) {
|
66
|
-
return this._create(importResolver, configOptions, evaluator, path, newPath, UpdateType.File, token);
|
67
|
-
}
|
68
|
-
else if ((0, pathUtils_1.isDirectory)(importResolver.fileSystem, path)) {
|
69
|
-
// Make sure folder path is simple rename.
|
70
|
-
if ((0, pathUtils_1.getDirectoryChangeKind)(importResolver.fileSystem, path, newPath) !== 'Renamed') {
|
71
|
-
return undefined;
|
72
|
-
}
|
73
|
-
// We don't support namespace folder name. Currently, we don't have
|
74
|
-
// a way to find namespace folder references.
|
75
|
-
let fileNameForPackage = (0, pathUtils_1.combinePaths)(path, '__init__.pyi');
|
76
|
-
if (!importResolver.fileSystem.existsSync(fileNameForPackage)) {
|
77
|
-
fileNameForPackage = (0, pathUtils_1.combinePaths)(path, '__init__.py');
|
78
|
-
if (!importResolver.fileSystem.existsSync(fileNameForPackage)) {
|
79
|
-
return undefined;
|
80
|
-
}
|
81
|
-
}
|
82
|
-
return this._create(importResolver, configOptions, evaluator, fileNameForPackage, (0, pathUtils_1.combinePaths)(newPath, (0, pathUtils_1.getFileName)(fileNameForPackage)), UpdateType.Folder, token);
|
83
|
-
}
|
84
|
-
return undefined;
|
85
|
-
}
|
86
|
-
static createForSymbol(importResolver, configOptions, evaluator, path, newPath, declarations, token) {
|
87
|
-
if (!importResolver.fileSystem.existsSync(path)) {
|
88
|
-
return undefined;
|
89
|
-
}
|
90
|
-
const filteredDecls = declarations.filter((d) => (0, declaration_1.isClassDeclaration)(d) || (0, declaration_1.isFunctionDeclaration)(d) || (0, declaration_1.isVariableDeclaration)(d));
|
91
|
-
if (filteredDecls.length === 0) {
|
92
|
-
return undefined;
|
93
|
-
}
|
94
|
-
return this._create(importResolver, configOptions, evaluator, path, newPath, UpdateType.Symbol, filteredDecls, token);
|
95
|
-
}
|
96
|
-
static canMoveSymbol(configOptions, evaluator, node) {
|
97
|
-
var _a;
|
98
|
-
const filePath = (_a = (0, analyzerNodeInfo_1.getFileInfo)(node)) === null || _a === void 0 ? void 0 : _a.filePath;
|
99
|
-
if (!filePath || !(0, configOptions_1.matchFileSpecs)(configOptions, filePath, /* isFile */ true)) {
|
100
|
-
// We only support moving symbols from a user file.
|
101
|
-
return false;
|
102
|
-
}
|
103
|
-
if ((0, symbolNameUtils_1.isPrivateName)(node.value)) {
|
104
|
-
return false;
|
105
|
-
}
|
106
|
-
const lookUpResult = evaluator.lookUpSymbolRecursive(node, node.value, /* honorCodeFlow */ false);
|
107
|
-
if (lookUpResult === undefined || lookUpResult.scope.type !== 3 /* Module */) {
|
108
|
-
// We only allow moving a symbol at the module level.
|
109
|
-
return false;
|
110
|
-
}
|
111
|
-
// For now, we only supports module level variable, function and class.
|
112
|
-
const declarations = lookUpResult.symbol.getDeclarations();
|
113
|
-
if (declarations.length === 0) {
|
114
|
-
return false;
|
115
|
-
}
|
116
|
-
return declarations.every((d) => {
|
117
|
-
var _a, _b, _c;
|
118
|
-
if (!textRange_1.TextRange.containsRange(d.node, node)) {
|
119
|
-
return false;
|
120
|
-
}
|
121
|
-
if ((0, declaration_1.isFunctionDeclaration)(d) || (0, declaration_1.isClassDeclaration)(d)) {
|
122
|
-
return true;
|
123
|
-
}
|
124
|
-
if ((0, declaration_1.isVariableDeclaration)(d)) {
|
125
|
-
// We only support simple variable assignment.
|
126
|
-
// ex) a = 1
|
127
|
-
if (evaluator.isExplicitTypeAliasDeclaration(d)) {
|
128
|
-
return false;
|
129
|
-
}
|
130
|
-
if (d.inferredTypeSource && (0, parseNodes_1.isExpressionNode)(d.inferredTypeSource)) {
|
131
|
-
const type = evaluator.getType(d.inferredTypeSource);
|
132
|
-
if ((type === null || type === void 0 ? void 0 : type.category) === 10 /* TypeVar */) {
|
133
|
-
return false;
|
134
|
-
}
|
135
|
-
}
|
136
|
-
// This make sure we are not one of these
|
137
|
-
// ex) a = b = 1
|
138
|
-
// a, b = 1, 2
|
139
|
-
if (((_a = d.node.parent) === null || _a === void 0 ? void 0 : _a.nodeType) !== 3 /* Assignment */ ||
|
140
|
-
((_c = (_b = d.node.parent) === null || _b === void 0 ? void 0 : _b.parent) === null || _c === void 0 ? void 0 : _c.nodeType) !== 47 /* StatementList */) {
|
141
|
-
return false;
|
142
|
-
}
|
143
|
-
if (d.node.start !== d.node.parent.start) {
|
144
|
-
return false;
|
145
|
-
}
|
146
|
-
return true;
|
147
|
-
}
|
148
|
-
return false;
|
149
|
-
});
|
150
|
-
}
|
151
|
-
static getSymbolTextRange(parseResults, decl) {
|
152
|
-
var _a, _b;
|
153
|
-
if ((0, declaration_1.isVariableDeclaration)(decl)) {
|
154
|
-
const assignment = (_a = (0, parseTreeUtils_1.getFirstAncestorOrSelfOfKind)(decl.node, 3 /* Assignment */)) !== null && _a !== void 0 ? _a : decl.node;
|
155
|
-
const range = (0, parseTreeUtils_1.getFullStatementRange)(assignment, parseResults);
|
156
|
-
const textRange = (_b = (0, positionUtils_1.convertRangeToTextRange)(range, parseResults.tokenizerOutput.lines)) !== null && _b !== void 0 ? _b : assignment;
|
157
|
-
if (decl.docString !== undefined) {
|
158
|
-
const docNode = (0, parseTreeUtils_1.getVariableDocStringNode)(decl.node);
|
159
|
-
if (docNode) {
|
160
|
-
textRange_1.TextRange.extend(textRange, docNode);
|
161
|
-
}
|
162
|
-
}
|
163
|
-
return textRange;
|
164
|
-
}
|
165
|
-
return decl.node;
|
166
|
-
}
|
167
|
-
static getSymbolFullStatementTextRange(parseResults, decl) {
|
168
|
-
var _a, _b;
|
169
|
-
const statementNode = (0, declaration_1.isVariableDeclaration)(decl)
|
170
|
-
? (_a = (0, parseTreeUtils_1.getFirstAncestorOrSelfOfKind)(decl.node, 3 /* Assignment */)) !== null && _a !== void 0 ? _a : decl.node
|
171
|
-
: decl.node;
|
172
|
-
const range = (0, parseTreeUtils_1.getFullStatementRange)(statementNode, parseResults, {
|
173
|
-
includeTrailingBlankLines: true,
|
174
|
-
});
|
175
|
-
return (_b = (0, positionUtils_1.convertRangeToTextRange)(range, parseResults.tokenizerOutput.lines)) !== null && _b !== void 0 ? _b : statementNode;
|
176
|
-
}
|
177
|
-
static getRenameModulePath(declarations) {
|
178
|
-
// If we have a decl with no node, we will prefer that decl over others.
|
179
|
-
// The decl with no node is a synthesized alias decl created only for IDE case
|
180
|
-
// that should point to the right module file.
|
181
|
-
const bestDecl = declarations.find((d) => !d.node);
|
182
|
-
if (bestDecl) {
|
183
|
-
return bestDecl.path;
|
184
|
-
}
|
185
|
-
// Otherwise, prefer stub if we have one. or just return first decl.
|
186
|
-
const declarationPaths = [...declarations.reduce((s, d) => s.add(d.path), new Set())];
|
187
|
-
const stubIndex = declarationPaths.findIndex((d) => (0, sourceMapper_1.isStubFile)(d));
|
188
|
-
if (stubIndex >= 0) {
|
189
|
-
return declarationPaths[stubIndex];
|
190
|
-
}
|
191
|
-
return declarationPaths[0];
|
192
|
-
}
|
193
|
-
static getRenameModulePathInfo(declarationPath, newName) {
|
194
|
-
const filePath = getFilePathToRename(declarationPath);
|
195
|
-
const newFilePath = replaceFileName(filePath, newName);
|
196
|
-
return { filePath, newFilePath };
|
197
|
-
function getFilePathToRename(filePath) {
|
198
|
-
const fileName = (0, pathUtils_1.stripFileExtension)((0, pathUtils_1.getFileName)(filePath));
|
199
|
-
if (fileName === '__init__') {
|
200
|
-
return (0, pathUtils_1.getDirectoryPath)(filePath);
|
201
|
-
}
|
202
|
-
return filePath;
|
203
|
-
}
|
204
|
-
function replaceFileName(filePath, newName) {
|
205
|
-
const ext = (0, pathUtils_1.getFileExtension)(filePath);
|
206
|
-
const directory = (0, pathUtils_1.getDirectoryPath)(filePath);
|
207
|
-
return (0, pathUtils_1.combinePaths)(directory, `${newName}${ext}`);
|
208
|
-
}
|
209
|
-
}
|
210
|
-
getEdits() {
|
211
|
-
return this._textEditTracker.getEdits(this._token);
|
212
|
-
}
|
213
|
-
renameReferences(parseResults) {
|
214
|
-
switch (this._type) {
|
215
|
-
case UpdateType.Folder:
|
216
|
-
return this._renameFolderReferences(parseResults);
|
217
|
-
case UpdateType.File:
|
218
|
-
return this._renameModuleReferences(parseResults);
|
219
|
-
case UpdateType.Symbol:
|
220
|
-
return this._updateSymbolReferences(parseResults);
|
221
|
-
default:
|
222
|
-
return (0, debug_1.assertNever)(this._type, `${this._type} is unknown`);
|
223
|
-
}
|
224
|
-
}
|
225
|
-
tryGetFirstSymbolUsage(parseResults, symbol) {
|
226
|
-
var _a, _b, _c;
|
227
|
-
const name = (_b = (_a = symbol === null || symbol === void 0 ? void 0 : symbol.name) !== null && _a !== void 0 ? _a : (0, declarationUtils_1.getNameFromDeclaration)(this.declarations[0])) !== null && _b !== void 0 ? _b : '';
|
228
|
-
const collector = new documentSymbolCollector_1.DocumentSymbolCollector([name], (_c = symbol === null || symbol === void 0 ? void 0 : symbol.decls) !== null && _c !== void 0 ? _c : this.declarations, this._evaluator, this._token, parseResults.parseTree,
|
229
|
-
/* treatModuleImportAndFromImportSame */ true,
|
230
|
-
/* skipUnreachableCode */ false);
|
231
|
-
for (const result of collector.collect().sort((r1, r2) => r1.range.start - r2.range.start)) {
|
232
|
-
// We only care about symbol usages, not alias decl of the symbol.
|
233
|
-
if ((0, parseTreeUtils_1.isImportModuleName)(result.node) ||
|
234
|
-
(0, parseTreeUtils_1.isImportAlias)(result.node) ||
|
235
|
-
(0, parseTreeUtils_1.isFromImportModuleName)(result.node) ||
|
236
|
-
(0, parseTreeUtils_1.isFromImportName)(result.node) ||
|
237
|
-
(0, parseTreeUtils_1.isFromImportAlias)(result.node)) {
|
238
|
-
continue;
|
239
|
-
}
|
240
|
-
return result.range.start;
|
241
|
-
}
|
242
|
-
return undefined;
|
243
|
-
}
|
244
|
-
get _moduleName() {
|
245
|
-
return this._moduleNameAndType.moduleName;
|
246
|
-
}
|
247
|
-
get _newLastModuleName() {
|
248
|
-
return this._newModuleNames[this._newModuleNames.length - 1];
|
249
|
-
}
|
250
|
-
get _newModuleName() {
|
251
|
-
return this._newModuleNameAndType.moduleName;
|
252
|
-
}
|
253
|
-
static _create(importResolver, configOptions, evaluator, moduleFilePath, newModuleFilePath, type, tokenOrDeclarations, token) {
|
254
|
-
const execEnv = configOptions.findExecEnvironment(moduleFilePath);
|
255
|
-
const moduleName = importResolver.getModuleNameForImport(moduleFilePath, execEnv);
|
256
|
-
if (!moduleName.moduleName) {
|
257
|
-
return undefined;
|
258
|
-
}
|
259
|
-
const newModuleName = importResolver.getModuleNameForImport(newModuleFilePath, execEnv);
|
260
|
-
if (!newModuleName.moduleName) {
|
261
|
-
return undefined;
|
262
|
-
}
|
263
|
-
token = vscode_languageserver_1.CancellationToken.is(tokenOrDeclarations) ? tokenOrDeclarations : token;
|
264
|
-
const declarations = vscode_languageserver_1.CancellationToken.is(tokenOrDeclarations) ? [] : tokenOrDeclarations;
|
265
|
-
if (declarations.length === 0) {
|
266
|
-
// Create synthesized alias decls from the given file path. If the given file is for stub,
|
267
|
-
// create one for the corresponding py file as well.
|
268
|
-
declarations.push((0, declarationUtils_1.createSynthesizedAliasDeclaration)(moduleFilePath));
|
269
|
-
if ((0, sourceMapper_1.isStubFile)(moduleFilePath)) {
|
270
|
-
// The resolveImport should make sure non stub file search to happen.
|
271
|
-
importResolver.resolveImport(moduleFilePath, execEnv, (0, importResolver_1.createImportedModuleDescriptor)(moduleName.moduleName));
|
272
|
-
importResolver
|
273
|
-
.getSourceFilesFromStub(moduleFilePath, execEnv, /* mapCompiled */ false)
|
274
|
-
.forEach((p) => declarations.push((0, declarationUtils_1.createSynthesizedAliasDeclaration)(p)));
|
275
|
-
}
|
276
|
-
}
|
277
|
-
return new RenameModuleProvider(importResolver.fileSystem, evaluator, moduleFilePath, newModuleFilePath, moduleName, newModuleName, type, declarations, token);
|
278
|
-
}
|
279
|
-
_updateSymbolReferences(parseResults) {
|
280
|
-
const filePath = (0, analyzerNodeInfo_1.getFileInfo)(parseResults.parseTree).filePath;
|
281
|
-
const isSource = filePath === this._moduleFilePath;
|
282
|
-
const collector = new documentSymbolCollector_1.DocumentSymbolCollector([(0, declarationUtils_1.getNameFromDeclaration)(this.declarations[0]) || ''], this.declarations, this._evaluator, this._token, parseResults.parseTree,
|
283
|
-
/* treatModuleImportAndFromImportSame */ true,
|
284
|
-
/* skipUnreachableCode */ false);
|
285
|
-
// See if we need to insert new import statement
|
286
|
-
const importStatements = (0, importStatementUtils_1.getTopLevelImports)(parseResults.parseTree, /* includeImplicitImports */ true);
|
287
|
-
// See whether we have existing import statement for the same module
|
288
|
-
// ex) import [moduleName] or from ... import [moduleName]
|
289
|
-
const imported = importStatements.orderedImports.find((i) => i.moduleName === this._newModuleName);
|
290
|
-
// Indicate whether current file has any usage of the symbol
|
291
|
-
let hasSymbolUsage = false;
|
292
|
-
const wildcardImports = new Map();
|
293
|
-
const importUsed = new Map();
|
294
|
-
for (const result of collector.collect()) {
|
295
|
-
const nodeFound = result.node;
|
296
|
-
if (nodeFound.nodeType === 49 /* String */) {
|
297
|
-
if (isSource) {
|
298
|
-
// Delete the symbol reference in __all__ if the file is the source file.
|
299
|
-
this._textEditTracker.addEditWithTextRange(parseResults, nodeFound, '');
|
300
|
-
}
|
301
|
-
continue;
|
302
|
-
}
|
303
|
-
if ((0, parseTreeUtils_1.isFromImportName)(nodeFound)) {
|
304
|
-
this._updateNameInFromImportForSymbolReferences(parseResults, importStatements, nodeFound);
|
305
|
-
continue;
|
306
|
-
}
|
307
|
-
// Exclude symbol decl itself.
|
308
|
-
hasSymbolUsage || (hasSymbolUsage = isSource
|
309
|
-
? !this.declarations.some((d) => textRange_1.TextRange.containsRange(d.node, nodeFound))
|
310
|
-
: true);
|
311
|
-
const dottedName = (0, parseTreeUtils_1.getDottedNameWithGivenNodeAsLastName)(nodeFound);
|
312
|
-
if (dottedName === nodeFound || dottedName.nodeType !== 35 /* MemberAccess */) {
|
313
|
-
this._collectWildcardImports(nodeFound, wildcardImports);
|
314
|
-
// ex) from module import foo
|
315
|
-
// foo
|
316
|
-
// foo.method()
|
317
|
-
continue;
|
318
|
-
}
|
319
|
-
this._collectSymbolReferencesPerImports(dottedName, importUsed);
|
320
|
-
}
|
321
|
-
if (isSource && hasSymbolUsage) {
|
322
|
-
// If the original file has references to the symbol moved, we need to either
|
323
|
-
// insert import statement or update existing one.
|
324
|
-
const newModuleName = (imported === null || imported === void 0 ? void 0 : imported.node.nodeType) === 22 /* ImportFrom */
|
325
|
-
? this._getNewModuleName(filePath, imported.node.module.leadingDots > 0,
|
326
|
-
/* isLastPartImportName */ false)
|
327
|
-
: undefined;
|
328
|
-
const options = (imported === null || imported === void 0 ? void 0 : imported.node.nodeType) === 22 /* ImportFrom */
|
329
|
-
? {
|
330
|
-
currentFromImport: imported.node,
|
331
|
-
originalModuleName: this._moduleName,
|
332
|
-
}
|
333
|
-
: undefined;
|
334
|
-
this._textEditTracker.addOrUpdateImport(parseResults, importStatements, { name: this._newModuleName, nameForImportFrom: newModuleName }, (0, importStatementUtils_1.getImportGroupFromModuleNameAndType)(this._newModuleNameAndType), [{ name: (0, declarationUtils_1.getNameFromDeclaration)(this.declarations[0]) }], options);
|
335
|
-
}
|
336
|
-
// Handle symbol references that are used off wildcard imports.
|
337
|
-
this._processSymbolReferenceOffWildcardImports(parseResults, importStatements, wildcardImports);
|
338
|
-
// Handle symbol references that are used off imported modules.
|
339
|
-
this._processSymbolReferenceOffImports(parseResults, importStatements, imported, importUsed);
|
340
|
-
}
|
341
|
-
_processSymbolReferenceOffImports(parseResults, importStatements, imported, importUsed) {
|
342
|
-
const filePath = (0, analyzerNodeInfo_1.getFileInfo)(parseResults.parseTree).filePath;
|
343
|
-
const isDestination = filePath === this._newModuleFilePath;
|
344
|
-
if (isDestination) {
|
345
|
-
for (const [key, value] of importUsed) {
|
346
|
-
if (this._canReplaceImportName(parseResults, key, value)) {
|
347
|
-
// We can remove existing import statement.
|
348
|
-
this._textEditTracker.deleteImportName(parseResults, key);
|
349
|
-
}
|
350
|
-
for (const node of value) {
|
351
|
-
this._textEditTracker.addEditWithTextRange(parseResults, textRange_1.TextRange.fromBounds(node.start, node.memberName.start), '');
|
352
|
-
}
|
353
|
-
}
|
354
|
-
return;
|
355
|
-
}
|
356
|
-
// Other files.
|
357
|
-
for (const [key, value] of importUsed) {
|
358
|
-
let referenceModuleName;
|
359
|
-
if (this._canReplaceImportName(parseResults, key, value)) {
|
360
|
-
const moduleName = this._getReferenceModuleName(importStatements, imported);
|
361
|
-
if (key.nodeType === 21 /* ImportAs */) {
|
362
|
-
if (moduleName) {
|
363
|
-
referenceModuleName = moduleName;
|
364
|
-
this._textEditTracker.deleteImportName(parseResults, key);
|
365
|
-
}
|
366
|
-
else {
|
367
|
-
referenceModuleName = key.alias ? key.alias.value : this._newModuleName;
|
368
|
-
this._textEditTracker.addEditWithTextRange(parseResults, key.module, this._newModuleName);
|
369
|
-
}
|
370
|
-
}
|
371
|
-
else {
|
372
|
-
if (moduleName) {
|
373
|
-
referenceModuleName = moduleName;
|
374
|
-
this._textEditTracker.deleteImportName(parseResults, key);
|
375
|
-
}
|
376
|
-
else {
|
377
|
-
const fromNode = key.parent;
|
378
|
-
const newModuleName = this._getNewModuleName(filePath, fromNode.module.leadingDots > 0,
|
379
|
-
/* isLastPartImportName */ true);
|
380
|
-
referenceModuleName = key.alias ? key.alias.value : this._newLastModuleName;
|
381
|
-
this._textEditTracker.addEditWithTextRange(parseResults, fromNode.module, newModuleName);
|
382
|
-
this._textEditTracker.addEditWithTextRange(parseResults, key.name, this._newLastModuleName);
|
383
|
-
}
|
384
|
-
}
|
385
|
-
}
|
386
|
-
else {
|
387
|
-
const moduleName = this._getReferenceModuleName(importStatements, imported);
|
388
|
-
if (moduleName) {
|
389
|
-
referenceModuleName = moduleName;
|
390
|
-
}
|
391
|
-
else {
|
392
|
-
referenceModuleName = this._newModuleName;
|
393
|
-
this._textEditTracker.addOrUpdateImport(parseResults, importStatements, { name: this._newModuleName }, (0, importStatementUtils_1.getImportGroupFromModuleNameAndType)(this._newModuleNameAndType));
|
394
|
-
}
|
395
|
-
}
|
396
|
-
for (const node of value) {
|
397
|
-
this._textEditTracker.addEditWithTextRange(parseResults, node.leftExpression, referenceModuleName);
|
398
|
-
}
|
399
|
-
}
|
400
|
-
}
|
401
|
-
_processSymbolReferenceOffWildcardImports(parseResults, importStatements, wildcardImports) {
|
402
|
-
const filePath = (0, analyzerNodeInfo_1.getFileInfo)(parseResults.parseTree).filePath;
|
403
|
-
const isDestination = filePath === this._newModuleFilePath;
|
404
|
-
if (isDestination) {
|
405
|
-
// Destination file contains the moved symbol decl. no need to insert
|
406
|
-
// import statement for the symbol moved.
|
407
|
-
return;
|
408
|
-
}
|
409
|
-
for (const [key, value] of wildcardImports) {
|
410
|
-
const fromNode = key;
|
411
|
-
const newModuleName = this._getNewModuleName(filePath, fromNode.module.leadingDots > 0,
|
412
|
-
/* isLastPartImportName */ false);
|
413
|
-
this._textEditTracker.addOrUpdateImport(parseResults, importStatements, { name: this._newModuleName, nameForImportFrom: newModuleName }, (0, importStatementUtils_1.getImportGroupFromModuleNameAndType)(this._newModuleNameAndType), [...value].map((v) => ({ name: v })), {
|
414
|
-
currentFromImport: fromNode,
|
415
|
-
originalModuleName: this._moduleName,
|
416
|
-
});
|
417
|
-
}
|
418
|
-
}
|
419
|
-
_collectSymbolReferencesPerImports(dottedName, importUsed) {
|
420
|
-
var _a;
|
421
|
-
const moduleName = dottedName.leftExpression.nodeType === 35 /* MemberAccess */
|
422
|
-
? dottedName.leftExpression.memberName
|
423
|
-
: dottedName.leftExpression.nodeType === 38 /* Name */
|
424
|
-
? dottedName.leftExpression
|
425
|
-
: undefined;
|
426
|
-
if (!moduleName) {
|
427
|
-
// ex) from module import foo
|
428
|
-
// getModule().foo
|
429
|
-
return;
|
430
|
-
}
|
431
|
-
const moduleDecl = (_a = this._evaluator
|
432
|
-
.getDeclarationsForNameNode(moduleName)) === null || _a === void 0 ? void 0 : _a.filter((d) => (0, declaration_1.isAliasDeclaration)(d) &&
|
433
|
-
(d.node.nodeType === 21 /* ImportAs */ || d.node.nodeType === 23 /* ImportFromAs */));
|
434
|
-
if (!moduleDecl || moduleDecl.length === 0) {
|
435
|
-
// ex) from xxx import yyy
|
436
|
-
// yyy.property.foo
|
437
|
-
return;
|
438
|
-
}
|
439
|
-
const importAs = moduleDecl[0].node;
|
440
|
-
(0, collectionUtils_1.getOrAdd)(importUsed, importAs, () => []).push(dottedName);
|
441
|
-
}
|
442
|
-
_collectWildcardImports(nodeFound, wildcardImports) {
|
443
|
-
const nameDecls = this._evaluator.getDeclarationsForNameNode(nodeFound);
|
444
|
-
const aliasDeclFromWildCardImport = nameDecls === null || nameDecls === void 0 ? void 0 : nameDecls.find((d) => d.node.nodeType === 22 /* ImportFrom */ && d.node.isWildcardImport);
|
445
|
-
if (!aliasDeclFromWildCardImport || !(0, declaration_1.isAliasDeclaration)(aliasDeclFromWildCardImport)) {
|
446
|
-
return;
|
447
|
-
}
|
448
|
-
// ex) from module import *
|
449
|
-
// foo()
|
450
|
-
// bar()
|
451
|
-
(0, collectionUtils_1.getOrAdd)(wildcardImports, aliasDeclFromWildCardImport.node, () => new Set()).add(nodeFound.value);
|
452
|
-
}
|
453
|
-
_updateNameInFromImportForSymbolReferences(parseResults, importStatements, nodeFound) {
|
454
|
-
var _a;
|
455
|
-
const filePath = (0, analyzerNodeInfo_1.getFileInfo)(parseResults.parseTree).filePath;
|
456
|
-
const isDestination = filePath === this._newModuleFilePath;
|
457
|
-
// ex) from ... import [symbol] ...
|
458
|
-
const importFromAs = nodeFound.parent;
|
459
|
-
const fromNode = importFromAs === null || importFromAs === void 0 ? void 0 : importFromAs.parent;
|
460
|
-
const newModuleName = this._getNewModuleName(filePath, fromNode.module.leadingDots > 0,
|
461
|
-
/* isLastPartImportName */ false);
|
462
|
-
if (isDestination) {
|
463
|
-
// If we have import statement for the symbol in the destination file,
|
464
|
-
// we need to remove it.
|
465
|
-
// ex) "from module import symbol, another_symbol" to
|
466
|
-
// "from module import another_symbol"
|
467
|
-
this._textEditTracker.deleteImportName(parseResults, importFromAs);
|
468
|
-
return;
|
469
|
-
}
|
470
|
-
if (fromNode.imports.length === 1) {
|
471
|
-
// ex) "from [module] import symbol" to "from [module.changed] import symbol"
|
472
|
-
this._textEditTracker.addEditWithTextRange(parseResults, fromNode.module, newModuleName);
|
473
|
-
return;
|
474
|
-
}
|
475
|
-
// ex) "from module import symbol, another_symbol" to
|
476
|
-
// "from module import another_symbol" and "from module.changed import symbol"
|
477
|
-
// Delete the existing import name including alias.
|
478
|
-
this._textEditTracker.deleteImportName(parseResults, importFromAs);
|
479
|
-
// For now, this won't merge absolute and relative path "from import" statement.
|
480
|
-
const importNameInfo = {
|
481
|
-
name: importFromAs.name.value,
|
482
|
-
alias: (_a = importFromAs.alias) === null || _a === void 0 ? void 0 : _a.value,
|
483
|
-
};
|
484
|
-
this._textEditTracker.addOrUpdateImport(parseResults, importStatements, { name: this._newModuleName, nameForImportFrom: newModuleName }, (0, importStatementUtils_1.getImportGroupFromModuleNameAndType)(this._newModuleNameAndType), [importNameInfo], {
|
485
|
-
currentFromImport: fromNode,
|
486
|
-
originalModuleName: this._moduleName,
|
487
|
-
});
|
488
|
-
}
|
489
|
-
_getReferenceModuleName(importStatements, imported) {
|
490
|
-
var _a, _b;
|
491
|
-
if (imported && imported.node.nodeType === 20 /* Import */) {
|
492
|
-
return ((_a = imported.subnode) === null || _a === void 0 ? void 0 : _a.alias) ? imported.subnode.alias.value : this._newModuleName;
|
493
|
-
}
|
494
|
-
else if ((_b = importStatements.implicitImports) === null || _b === void 0 ? void 0 : _b.has(this._newModuleFilePath)) {
|
495
|
-
const fromImportAs = importStatements.implicitImports.get(this._newModuleFilePath);
|
496
|
-
return fromImportAs.alias ? fromImportAs.alias.value : fromImportAs.name.value;
|
497
|
-
}
|
498
|
-
return undefined;
|
499
|
-
}
|
500
|
-
_canReplaceImportName(parseResults, importAs, symbolReferences) {
|
501
|
-
var _a;
|
502
|
-
const nameToBind = (_a = importAs.alias) !== null && _a !== void 0 ? _a : (importAs.nodeType === 21 /* ImportAs */
|
503
|
-
? importAs.module.nameParts[importAs.module.nameParts.length - 1]
|
504
|
-
: importAs.name);
|
505
|
-
const declarations = documentSymbolCollector_1.DocumentSymbolCollector.getDeclarationsForNode(nameToBind, this._evaluator,
|
506
|
-
/* resolveLocalName */ false, documentSymbolCollector_1.DocumentSymbolCollectorUseCase.Rename, this._token);
|
507
|
-
if (declarations.length === 0) {
|
508
|
-
return false;
|
509
|
-
}
|
510
|
-
const collector = new documentSymbolCollector_1.DocumentSymbolCollector([nameToBind.value], declarations, this._evaluator, this._token, parseResults.parseTree,
|
511
|
-
/* treatModuleImportAndFromImportSame */ true,
|
512
|
-
/* skipUnreachableCode */ false);
|
513
|
-
for (const result of collector.collect()) {
|
514
|
-
if ((0, parseTreeUtils_1.isImportModuleName)(result.node) ||
|
515
|
-
(0, parseTreeUtils_1.isImportAlias)(result.node) ||
|
516
|
-
(0, parseTreeUtils_1.isFromImportModuleName)(result.node) ||
|
517
|
-
(0, parseTreeUtils_1.isFromImportName)(result.node) ||
|
518
|
-
(0, parseTreeUtils_1.isFromImportAlias)(result.node)) {
|
519
|
-
// collector will report decls as well. ignore decls.
|
520
|
-
continue;
|
521
|
-
}
|
522
|
-
// other symbols from the module are used in the file.
|
523
|
-
if (!symbolReferences.some((s) => textRange_1.TextRange.containsRange(s, result.node))) {
|
524
|
-
return false;
|
525
|
-
}
|
526
|
-
}
|
527
|
-
return true;
|
528
|
-
}
|
529
|
-
_renameFolderReferences(parseResults) {
|
530
|
-
const collector = new documentSymbolCollector_1.DocumentSymbolCollector([this.lastModuleName], this.declarations, this._evaluator, this._token, parseResults.parseTree,
|
531
|
-
/* treatModuleImportAndFromImportSame */ true,
|
532
|
-
/* skipUnreachableCode */ false);
|
533
|
-
// We only support simple rename of folder. Change all occurrence of the old folder name
|
534
|
-
// to new name.
|
535
|
-
for (const result of collector.collect()) {
|
536
|
-
this._textEditTracker.addEditWithTextRange(parseResults, result.range, this._newLastModuleName);
|
537
|
-
}
|
538
|
-
}
|
539
|
-
_renameModuleReferences(parseResults) {
|
540
|
-
const collector = new documentSymbolCollector_1.DocumentSymbolCollector([this.lastModuleName], this.declarations, this._evaluator, this._token, parseResults.parseTree,
|
541
|
-
/* treatModuleImportAndFromImportSame */ true,
|
542
|
-
/* skipUnreachableCode */ false);
|
543
|
-
const results = collector.collect();
|
544
|
-
// Update module references first.
|
545
|
-
this._updateModuleReferences(parseResults, results);
|
546
|
-
// If the module file has moved, we need to update all relative paths used in the file to reflect the move.
|
547
|
-
this._updateRelativeModuleNamePath(parseResults, results);
|
548
|
-
}
|
549
|
-
_updateRelativeModuleNamePath(parseResults, results) {
|
550
|
-
const filePath = (0, analyzerNodeInfo_1.getFileInfo)(parseResults.parseTree).filePath;
|
551
|
-
if (filePath !== this._moduleFilePath) {
|
552
|
-
// We only update relative import paths for the file that has moved.
|
553
|
-
return;
|
554
|
-
}
|
555
|
-
let importStatements;
|
556
|
-
// Filter out module name that is already re-written.
|
557
|
-
for (const edit of this._getNewRelativeModuleNamesForFileMoved(filePath, ModuleNameCollector.collect(parseResults.parseTree).filter((m) => !results.some((r) => textRange_1.TextRange.containsRange(m.parent, r.node))))) {
|
558
|
-
this._textEditTracker.addEditWithTextRange(parseResults, edit.moduleName, edit.newModuleName);
|
559
|
-
if (!edit.itemsToMove) {
|
560
|
-
continue;
|
561
|
-
}
|
562
|
-
// This could introduce multiple import statements for same modules with
|
563
|
-
// different symbols per module name. Unfortunately, there is no easy way to
|
564
|
-
// prevent it since we can't see changes made by other code until all changes
|
565
|
-
// are committed. In future, if we support snapshot and diff between snapshots,
|
566
|
-
// then we can support those complex code generations.
|
567
|
-
const fromNode = edit.moduleName.parent;
|
568
|
-
// First, delete existing exported symbols from "from import" statement.
|
569
|
-
for (const importFromAs of edit.itemsToMove) {
|
570
|
-
this._textEditTracker.deleteImportName(parseResults, importFromAs);
|
571
|
-
}
|
572
|
-
importStatements =
|
573
|
-
importStatements !== null && importStatements !== void 0 ? importStatements : (0, importStatementUtils_1.getTopLevelImports)(parseResults.parseTree, /* includeImplicitImports */ false);
|
574
|
-
// For now, this won't merge absolute and relative path "from import"
|
575
|
-
// statement.
|
576
|
-
this._textEditTracker.addOrUpdateImport(parseResults, importStatements, {
|
577
|
-
name: this._newModuleName,
|
578
|
-
nameForImportFrom: (0, importStatementUtils_1.getRelativeModuleName)(this._fs, this._newModuleFilePath, this._newModuleFilePath,
|
579
|
-
/* ignoreFolderStructure */ false,
|
580
|
-
/* sourceIsFile */ true),
|
581
|
-
}, (0, importStatementUtils_1.getImportGroupFromModuleNameAndType)(this._newModuleNameAndType), edit.itemsToMove.map((i) => {
|
582
|
-
var _a;
|
583
|
-
return { name: i.name.value, alias: (_a = i.alias) === null || _a === void 0 ? void 0 : _a.value };
|
584
|
-
}), {
|
585
|
-
currentFromImport: fromNode,
|
586
|
-
originalModuleName: this._moduleName,
|
587
|
-
});
|
588
|
-
}
|
589
|
-
}
|
590
|
-
_updateModuleReferences(parseResults, results) {
|
591
|
-
var _a, _b, _c, _d, _e;
|
592
|
-
const filePath = (0, analyzerNodeInfo_1.getFileInfo)(parseResults.parseTree).filePath;
|
593
|
-
let importStatements;
|
594
|
-
for (const result of results) {
|
595
|
-
const nodeFound = result.node;
|
596
|
-
if (nodeFound.nodeType === 49 /* String */) {
|
597
|
-
// ex) __all__ = ["[a]"]
|
598
|
-
this._textEditTracker.addEditWithTextRange(parseResults, result.range, this._newLastModuleName);
|
599
|
-
continue;
|
600
|
-
}
|
601
|
-
if ((0, parseTreeUtils_1.isImportModuleName)(nodeFound)) {
|
602
|
-
if (!(0, parseTreeUtils_1.isLastNameOfModuleName)(nodeFound)) {
|
603
|
-
// It must be directory and we don't support folder rename.
|
604
|
-
continue;
|
605
|
-
}
|
606
|
-
const moduleNameNode = (0, parseTreeUtils_1.getFirstAncestorOrSelfOfKind)(nodeFound, 37 /* ModuleName */);
|
607
|
-
// * Enhancement * one case we don't handle is introducing new symbol in __all__
|
608
|
-
// or converting "import" statement to "from import" statement.
|
609
|
-
//
|
610
|
-
// when the existing statement was "import x as x" and it is changed to
|
611
|
-
// "import y.z as z". we either need to introduce "z" in __all__ or convert
|
612
|
-
// "import y.z as z" to "from y import z as z" to make sure we keep the symbol
|
613
|
-
// visibility same.
|
614
|
-
//
|
615
|
-
// when we convert "import x as x" to "from y import z as z", we need to handle
|
616
|
-
// deletion of existing import statement or (x as x) and inserting/merging
|
617
|
-
// new "from import" statement.
|
618
|
-
// If original module name was single word and it becomes dotted name without alias,
|
619
|
-
// then we introduce alias to keep references as a single word.
|
620
|
-
// ex) import [xxx] to import [aaa.bbb as bbb]
|
621
|
-
if (moduleNameNode.nameParts.length === 1 &&
|
622
|
-
((_a = moduleNameNode.parent) === null || _a === void 0 ? void 0 : _a.nodeType) === 21 /* ImportAs */ &&
|
623
|
-
!moduleNameNode.parent.alias &&
|
624
|
-
this._newModuleNames.length > 1) {
|
625
|
-
this._aliasIntroduced.add(moduleNameNode.parent);
|
626
|
-
this._textEditTracker.addEditWithTextRange(parseResults, moduleNameNode, `${this._newModuleName} as ${this._newLastModuleName}`);
|
627
|
-
continue;
|
628
|
-
}
|
629
|
-
// Otherwise, update whole module name to new name
|
630
|
-
// ex) import [xxx.yyy] to import [aaa.bbb]
|
631
|
-
this._textEditTracker.addEditWithTextRange(parseResults, moduleNameNode, this._newModuleName);
|
632
|
-
continue;
|
633
|
-
}
|
634
|
-
if ((0, parseTreeUtils_1.isImportAlias)(nodeFound)) {
|
635
|
-
// ex) import xxx as [yyy] to import xxx as [zzz]
|
636
|
-
this._textEditTracker.addEditWithTextRange(parseResults, result.range, this._newLastModuleName);
|
637
|
-
continue;
|
638
|
-
}
|
639
|
-
if ((0, parseTreeUtils_1.isFromImportModuleName)(nodeFound)) {
|
640
|
-
if (!(0, parseTreeUtils_1.isLastNameOfModuleName)(nodeFound)) {
|
641
|
-
// It must be directory and we don't support folder rename.
|
642
|
-
continue;
|
643
|
-
}
|
644
|
-
const moduleNameNode = (0, parseTreeUtils_1.getFirstAncestorOrSelfOfKind)(nodeFound, 37 /* ModuleName */);
|
645
|
-
const fromNode = moduleNameNode.parent;
|
646
|
-
// We need to check whether imports of this import statement has
|
647
|
-
// any implicit submodule imports or not. If there is one, we need to
|
648
|
-
// either split or leave it as it is.
|
649
|
-
const exportedSymbols = [];
|
650
|
-
const subModules = [];
|
651
|
-
for (const importFromAs of fromNode.imports) {
|
652
|
-
if (this._isExportedSymbol(importFromAs.name)) {
|
653
|
-
exportedSymbols.push(importFromAs);
|
654
|
-
}
|
655
|
-
else {
|
656
|
-
subModules.push(importFromAs);
|
657
|
-
}
|
658
|
-
}
|
659
|
-
if (subModules.length === 0) {
|
660
|
-
// We don't have any sub modules, we can change module name to new one.
|
661
|
-
// Update whole module name to new name.
|
662
|
-
// ex) from [xxx.yyy] import zzz to from [aaa.bbb] import zzz
|
663
|
-
this._textEditTracker.addEditWithTextRange(parseResults, moduleNameNode, this._getNewModuleName(filePath, moduleNameNode.leadingDots > 0,
|
664
|
-
/* isLastPartImportName */ false));
|
665
|
-
continue;
|
666
|
-
}
|
667
|
-
if (exportedSymbols.length === 0) {
|
668
|
-
// We only have sub modules. That means module name actually refers to
|
669
|
-
// folder name, not module (ex, __init__.py). Folder rename is done by
|
670
|
-
// different code path.
|
671
|
-
continue;
|
672
|
-
}
|
673
|
-
// Now, we need to split "from import" statement to 2.
|
674
|
-
// Update module name if needed.
|
675
|
-
if (fromNode.module.leadingDots > 0) {
|
676
|
-
for (const edit of this._getNewRelativeModuleNamesForFileMoved(filePath, [fromNode.module])) {
|
677
|
-
this._textEditTracker.addEditWithTextRange(parseResults, edit.moduleName, edit.newModuleName);
|
678
|
-
}
|
679
|
-
}
|
680
|
-
// First, delete existing exported symbols from "from import" statement.
|
681
|
-
for (const importFromAs of exportedSymbols) {
|
682
|
-
this._textEditTracker.deleteImportName(parseResults, importFromAs);
|
683
|
-
}
|
684
|
-
importStatements =
|
685
|
-
importStatements !== null && importStatements !== void 0 ? importStatements : (0, importStatementUtils_1.getTopLevelImports)(parseResults.parseTree, /* includeImplicitImports */ false);
|
686
|
-
// For now, this won't merge absolute and relative path "from import"
|
687
|
-
// statement.
|
688
|
-
this._textEditTracker.addOrUpdateImport(parseResults, importStatements, { name: this._newModuleName }, (0, importStatementUtils_1.getImportGroupFromModuleNameAndType)(this._newModuleNameAndType), exportedSymbols.map((i) => {
|
689
|
-
var _a;
|
690
|
-
const name = results.findIndex((r) => r.node === i.name) >= 0 ? this._newLastModuleName : i.name.value;
|
691
|
-
const alias = results.findIndex((r) => r.node === i.alias) >= 0
|
692
|
-
? this._newLastModuleName
|
693
|
-
: (_a = i.alias) === null || _a === void 0 ? void 0 : _a.value;
|
694
|
-
return { name, alias };
|
695
|
-
}), {
|
696
|
-
currentFromImport: fromNode,
|
697
|
-
originalModuleName: this._moduleName,
|
698
|
-
});
|
699
|
-
continue;
|
700
|
-
}
|
701
|
-
if ((0, parseTreeUtils_1.isFromImportName)(nodeFound)) {
|
702
|
-
if (this._textEditTracker.isNodeRemoved(nodeFound)) {
|
703
|
-
// Import name is already removed.
|
704
|
-
continue;
|
705
|
-
}
|
706
|
-
const fromNode = (_b = nodeFound.parent) === null || _b === void 0 ? void 0 : _b.parent;
|
707
|
-
const newModuleName = this._getNewModuleName(filePath, fromNode.module.leadingDots > 0,
|
708
|
-
/* isLastPartImportName */ true);
|
709
|
-
// If the name bound to symbol re-exported, we don't need to update module name.
|
710
|
-
// Existing logic should make sure re-exported symbol name work as before after
|
711
|
-
// symbol rename.
|
712
|
-
if (this._isExportedSymbol(nodeFound)) {
|
713
|
-
this._textEditTracker.addEditWithTextRange(parseResults, result.range, this._newLastModuleName);
|
714
|
-
continue;
|
715
|
-
}
|
716
|
-
if (fromNode.imports.length === 1) {
|
717
|
-
// ex) from xxx import [yyy] to from [aaa.bbb] import [zzz]
|
718
|
-
this._textEditTracker.addEditWithTextRange(parseResults, fromNode.module, newModuleName);
|
719
|
-
this._textEditTracker.addEditWithTextRange(parseResults, result.range, this._newLastModuleName);
|
720
|
-
}
|
721
|
-
else {
|
722
|
-
// Delete the existing import name including alias.
|
723
|
-
const importFromAs = nodeFound.parent;
|
724
|
-
// Update module name if needed.
|
725
|
-
if (fromNode.module.leadingDots > 0) {
|
726
|
-
for (const edit of this._getNewRelativeModuleNamesForFileMoved(filePath, [fromNode.module])) {
|
727
|
-
this._textEditTracker.addEditWithTextRange(parseResults, edit.moduleName, edit.newModuleName);
|
728
|
-
}
|
729
|
-
}
|
730
|
-
this._textEditTracker.deleteImportName(parseResults, importFromAs);
|
731
|
-
importStatements =
|
732
|
-
importStatements !== null && importStatements !== void 0 ? importStatements : (0, importStatementUtils_1.getTopLevelImports)(parseResults.parseTree, /* includeImplicitImports */ false);
|
733
|
-
// ex) from xxx import yyy, [zzz] to
|
734
|
-
// from xxx import yyy
|
735
|
-
// from [aaa.bbb] import [ccc]
|
736
|
-
// or
|
737
|
-
// from aaa.bbb import ddd
|
738
|
-
// from xxx import yyy, [zzz] to
|
739
|
-
// from aaa.bbb import [ccc], ddd
|
740
|
-
//
|
741
|
-
// For now, this won't merge absolute and relative path "from import"
|
742
|
-
// statement.
|
743
|
-
const importNameInfo = {
|
744
|
-
name: this._newLastModuleName,
|
745
|
-
alias: ((_c = importFromAs.alias) === null || _c === void 0 ? void 0 : _c.value) === this.lastModuleName
|
746
|
-
? this._newLastModuleName
|
747
|
-
: (_d = importFromAs.alias) === null || _d === void 0 ? void 0 : _d.value,
|
748
|
-
};
|
749
|
-
this._textEditTracker.addOrUpdateImport(parseResults, importStatements, { name: this._newModuleName, nameForImportFrom: newModuleName }, (0, importStatementUtils_1.getImportGroupFromModuleNameAndType)(this._newModuleNameAndType), [importNameInfo], {
|
750
|
-
currentFromImport: fromNode,
|
751
|
-
originalModuleName: this._moduleName,
|
752
|
-
});
|
753
|
-
}
|
754
|
-
continue;
|
755
|
-
}
|
756
|
-
if ((0, parseTreeUtils_1.isFromImportAlias)(nodeFound)) {
|
757
|
-
if (this._textEditTracker.isNodeRemoved(nodeFound)) {
|
758
|
-
// alias is already removed.
|
759
|
-
continue;
|
760
|
-
}
|
761
|
-
// ex) from ccc import xxx as [yyy] to from ccc import xxx as [zzz]
|
762
|
-
this._textEditTracker.addEditWithTextRange(parseResults, result.range, this._newLastModuleName);
|
763
|
-
continue;
|
764
|
-
}
|
765
|
-
/** TODO: if we get more than 1 decls, flag it as attention needed */
|
766
|
-
const decls = documentSymbolCollector_1.DocumentSymbolCollector.getDeclarationsForNode(nodeFound, this._evaluator,
|
767
|
-
/* resolveLocalName */ false, documentSymbolCollector_1.DocumentSymbolCollectorUseCase.Rename, this._token).filter((d) => (0, declaration_1.isAliasDeclaration)(d));
|
768
|
-
if (this._onlyNameChanged) {
|
769
|
-
// Simple case. only name has changed. but not path.
|
770
|
-
// Just replace name to new symbol name.
|
771
|
-
// ex) a.[b].foo() to a.[z].foo()
|
772
|
-
this._textEditTracker.addEditWithTextRange(parseResults, result.range, this._newLastModuleName);
|
773
|
-
continue;
|
774
|
-
}
|
775
|
-
if (decls === null || decls === void 0 ? void 0 : decls.some((d) => !d.usesLocalName &&
|
776
|
-
(!d.node || d.node.nodeType === 21 /* ImportAs */) &&
|
777
|
-
!this._aliasIntroduced.has(d.node))) {
|
778
|
-
const dottedName = (0, parseTreeUtils_1.getDottedNameWithGivenNodeAsLastName)(nodeFound);
|
779
|
-
if (((_e = dottedName.parent) === null || _e === void 0 ? void 0 : _e.nodeType) !== 35 /* MemberAccess */) {
|
780
|
-
// Replace whole dotted name with new module name.
|
781
|
-
this._textEditTracker.addEditWithTextRange(parseResults, dottedName, this._newModuleName);
|
782
|
-
continue;
|
783
|
-
}
|
784
|
-
// Check whether name after me is sub module or not.
|
785
|
-
// ex) a.b.[c]
|
786
|
-
const nextNameDecl = this._evaluator.getDeclarationsForNameNode(dottedName.parent.memberName);
|
787
|
-
if (!nextNameDecl || nextNameDecl.length === 0) {
|
788
|
-
// Next dotted name is sub module. That means dottedName actually refers to folder names, not modules.
|
789
|
-
// and We don't support renaming folder. So, leave things as they are.
|
790
|
-
// ex) import a.b.c
|
791
|
-
// [a.b].[c]
|
792
|
-
continue;
|
793
|
-
}
|
794
|
-
// Next name is actual symbol. Replace whole name to new module name.
|
795
|
-
// ex) import a.b.c
|
796
|
-
// [a.b.c].[foo]()
|
797
|
-
this._textEditTracker.addEditWithTextRange(parseResults, dottedName, this._newModuleName);
|
798
|
-
continue;
|
799
|
-
}
|
800
|
-
if (result.node.value !== this._newLastModuleName) {
|
801
|
-
this._textEditTracker.addEditWithTextRange(parseResults, result.range, this._newLastModuleName);
|
802
|
-
continue;
|
803
|
-
}
|
804
|
-
}
|
805
|
-
}
|
806
|
-
_getNewRelativeModuleNamesForFileMoved(filePath, moduleNames) {
|
807
|
-
if (filePath !== this._moduleFilePath) {
|
808
|
-
// We only update relative import paths for the file that has moved.
|
809
|
-
return [];
|
810
|
-
}
|
811
|
-
const originalFileName = (0, pathUtils_1.stripFileExtension)((0, pathUtils_1.getFileName)(filePath));
|
812
|
-
const originalInit = originalFileName === '__init__';
|
813
|
-
const originalDirectory = (0, pathUtils_1.getDirectoryPath)(filePath);
|
814
|
-
const newNames = [];
|
815
|
-
for (const moduleName of moduleNames) {
|
816
|
-
// Filter out all absolute path.
|
817
|
-
if (moduleName.leadingDots === 0) {
|
818
|
-
continue;
|
819
|
-
}
|
820
|
-
const result = this._getNewModuleNameInfoForFileMoved(moduleName, originalInit, originalDirectory);
|
821
|
-
if (!result) {
|
822
|
-
continue;
|
823
|
-
}
|
824
|
-
const newModuleName = (0, importStatementUtils_1.getRelativeModuleName)(this._fs, result.src, result.dest,
|
825
|
-
/* ignoreFolderStructure */ false,
|
826
|
-
/* sourceIsFile */ true);
|
827
|
-
newNames.push({ moduleName, newModuleName, itemsToMove: result.itemsToMove });
|
828
|
-
}
|
829
|
-
return newNames;
|
830
|
-
}
|
831
|
-
_getNewModuleNameInfoForFileMoved(moduleName, originalInit, originalDirectory) {
|
832
|
-
const importInfo = (0, analyzerNodeInfo_1.getImportInfo)(moduleName);
|
833
|
-
if (!importInfo) {
|
834
|
-
return undefined;
|
835
|
-
}
|
836
|
-
let importPath = importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1];
|
837
|
-
if (!importPath) {
|
838
|
-
// It is possible for the module name to point to namespace folder (no __init__).
|
839
|
-
// See whether we can use some heuristic to get importPath
|
840
|
-
if (moduleName.nameParts.length === 0) {
|
841
|
-
const directory = (0, importStatementUtils_1.getDirectoryLeadingDotsPointsTo)(originalDirectory, moduleName.leadingDots);
|
842
|
-
if (!directory) {
|
843
|
-
return undefined;
|
844
|
-
}
|
845
|
-
// Add fake __init__.py since we know this is namespace folder.
|
846
|
-
importPath = (0, pathUtils_1.combinePaths)(directory, '__init__.py');
|
847
|
-
}
|
848
|
-
else {
|
849
|
-
return undefined;
|
850
|
-
}
|
851
|
-
}
|
852
|
-
// Check whether module is pointing to moved file itself and whether it is __init__
|
853
|
-
if (this._moduleFilePath !== importPath || !originalInit) {
|
854
|
-
return { src: this._newModuleFilePath, dest: importPath };
|
855
|
-
}
|
856
|
-
// Now, moduleName is pointing to __init__ which point to moved file itself.
|
857
|
-
// We need to check whether imports of this import statement has
|
858
|
-
// any implicit submodule imports or not. If there is one, we need to
|
859
|
-
// either split or leave it as it is.
|
860
|
-
const exportedSymbols = [];
|
861
|
-
const subModules = [];
|
862
|
-
for (const importFromAs of moduleName.parent.imports) {
|
863
|
-
if (this._isExportedSymbol(importFromAs.name)) {
|
864
|
-
exportedSymbols.push(importFromAs);
|
865
|
-
}
|
866
|
-
else {
|
867
|
-
subModules.push(importFromAs);
|
868
|
-
}
|
869
|
-
}
|
870
|
-
// Point to itself.
|
871
|
-
if (subModules.length === 0) {
|
872
|
-
return { src: this._newModuleFilePath, dest: this._newModuleFilePath };
|
873
|
-
}
|
874
|
-
// "." is used to point folder location.
|
875
|
-
if (exportedSymbols.length === 0) {
|
876
|
-
return { src: this._newModuleFilePath, dest: this._moduleFilePath };
|
877
|
-
}
|
878
|
-
// now we need to split, provide split info as well.
|
879
|
-
return {
|
880
|
-
src: this._newModuleFilePath,
|
881
|
-
dest: this._moduleFilePath,
|
882
|
-
itemsToMove: [...exportedSymbols],
|
883
|
-
};
|
884
|
-
}
|
885
|
-
_isExportedSymbol(nameNode) {
|
886
|
-
const decls = this._evaluator.getDeclarationsForNameNode(nameNode);
|
887
|
-
if (!decls) {
|
888
|
-
return false;
|
889
|
-
}
|
890
|
-
// If submoduleFallback exists, then, it points to submodule not symbol.
|
891
|
-
return !decls.some((d) => (0, declaration_1.isAliasDeclaration)(d) && d.submoduleFallback);
|
892
|
-
}
|
893
|
-
_getNewModuleName(currentFilePath, isRelativePath, isLastPartImportName) {
|
894
|
-
const filePath = currentFilePath === this._moduleFilePath ? this._newModuleFilePath : currentFilePath;
|
895
|
-
// If the existing code was using relative path, try to keep the relative path.
|
896
|
-
const moduleName = isRelativePath
|
897
|
-
? (0, importStatementUtils_1.getRelativeModuleName)(this._fs, filePath, this._newModuleFilePath, isLastPartImportName,
|
898
|
-
/* sourceIsFile*/ true)
|
899
|
-
: this._newModuleName;
|
900
|
-
if (isLastPartImportName && moduleName.endsWith(this._newLastModuleName)) {
|
901
|
-
const dotPrefix = moduleName === this._newLastModuleName
|
902
|
-
? 0
|
903
|
-
: moduleName.length > this._newLastModuleName.length + 1
|
904
|
-
? moduleName[moduleName.length - this._newLastModuleName.length - 2] !== '.'
|
905
|
-
? 1
|
906
|
-
: 0
|
907
|
-
: 0;
|
908
|
-
const length = moduleName.length - this._newLastModuleName.length - dotPrefix;
|
909
|
-
//ex) x.y.z used in "from x.y import z"
|
910
|
-
const newModuleName = moduleName.substr(0, length);
|
911
|
-
return newModuleName.length > 0 ? newModuleName : '.';
|
912
|
-
}
|
913
|
-
// ex) x.y.z used in "from x.y.z import ..."
|
914
|
-
return moduleName;
|
915
|
-
}
|
916
|
-
}
|
917
|
-
exports.RenameModuleProvider = RenameModuleProvider;
|
918
|
-
class ModuleNameCollector extends parseTreeWalker_1.ParseTreeWalker {
|
919
|
-
constructor() {
|
920
|
-
super(...arguments);
|
921
|
-
this._result = [];
|
922
|
-
}
|
923
|
-
walk(node) {
|
924
|
-
if ((0, parseNodes_1.isExpressionNode)(node)) {
|
925
|
-
return;
|
926
|
-
}
|
927
|
-
super.walk(node);
|
928
|
-
}
|
929
|
-
visitModuleName(node) {
|
930
|
-
this._result.push(node);
|
931
|
-
return false;
|
932
|
-
}
|
933
|
-
static collect(root) {
|
934
|
-
const collector = new ModuleNameCollector();
|
935
|
-
collector.walk(root);
|
936
|
-
return collector._result;
|
937
|
-
}
|
938
|
-
}
|
939
|
-
//# sourceMappingURL=renameModuleProvider.js.map
|