@tslite/services 1.16.0 → 1.17.0
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/NOTICE +3 -1
- package/dist/_namespaces/edit.d.ts +1 -1
- package/dist/_namespaces/edit.js +10759 -11
- package/dist/_namespaces/ts.codefix.d.ts +0 -1
- package/dist/_namespaces/ts.js +10196 -19
- package/dist/classifier.d.ts +3 -1
- package/dist/codeFixProvider.d.ts +2 -1
- package/dist/codefixes/helpers.d.ts +3 -1
- package/dist/codefixes/importFixes.d.ts +3 -1
- package/dist/completions.d.ts +5 -2
- package/dist/documentHighlights.d.ts +3 -1
- package/dist/documentRegistry.d.ts +2 -1
- package/dist/findAllReferences.d.ts +3 -1
- package/dist/formatting/formatting.d.ts +3 -2
- package/dist/formatting/formattingContext.d.ts +2 -1
- package/dist/formatting/formattingScanner.d.ts +2 -2
- package/dist/formatting/rule.d.ts +1 -1
- package/dist/formatting/rulesMap.d.ts +1 -1
- package/dist/formatting/smartIndenter.d.ts +2 -1
- package/dist/getEditsForFileRename.d.ts +4 -1
- package/dist/goToDefinition.d.ts +3 -1
- package/dist/importTracker.d.ts +2 -1
- package/dist/navigationBar.d.ts +3 -1
- package/dist/organizeImports.d.ts +4 -2
- package/dist/outliningElementsCollector.d.ts +3 -1
- package/dist/refactorProvider.d.ts +1 -1
- package/dist/refactors/extractSymbol.d.ts +2 -1
- package/dist/rename.d.ts +3 -1
- package/dist/services.d.ts +5 -2
- package/dist/signatureHelp.d.ts +3 -1
- package/dist/smartSelection.d.ts +2 -1
- package/dist/stringCompletions.d.ts +3 -1
- package/dist/suggestionDiagnostics.d.ts +4 -3
- package/dist/symbolDisplay.d.ts +3 -1
- package/dist/textChanges.d.ts +4 -2
- package/dist/types.d.ts +3 -3
- package/dist/utilities.d.ts +7 -2
- package/package.json +7 -5
- package/dist/_namespaces/ts.Completions.StringCompletions.js +0 -2
- package/dist/_namespaces/ts.Completions.js +0 -3
- package/dist/_namespaces/ts.FindAllReferences.js +0 -3
- package/dist/_namespaces/ts.GoToDefinition.js +0 -2
- package/dist/_namespaces/ts.NavigationBar.js +0 -2
- package/dist/_namespaces/ts.OrganizeImports.js +0 -2
- package/dist/_namespaces/ts.OutliningElementsCollector.js +0 -2
- package/dist/_namespaces/ts.Rename.js +0 -2
- package/dist/_namespaces/ts.SignatureHelp.js +0 -2
- package/dist/_namespaces/ts.SmartSelectionRange.js +0 -2
- package/dist/_namespaces/ts.SymbolDisplay.js +0 -2
- package/dist/_namespaces/ts.codefix.js +0 -27
- package/dist/_namespaces/ts.formatting.js +0 -8
- package/dist/_namespaces/ts.refactor.addOrRemoveBracesToArrowFunction.js +0 -2
- package/dist/_namespaces/ts.refactor.convertParamsToDestructuredObject.js +0 -2
- package/dist/_namespaces/ts.refactor.convertStringOrTemplateLiteral.js +0 -2
- package/dist/_namespaces/ts.refactor.extractSymbol.js +0 -2
- package/dist/_namespaces/ts.refactor.js +0 -10
- package/dist/_namespaces/ts.textChanges.js +0 -2
- package/dist/classifier.js +0 -715
- package/dist/codeFixProvider.js +0 -84
- package/dist/codefixes/addConvertToUnknownForNonOverlappingTypes.d.ts +0 -1
- package/dist/codefixes/addConvertToUnknownForNonOverlappingTypes.js +0 -21
- package/dist/codefixes/addEmptyExportDeclaration.js +0 -17
- package/dist/codefixes/addMissingAsync.js +0 -75
- package/dist/codefixes/addMissingAwait.js +0 -236
- package/dist/codefixes/addMissingConst.js +0 -91
- package/dist/codefixes/addNameToNamelessParameter.js +0 -29
- package/dist/codefixes/convertConstToLet.js +0 -30
- package/dist/codefixes/convertToAsyncFunction.js +0 -495
- package/dist/codefixes/convertToMappedObjectType.js +0 -48
- package/dist/codefixes/correctQualifiedNameToIndexedAccessType.js +0 -33
- package/dist/codefixes/fixAddModuleReferTypeMissingTypeof.js +0 -27
- package/dist/codefixes/fixAwaitInSyncFunction.js +0 -69
- package/dist/codefixes/fixExpectedComma.js +0 -35
- package/dist/codefixes/fixInvalidJsxCharacters.js +0 -41
- package/dist/codefixes/fixMissingCallParentheses.js +0 -42
- package/dist/codefixes/fixSpelling.js +0 -94
- package/dist/codefixes/fixUnreachableCode.js +0 -65
- package/dist/codefixes/fixUnusedIdentifier.js +0 -224
- package/dist/codefixes/helpers.js +0 -53
- package/dist/codefixes/importFixes.js +0 -441
- package/dist/codefixes/inferFromUsage.js +0 -742
- package/dist/codefixes/removeUnnecessaryAwait.js +0 -41
- package/dist/codefixes/returnValueCorrect.js +0 -132
- package/dist/codefixes/splitTypeOnlyImport.js +0 -32
- package/dist/codefixes/useBigintLiteral.js +0 -29
- package/dist/completions.js +0 -1720
- package/dist/documentHighlights.js +0 -367
- package/dist/documentRegistry.js +0 -63
- package/dist/editingServices.js +0 -4
- package/dist/findAllReferences.js +0 -1366
- package/dist/formatting/formatting.js +0 -1021
- package/dist/formatting/formattingContext.js +0 -78
- package/dist/formatting/formattingScanner.js +0 -228
- package/dist/formatting/rule.js +0 -22
- package/dist/formatting/rules.js +0 -610
- package/dist/formatting/rulesMap.js +0 -127
- package/dist/formatting/smartIndenter.js +0 -518
- package/dist/getEditsForFileRename.js +0 -86
- package/dist/goToDefinition.js +0 -246
- package/dist/importTracker.js +0 -422
- package/dist/index.js +0 -3
- package/dist/navigationBar.js +0 -549
- package/dist/organizeImports.js +0 -290
- package/dist/outliningElementsCollector.js +0 -261
- package/dist/refactorProvider.js +0 -16
- package/dist/refactors/addOrRemoveBracesToArrowFunction.js +0 -86
- package/dist/refactors/convertExport.js +0 -126
- package/dist/refactors/convertImport.js +0 -116
- package/dist/refactors/convertParamsToDestructuredObject.js +0 -282
- package/dist/refactors/convertStringOrTemplateLiteral.js +0 -148
- package/dist/refactors/extractSymbol.js +0 -1281
- package/dist/refactors/extractType.js +0 -102
- package/dist/refactors/moveToNewFile.js +0 -477
- package/dist/rename.js +0 -100
- package/dist/services.js +0 -1480
- package/dist/signatureHelp.js +0 -414
- package/dist/smartSelection.js +0 -243
- package/dist/stringCompletions.js +0 -220
- package/dist/suggestionDiagnostics.js +0 -92
- package/dist/symbolDisplay.js +0 -400
- package/dist/textChanges.js +0 -1163
- package/dist/types.js +0 -263
- package/dist/utilities.js +0 -1981
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
// Modificado de microsoft/TypeScript v3.9.10, src/services/stringCompletions.ts: o namespace virou módulo ES, com os imports explícitos.
|
|
2
|
-
import { Debug, Extension, ScriptTarget, SignatureHelp, SyntaxKind, TypeFlags, addToSeen, contains, createMap, createTextSpan, directorySeparator, emptyArray, find, flatMap, getContextualTypeFromParent, getReplacementSpanForContextToken, hasIndexSignature, isIdentifierText, isImportCall, isInString, isLiteralTypeNode, isObjectLiteralExpression, isRequireCall, isStringLiteral, isStringLiteralLike, isTypeReferenceNode, mapDefined, signatureHasRestParameter, skipConstraint, textPart, } from "./_namespaces/ts.js";
|
|
3
|
-
import { SortText, createCompletionDetails, createCompletionDetailsForSymbol, getCompletionEntriesFromSymbols, } from "./_namespaces/ts.Completions.js";
|
|
4
|
-
/* @internal */
|
|
5
|
-
export function getStringLiteralCompletions(sourceFile, position, contextToken, checker, _host, log, preferences) {
|
|
6
|
-
// Na tag, o caminho de uma referência `/// <reference path>` tinha completion; a Linguagem não tem a referência (CK-03).
|
|
7
|
-
if (isInString(sourceFile, position, contextToken)) {
|
|
8
|
-
if (!contextToken || !isStringLiteralLike(contextToken))
|
|
9
|
-
return undefined;
|
|
10
|
-
const entries = getStringLiteralCompletionEntries(sourceFile, contextToken, position, checker);
|
|
11
|
-
return convertStringLiteralCompletions(entries, contextToken, sourceFile, checker, log, preferences);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function convertStringLiteralCompletions(completion, contextToken, sourceFile, checker, log, preferences) {
|
|
15
|
-
if (completion === undefined) {
|
|
16
|
-
return undefined;
|
|
17
|
-
}
|
|
18
|
-
switch (completion.kind) {
|
|
19
|
-
case 0 /* StringLiteralCompletionKind.Paths */:
|
|
20
|
-
return convertPathCompletions(completion.paths);
|
|
21
|
-
case 1 /* StringLiteralCompletionKind.Properties */: {
|
|
22
|
-
const entries = [];
|
|
23
|
-
getCompletionEntriesFromSymbols(completion.symbols, entries, contextToken, sourceFile, sourceFile, checker, ScriptTarget.ESNext, log, 4 /* CompletionKind.String */, preferences); // Target will not be used, so arbitrary
|
|
24
|
-
return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: completion.hasIndexSignature, entries };
|
|
25
|
-
}
|
|
26
|
-
case 2 /* StringLiteralCompletionKind.Types */: {
|
|
27
|
-
const entries = completion.types.map(type => ({
|
|
28
|
-
name: type.value,
|
|
29
|
-
kindModifiers: "" /* ScriptElementKindModifier.none */,
|
|
30
|
-
kind: "string" /* ScriptElementKind.string */,
|
|
31
|
-
sortText: "0",
|
|
32
|
-
replacementSpan: getReplacementSpanForContextToken(contextToken)
|
|
33
|
-
}));
|
|
34
|
-
return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: completion.isNewIdentifier, entries };
|
|
35
|
-
}
|
|
36
|
-
default:
|
|
37
|
-
return Debug.assertNever(completion);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
export function getStringLiteralCompletionDetails(name, sourceFile, position, contextToken, checker, _host, cancellationToken) {
|
|
41
|
-
if (!contextToken || !isStringLiteralLike(contextToken))
|
|
42
|
-
return undefined;
|
|
43
|
-
const completions = getStringLiteralCompletionEntries(sourceFile, contextToken, position, checker);
|
|
44
|
-
return completions && stringLiteralCompletionDetails(name, contextToken, completions, sourceFile, checker, cancellationToken);
|
|
45
|
-
}
|
|
46
|
-
function stringLiteralCompletionDetails(name, location, completion, sourceFile, checker, cancellationToken) {
|
|
47
|
-
switch (completion.kind) {
|
|
48
|
-
case 0 /* StringLiteralCompletionKind.Paths */: {
|
|
49
|
-
const match = find(completion.paths, p => p.name === name);
|
|
50
|
-
return match && createCompletionDetails(name, kindModifiersFromExtension(match.extension), match.kind, [textPart(name)]);
|
|
51
|
-
}
|
|
52
|
-
case 1 /* StringLiteralCompletionKind.Properties */: {
|
|
53
|
-
const match = find(completion.symbols, s => s.name === name);
|
|
54
|
-
return match && createCompletionDetailsForSymbol(match, checker, sourceFile, location, cancellationToken);
|
|
55
|
-
}
|
|
56
|
-
case 2 /* StringLiteralCompletionKind.Types */:
|
|
57
|
-
return find(completion.types, t => t.value === name) ? createCompletionDetails(name, "" /* ScriptElementKindModifier.none */, "type" /* ScriptElementKind.typeElement */, [textPart(name)]) : undefined;
|
|
58
|
-
default:
|
|
59
|
-
return Debug.assertNever(completion);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
function convertPathCompletions(pathCompletions) {
|
|
63
|
-
const isGlobalCompletion = false; // We don't want the editor to offer any other completions, such as snippets, inside a comment.
|
|
64
|
-
const isNewIdentifierLocation = true; // The user may type in a path that doesn't yet exist, creating a "new identifier" with respect to the collection of identifiers the server is aware of.
|
|
65
|
-
const entries = pathCompletions.map(({ name, kind, span, extension }) => ({ name, kind, kindModifiers: kindModifiersFromExtension(extension), sortText: SortText.LocationPriority, replacementSpan: span }));
|
|
66
|
-
return { isGlobalCompletion, isMemberCompletion: false, isNewIdentifierLocation, entries };
|
|
67
|
-
}
|
|
68
|
-
function kindModifiersFromExtension(extension) {
|
|
69
|
-
switch (extension) {
|
|
70
|
-
case Extension.Dts: return ".d.ts" /* ScriptElementKindModifier.dtsModifier */;
|
|
71
|
-
case Extension.Js: return ".js" /* ScriptElementKindModifier.jsModifier */;
|
|
72
|
-
case Extension.Json: return ".json" /* ScriptElementKindModifier.jsonModifier */;
|
|
73
|
-
case Extension.Jsx: return ".jsx" /* ScriptElementKindModifier.jsxModifier */;
|
|
74
|
-
case Extension.Ts: return ".ts" /* ScriptElementKindModifier.tsModifier */;
|
|
75
|
-
case Extension.Tsx: return ".tsx" /* ScriptElementKindModifier.tsxModifier */;
|
|
76
|
-
case Extension.TsBuildInfo: return Debug.fail(`Extension ${Extension.TsBuildInfo} is unsupported.`);
|
|
77
|
-
case undefined: return "" /* ScriptElementKindModifier.none */;
|
|
78
|
-
default:
|
|
79
|
-
return Debug.assertNever(extension);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
var StringLiteralCompletionKind;
|
|
83
|
-
(function (StringLiteralCompletionKind) {
|
|
84
|
-
StringLiteralCompletionKind[StringLiteralCompletionKind["Paths"] = 0] = "Paths";
|
|
85
|
-
StringLiteralCompletionKind[StringLiteralCompletionKind["Properties"] = 1] = "Properties";
|
|
86
|
-
StringLiteralCompletionKind[StringLiteralCompletionKind["Types"] = 2] = "Types";
|
|
87
|
-
})(StringLiteralCompletionKind || (StringLiteralCompletionKind = {}));
|
|
88
|
-
function getStringLiteralCompletionEntries(sourceFile, node, position, typeChecker) {
|
|
89
|
-
const { parent } = node;
|
|
90
|
-
switch (parent.kind) {
|
|
91
|
-
case SyntaxKind.LiteralType:
|
|
92
|
-
switch (parent.parent.kind) {
|
|
93
|
-
case SyntaxKind.TypeReference:
|
|
94
|
-
return { kind: 2 /* StringLiteralCompletionKind.Types */, types: getStringLiteralTypes(typeChecker.getTypeArgumentConstraint(parent)), isNewIdentifier: false };
|
|
95
|
-
case SyntaxKind.IndexedAccessType:
|
|
96
|
-
// Get all apparent property names
|
|
97
|
-
// i.e. interface Foo {
|
|
98
|
-
// foo: string;
|
|
99
|
-
// bar: string;
|
|
100
|
-
// }
|
|
101
|
-
// let x: Foo["/*completion position*/"]
|
|
102
|
-
return stringLiteralCompletionsFromProperties(typeChecker.getTypeFromTypeNode(parent.parent.objectType));
|
|
103
|
-
case SyntaxKind.ImportType:
|
|
104
|
-
return { kind: 0 /* StringLiteralCompletionKind.Paths */, paths: getStringLiteralCompletionsFromModuleNames(sourceFile, node) };
|
|
105
|
-
case SyntaxKind.UnionType: {
|
|
106
|
-
if (!isTypeReferenceNode(parent.parent.parent))
|
|
107
|
-
return undefined;
|
|
108
|
-
const alreadyUsedTypes = getAlreadyUsedTypesInStringLiteralUnion(parent.parent, parent);
|
|
109
|
-
const types = getStringLiteralTypes(typeChecker.getTypeArgumentConstraint(parent.parent)).filter(t => !contains(alreadyUsedTypes, t.value));
|
|
110
|
-
return { kind: 2 /* StringLiteralCompletionKind.Types */, types, isNewIdentifier: false };
|
|
111
|
-
}
|
|
112
|
-
default:
|
|
113
|
-
return undefined;
|
|
114
|
-
}
|
|
115
|
-
case SyntaxKind.PropertyAssignment:
|
|
116
|
-
if (isObjectLiteralExpression(parent.parent) && parent.name === node) {
|
|
117
|
-
// Get quoted name of properties of the object literal expression
|
|
118
|
-
// i.e. interface ConfigFiles {
|
|
119
|
-
// 'jspm:dev': string
|
|
120
|
-
// }
|
|
121
|
-
// let files: ConfigFiles = {
|
|
122
|
-
// '/*completion position*/'
|
|
123
|
-
// }
|
|
124
|
-
//
|
|
125
|
-
// function foo(c: ConfigFiles) {}
|
|
126
|
-
// foo({
|
|
127
|
-
// '/*completion position*/'
|
|
128
|
-
// });
|
|
129
|
-
return stringLiteralCompletionsFromProperties(typeChecker.getContextualType(parent.parent));
|
|
130
|
-
}
|
|
131
|
-
return fromContextualType();
|
|
132
|
-
case SyntaxKind.ElementAccessExpression: {
|
|
133
|
-
const { expression, argumentExpression } = parent;
|
|
134
|
-
if (node === argumentExpression) {
|
|
135
|
-
// Get all names of properties on the expression
|
|
136
|
-
// i.e. interface A {
|
|
137
|
-
// 'prop1': string
|
|
138
|
-
// }
|
|
139
|
-
// let a: A;
|
|
140
|
-
// a['/*completion position*/']
|
|
141
|
-
return stringLiteralCompletionsFromProperties(typeChecker.getTypeAtLocation(expression));
|
|
142
|
-
}
|
|
143
|
-
return undefined;
|
|
144
|
-
}
|
|
145
|
-
case SyntaxKind.CallExpression:
|
|
146
|
-
if (!isRequireCall(parent, /*checkArgumentIsStringLiteralLike*/ false) && !isImportCall(parent)) {
|
|
147
|
-
const argumentInfo = SignatureHelp.getArgumentInfoForCompletions(node, position, sourceFile);
|
|
148
|
-
// Get string literal completions from specialized signatures of the target
|
|
149
|
-
// i.e. declare function f(a: 'A');
|
|
150
|
-
// f("/*completion position*/")
|
|
151
|
-
return argumentInfo ? getStringLiteralCompletionsFromSignature(argumentInfo, typeChecker) : fromContextualType();
|
|
152
|
-
}
|
|
153
|
-
// falls through (is `require("")` or `import("")`)
|
|
154
|
-
case SyntaxKind.ImportDeclaration:
|
|
155
|
-
case SyntaxKind.ExportDeclaration:
|
|
156
|
-
// O caminho de um módulo: import * as ns from "/*completion position*/";
|
|
157
|
-
// var y = require("/*completion position*/");
|
|
158
|
-
// export * from "/*completion position*/";
|
|
159
|
-
return { kind: 0 /* StringLiteralCompletionKind.Paths */, paths: getStringLiteralCompletionsFromModuleNames(sourceFile, node) };
|
|
160
|
-
default:
|
|
161
|
-
return fromContextualType();
|
|
162
|
-
}
|
|
163
|
-
function fromContextualType() {
|
|
164
|
-
// Get completion for string literal from string literal type
|
|
165
|
-
// i.e. var x: "hi" | "hello" = "/*completion position*/"
|
|
166
|
-
return { kind: 2 /* StringLiteralCompletionKind.Types */, types: getStringLiteralTypes(getContextualTypeFromParent(node, typeChecker)), isNewIdentifier: false };
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
function getAlreadyUsedTypesInStringLiteralUnion(union, current) {
|
|
170
|
-
return mapDefined(union.types, type => type !== current && isLiteralTypeNode(type) && isStringLiteral(type.literal) ? type.literal.text : undefined);
|
|
171
|
-
}
|
|
172
|
-
function getStringLiteralCompletionsFromSignature(argumentInfo, checker) {
|
|
173
|
-
let isNewIdentifier = false;
|
|
174
|
-
const uniques = createMap();
|
|
175
|
-
const candidates = [];
|
|
176
|
-
checker.getResolvedSignature(argumentInfo.invocation, candidates, argumentInfo.argumentCount);
|
|
177
|
-
const types = flatMap(candidates, candidate => {
|
|
178
|
-
if (!signatureHasRestParameter(candidate) && argumentInfo.argumentCount > candidate.parameters.length)
|
|
179
|
-
return;
|
|
180
|
-
const type = checker.getParameterType(candidate, argumentInfo.argumentIndex);
|
|
181
|
-
isNewIdentifier = isNewIdentifier || !!(type.flags & TypeFlags.String);
|
|
182
|
-
return getStringLiteralTypes(type, uniques);
|
|
183
|
-
});
|
|
184
|
-
return { kind: 2 /* StringLiteralCompletionKind.Types */, types, isNewIdentifier };
|
|
185
|
-
}
|
|
186
|
-
function stringLiteralCompletionsFromProperties(type) {
|
|
187
|
-
return type && {
|
|
188
|
-
kind: 1 /* StringLiteralCompletionKind.Properties */,
|
|
189
|
-
// Na tag, sem o `#private` (CK-02).
|
|
190
|
-
symbols: type.getApparentProperties(),
|
|
191
|
-
hasIndexSignature: hasIndexSignature(type)
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
function getStringLiteralTypes(type, uniques = createMap()) {
|
|
195
|
-
if (!type)
|
|
196
|
-
return emptyArray;
|
|
197
|
-
type = skipConstraint(type);
|
|
198
|
-
// Na tag, o literal de string de um membro de enum ficava de fora (CK-02).
|
|
199
|
-
return type.isUnion() ? flatMap(type.types, t => getStringLiteralTypes(t, uniques)) :
|
|
200
|
-
type.isStringLiteral() && addToSeen(uniques, type.value) ? [type] : emptyArray;
|
|
201
|
-
}
|
|
202
|
-
function addReplacementSpans(text, textStart, names) {
|
|
203
|
-
const span = getDirectoryFragmentTextSpan(text, textStart);
|
|
204
|
-
return names.map(({ name, kind, extension }) => ({ name, kind, extension, span }));
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* O completion do caminho de um módulo. Na tag, ele lia o disco do host: os diretórios relativos, o `baseUrl` e o
|
|
208
|
-
* `paths`, os módulos ambientes, os pacotes de tipos e o `node_modules`. O host da Linguagem não lê disco (CK-47), e a
|
|
209
|
-
* resolução de um import é dele (CK-46): a lista sai vazia, como a da tag com um host que não lê diretório.
|
|
210
|
-
*/
|
|
211
|
-
function getStringLiteralCompletionsFromModuleNames(sourceFile, node) {
|
|
212
|
-
return addReplacementSpans(node.text, node.getStart(sourceFile) + 1, emptyArray);
|
|
213
|
-
}
|
|
214
|
-
function getDirectoryFragmentTextSpan(text, textStart) {
|
|
215
|
-
const index = Math.max(text.lastIndexOf(directorySeparator), text.lastIndexOf("\\"));
|
|
216
|
-
const offset = index !== -1 ? index + 1 : 0;
|
|
217
|
-
// If the range is an identifier, span is unnecessary.
|
|
218
|
-
const length = text.length - offset;
|
|
219
|
-
return length === 0 || isIdentifierText(text.substr(offset, length), ScriptTarget.ESNext) ? undefined : createTextSpan(textStart + offset, length);
|
|
220
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
// Modificado de microsoft/TypeScript v3.9.10, src/services/suggestionDiagnostics.ts: o namespace virou módulo ES, com os imports explícitos.
|
|
2
|
-
import { Diagnostics, SyntaxKind, addRange, createDiagnosticForNode, createMap, forEachReturnStatement, hasPropertyAccessExpressionWithName, isAsyncFunction, isBlock, isCallExpression, isFunctionLikeDeclaration, isIdentifier, isPropertyAccessExpression, isReturnStatement, isVariableDeclaration, some, } from "./_namespaces/ts.js";
|
|
3
|
-
const visitedNestedConvertibleFunctions = createMap();
|
|
4
|
-
export function computeSuggestionDiagnostics(sourceFile, program, cancellationToken) {
|
|
5
|
-
program.getSemanticDiagnostics(sourceFile, cancellationToken);
|
|
6
|
-
const diags = [];
|
|
7
|
-
const checker = program.getTypeChecker();
|
|
8
|
-
visitedNestedConvertibleFunctions.clear();
|
|
9
|
-
check(sourceFile);
|
|
10
|
-
addRange(diags, sourceFile.bindSuggestionDiagnostics);
|
|
11
|
-
addRange(diags, program.getSuggestionDiagnostics(sourceFile, cancellationToken));
|
|
12
|
-
return diags.sort((d1, d2) => d1.start - d2.start);
|
|
13
|
-
function check(node) {
|
|
14
|
-
// Na tag, também a sugestão de converter o `const x = require("…")` de topo num import (o TS80005): o `require` é
|
|
15
|
-
// CommonJS (CK-03), e o nome não se acha na Linguagem (CK-68).
|
|
16
|
-
if (isFunctionLikeDeclaration(node)) {
|
|
17
|
-
addConvertToAsyncFunctionDiagnostics(node, checker, diags);
|
|
18
|
-
}
|
|
19
|
-
node.forEachChild(check);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
function addConvertToAsyncFunctionDiagnostics(node, checker, diags) {
|
|
23
|
-
// A função se confere antes do mapa, para os callbacks aninhados mais fundo também se conferirem.
|
|
24
|
-
if (isConvertibleFunction(node, checker) && !visitedNestedConvertibleFunctions.has(getKeyFromNode(node))) {
|
|
25
|
-
diags.push(createDiagnosticForNode(!node.name && isVariableDeclaration(node.parent) && isIdentifier(node.parent.name) ? node.parent.name : node, Diagnostics.This_may_be_converted_to_an_async_function));
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
function isConvertibleFunction(node, checker) {
|
|
29
|
-
return !isAsyncFunction(node) &&
|
|
30
|
-
node.body &&
|
|
31
|
-
isBlock(node.body) &&
|
|
32
|
-
hasReturnStatementWithPromiseHandler(node.body) &&
|
|
33
|
-
returnsPromise(node, checker);
|
|
34
|
-
}
|
|
35
|
-
function returnsPromise(node, checker) {
|
|
36
|
-
const functionType = checker.getTypeAtLocation(node);
|
|
37
|
-
const callSignatures = checker.getSignaturesOfType(functionType);
|
|
38
|
-
const returnType = callSignatures.length ? checker.getReturnTypeOfSignature(callSignatures[0]) : undefined;
|
|
39
|
-
return !!returnType && !!checker.getPromisedTypeOfPromise(returnType);
|
|
40
|
-
}
|
|
41
|
-
function hasReturnStatementWithPromiseHandler(body) {
|
|
42
|
-
return !!forEachReturnStatement(body, isReturnStatementWithFixablePromiseHandler);
|
|
43
|
-
}
|
|
44
|
-
export function isReturnStatementWithFixablePromiseHandler(node) {
|
|
45
|
-
return isReturnStatement(node) && !!node.expression && isFixablePromiseHandler(node.expression);
|
|
46
|
-
}
|
|
47
|
-
// Acompanha o `transformExpression` do code fix que converte para async (`convertToAsyncFunction.ts`).
|
|
48
|
-
export function isFixablePromiseHandler(node) {
|
|
49
|
-
// a chamada de fora existe e é um handler de promise
|
|
50
|
-
if (!isPromiseHandler(node) || !node.arguments.every(isFixablePromiseArgument)) {
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
// e cada chamada encadeada é válida
|
|
54
|
-
let currentNode = node.expression;
|
|
55
|
-
while (isPromiseHandler(currentNode) || isPropertyAccessExpression(currentNode)) {
|
|
56
|
-
if (isCallExpression(currentNode) && !currentNode.arguments.every(isFixablePromiseArgument)) {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
currentNode = currentNode.expression;
|
|
60
|
-
}
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
function isPromiseHandler(node) {
|
|
64
|
-
return isCallExpression(node) && (hasPropertyAccessExpressionWithName(node, "then") && hasSupportedNumberOfArguments(node) ||
|
|
65
|
-
hasPropertyAccessExpressionWithName(node, "catch"));
|
|
66
|
-
}
|
|
67
|
-
function hasSupportedNumberOfArguments(node) {
|
|
68
|
-
if (node.arguments.length > 2)
|
|
69
|
-
return false;
|
|
70
|
-
if (node.arguments.length < 2)
|
|
71
|
-
return true;
|
|
72
|
-
return some(node.arguments, arg => {
|
|
73
|
-
return arg.kind === SyntaxKind.NullKeyword ||
|
|
74
|
-
isIdentifier(arg) && arg.text === "undefined";
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
// Acompanha o `getTransformationBody` do code fix que converte para async (`convertToAsyncFunction.ts`).
|
|
78
|
-
function isFixablePromiseArgument(arg) {
|
|
79
|
-
switch (arg.kind) {
|
|
80
|
-
case SyntaxKind.ArrowFunction:
|
|
81
|
-
visitedNestedConvertibleFunctions.set(getKeyFromNode(arg), true);
|
|
82
|
-
// falls through
|
|
83
|
-
case SyntaxKind.NullKeyword:
|
|
84
|
-
case SyntaxKind.Identifier: // o `undefined` é um identificador
|
|
85
|
-
return true;
|
|
86
|
-
default:
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
function getKeyFromNode(exp) {
|
|
91
|
-
return `${exp.pos.toString()}:${exp.end.toString()}`;
|
|
92
|
-
}
|