-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support naming tuple members (#38234)
* Initial draft of named tuple members * Show tuple labels and documentation in completions * Swap allowed syntax to parameter-like * Add quickfix for labeled tuple syntax mistakes * Add refactoring to convert list of signatures to single overload * Fix small bug in visitor verification * Signature help for rest parameters which are unions of tuples are displayed as seperate entries now * Expand sanity check test cases in conformance suite * Add tests and code for preserving tuple names through spreads where possible * More refactoring tests, some comment preservation and some fixed formatting of multiline tuples * Handle missing parameter named in isValidDeclarationForTupleLabel * Minor text fixes
- Loading branch information
Showing
62 changed files
with
2,176 additions
and
489 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const fixId = "fixIncorrectNamedTupleSyntax"; | ||
const errorCodes = [ | ||
Diagnostics.A_labeled_tuple_element_is_declared_as_optional_with_a_question_mark_after_the_name_and_before_the_colon_rather_than_after_the_type.code, | ||
Diagnostics.A_labeled_tuple_element_is_declared_as_rest_with_a_before_the_name_rather_than_before_the_type.code | ||
]; | ||
|
||
registerCodeFix({ | ||
errorCodes, | ||
getCodeActions: context => { | ||
const { sourceFile, span } = context; | ||
const namedTupleMember = getNamedTupleMember(sourceFile, span.start); | ||
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, namedTupleMember)); | ||
return [createCodeFixAction(fixId, changes, Diagnostics.Move_labeled_tuple_element_modifiers_to_labels, fixId, Diagnostics.Move_labeled_tuple_element_modifiers_to_labels)]; | ||
}, | ||
fixIds: [fixId] | ||
}); | ||
|
||
function getNamedTupleMember(sourceFile: SourceFile, pos: number) { | ||
const token = getTokenAtPosition(sourceFile, pos); | ||
return findAncestor(token, t => t.kind === SyntaxKind.NamedTupleMember) as NamedTupleMember | undefined; | ||
} | ||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, namedTupleMember?: NamedTupleMember) { | ||
if (!namedTupleMember) { | ||
return; | ||
} | ||
let unwrappedType = namedTupleMember.type; | ||
let sawOptional = false; | ||
let sawRest = false; | ||
while (unwrappedType.kind === SyntaxKind.OptionalType || unwrappedType.kind === SyntaxKind.RestType || unwrappedType.kind === SyntaxKind.ParenthesizedType) { | ||
if (unwrappedType.kind === SyntaxKind.OptionalType) { | ||
sawOptional = true; | ||
} | ||
else if (unwrappedType.kind === SyntaxKind.RestType) { | ||
sawRest = true; | ||
} | ||
unwrappedType = (unwrappedType as OptionalTypeNode | RestTypeNode | ParenthesizedTypeNode).type; | ||
} | ||
const updated = updateNamedTupleMember( | ||
namedTupleMember, | ||
namedTupleMember.dotDotDotToken || (sawRest ? createToken(SyntaxKind.DotDotDotToken) : undefined), | ||
namedTupleMember.name, | ||
namedTupleMember.questionToken || (sawOptional ? createToken(SyntaxKind.QuestionToken) : undefined), | ||
unwrappedType | ||
); | ||
if (updated === namedTupleMember) { | ||
return; | ||
} | ||
changes.replaceNode(sourceFile, namedTupleMember, updated); | ||
} | ||
} |
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.