-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exhaustive case completion for switch statements #50996
Merged
Merged
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
f6c3566
fix services' type's isLiteral
gabritto 4467330
update literal completions tests
gabritto a206fe1
initial prototype
gabritto 73c1eea
use symbol to expression. TODO: filter existing, replace import nodes
gabritto 5648cba
WIP
gabritto d46c0d2
WIP
gabritto 297f892
remove booleans from literals
gabritto fd1d6ed
Merge branch 'gabritto/servicesIsLiteral' into gabritto/switchsnippet
gabritto 4c528b3
trigger at case keyword positions
gabritto 1a5cd05
clean up tests
gabritto ee42732
fix element access expression case
gabritto 1819d0b
refactor dealing with existing values into a tracker
gabritto b19543e
Merge branch 'main' into gabritto/switchsnippet
gabritto bd5b817
fix merge errors
gabritto f02122b
cleanup and more tests
gabritto a35bc4a
fix lint errors
gabritto 83b88f7
more merge conflict fixes and cleanup
gabritto 599fb30
use appropriate quotes
gabritto 97dcf69
small indentation fix
gabritto 3b92638
refactor case clause tracker
gabritto 89f6f6b
Merge branch 'main' into gabritto/switchsnippet
gabritto 1894d2e
experiment: support tabstops after each case clause
gabritto 90767fc
address small CR comments
gabritto d1c8968
fix completion entry details; add test case
gabritto fb15ba1
Merge branch 'main' into gabritto/switchsnippet
gabritto 3980b93
fix lint errors
gabritto 8823108
remove space before tab stops; refactor
gabritto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ import { | |
createDiagnosticForFileFromMessageChain, createDiagnosticForNode, createDiagnosticForNodeArray, | ||
createDiagnosticForNodeFromMessageChain, createDiagnosticMessageChainFromDiagnostic, createEmptyExports, | ||
createFileDiagnostic, createGetCanonicalFileName, createGetSymbolWalker, createPrinter, | ||
createPropertyNameNodeForIdentifierOrLiteral, createScanner, createSymbolTable, createTextWriter, | ||
createPropertyNameNodeForIdentifierOrLiteral, createSymbolTable, createTextWriter, | ||
createUnderscoreEscapedMultiMap, Debug, Declaration, DeclarationName, declarationNameToString, DeclarationStatement, | ||
DeclarationWithTypeParameterChildren, DeclarationWithTypeParameters, Decorator, deduplicate, DefaultClause, | ||
defaultMaximumTruncationLength, DeferredTypeReference, DeleteExpression, Diagnostic, DiagnosticCategory, | ||
|
@@ -101,7 +101,7 @@ import { | |
isFunctionExpressionOrArrowFunction, isFunctionLike, isFunctionLikeDeclaration, | ||
isFunctionLikeOrClassStaticBlockDeclaration, isFunctionOrModuleBlock, isFunctionTypeNode, isGeneratedIdentifier, | ||
isGetAccessor, isGetAccessorDeclaration, isGetOrSetAccessorDeclaration, isGlobalScopeAugmentation, isHeritageClause, | ||
isIdentifier, isIdentifierStart, isIdentifierText, isIdentifierTypePredicate, isIdentifierTypeReference, | ||
isIdentifier, isIdentifierText, isIdentifierTypePredicate, isIdentifierTypeReference, | ||
isIfStatement, isImportCall, isImportClause, isImportDeclaration, isImportEqualsDeclaration, isImportKeyword, | ||
isImportOrExportSpecifier, isImportSpecifier, isImportTypeNode, isIndexedAccessTypeNode, isInExpressionContext, | ||
isInfinityOrNaNString, isInJSDoc, isInJSFile, isInJsonFile, isInterfaceDeclaration, | ||
|
@@ -195,7 +195,7 @@ import { | |
usingSingleLineStringWriter, VariableDeclaration, VariableDeclarationList, VariableLikeDeclaration, | ||
VariableStatement, VarianceFlags, visitEachChild, visitNode, visitNodes, Visitor, VisitResult, VoidExpression, | ||
walkUpBindingElementsAndPatterns, walkUpParenthesizedExpressions, walkUpParenthesizedTypes, | ||
walkUpParenthesizedTypesAndGetParentAndChild, WhileStatement, WideningContext, WithStatement, YieldExpression, ResolutionMode, | ||
walkUpParenthesizedTypesAndGetParentAndChild, WhileStatement, WideningContext, WithStatement, YieldExpression, ResolutionMode, canUsePropertyAccess, parseValidBigInt, isValidBigIntString, | ||
} from "./_namespaces/ts"; | ||
import * as performance from "./_namespaces/ts.performance"; | ||
import * as moduleSpecifiers from "./_namespaces/ts.moduleSpecifiers"; | ||
|
@@ -6881,10 +6881,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
if (isSingleOrDoubleQuote(firstChar) && some(symbol.declarations, hasNonGlobalAugmentationExternalModuleSymbol)) { | ||
return factory.createStringLiteral(getSpecifierForModuleSymbol(symbol, context)); | ||
} | ||
const canUsePropertyAccess = firstChar === CharacterCodes.hash ? | ||
symbolName.length > 1 && isIdentifierStart(symbolName.charCodeAt(1), languageVersion) : | ||
isIdentifierStart(firstChar, languageVersion); | ||
if (index === 0 || canUsePropertyAccess) { | ||
if (index === 0 || canUsePropertyAccess(symbolName, languageVersion)) { | ||
const identifier = setEmitFlags(factory.createIdentifier(symbolName, typeParameterNodes), EmitFlags.NoAsciiEscaping); | ||
identifier.symbol = symbol; | ||
|
||
|
@@ -22698,35 +22695,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
* @param text a valid bigint string excluding a trailing `n`, but including a possible prefix `-`. Use `isValidBigIntString(text, roundTripOnly)` before calling this function. | ||
*/ | ||
function parseBigIntLiteralType(text: string) { | ||
const negative = text.startsWith("-"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this into a new function in utilities. |
||
const base10Value = parsePseudoBigInt(`${negative ? text.slice(1) : text}n`); | ||
return getBigIntLiteralType({ negative, base10Value }); | ||
} | ||
|
||
/** | ||
* Tests whether the provided string can be parsed as a bigint. | ||
* @param s The string to test. | ||
* @param roundTripOnly Indicates the resulting bigint matches the input when converted back to a string. | ||
*/ | ||
function isValidBigIntString(s: string, roundTripOnly: boolean): boolean { | ||
if (s === "") return false; | ||
const scanner = createScanner(ScriptTarget.ESNext, /*skipTrivia*/ false); | ||
let success = true; | ||
scanner.setOnError(() => success = false); | ||
scanner.setText(s + "n"); | ||
let result = scanner.scan(); | ||
const negative = result === SyntaxKind.MinusToken; | ||
if (negative) { | ||
result = scanner.scan(); | ||
} | ||
const flags = scanner.getTokenFlags(); | ||
// validate that | ||
// * scanning proceeded without error | ||
// * a bigint can be scanned, and that when it is scanned, it is | ||
// * the full length of the input string (so the scanner is one character beyond the augmented input length) | ||
// * it does not contain a numeric seperator (the `BigInt` constructor does not accept a numeric seperator in its input) | ||
return success && result === SyntaxKind.BigIntLiteral && scanner.getTextPos() === (s.length + 1) && !(flags & TokenFlags.ContainsSeparator) | ||
&& (!roundTripOnly || s === pseudoBigIntToString({ negative, base10Value: parsePseudoBigInt(scanner.getTokenValue()) })); | ||
return getBigIntLiteralType(parseValidBigInt(text)); | ||
} | ||
|
||
function isMemberOfStringMapping(source: Type, target: Type): boolean { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also moved this into a new function in utilities.