-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Spelling correction #15507
Merged
Merged
Spelling correction #15507
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2345ecd
Suggest spelling for unknown symbols + properties
sandersn 1bff5b7
Codefix:suggest spellings+update others w/new codes
sandersn 2479c07
Update baselines
sandersn ca7d7f5
Only 100 spelling corrections for unknown symbols
sandersn eb33ba7
Fix semicolon lint
sandersn ee1edf0
Lower allowed levenshtein distance for suggestions
sandersn 4877343
Reduce max number of spelling suggestions to 10
sandersn 9bf5209
Lower allowed length difference for suggestions
sandersn 2a7398b
Include primitives in type-as-value error message
sandersn 5a7e967
Blacklist some built-ins and improve max cutoff
sandersn 0c10098
Fix case of suggestion blacklist.
sandersn 9eaf40b
Merge branch 'master' into spelling-correction
sandersn 370b561
Merge branch 'master' into spelling-correction
sandersn 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
registerCodeFix({ | ||
errorCodes: [Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2.code, | ||
Diagnostics.Cannot_find_name_0_Did_you_mean_1.code], | ||
getCodeActions: getActionsForCorrectSpelling | ||
}); | ||
|
||
function getActionsForCorrectSpelling(context: CodeFixContext): CodeAction[] | undefined { | ||
const sourceFile = context.sourceFile; | ||
|
||
// This is the identifier of the misspelled word. eg: | ||
// this.speling = 1; | ||
// ^^^^^^^ | ||
const node = getTokenAtPosition(sourceFile, context.span.start); | ||
const checker = context.program.getTypeChecker(); | ||
let suggestion: string; | ||
if (node.kind === SyntaxKind.Identifier && isPropertyAccessExpression(node.parent)) { | ||
const containingType = checker.getTypeAtLocation(node.parent.expression); | ||
suggestion = checker.getSuggestionForNonexistentProperty(node as Identifier, containingType); | ||
} | ||
else { | ||
const meaning = getMeaningFromLocation(node); | ||
suggestion = checker.getSuggestionForNonexistentSymbol(node, getTextOfNode(node), convertSemanticMeaningToSymbolFlags(meaning)); | ||
} | ||
if (suggestion) { | ||
return [{ | ||
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Change_spelling_to_0), [suggestion]), | ||
changes: [{ | ||
fileName: sourceFile.fileName, | ||
textChanges: [{ | ||
span: { start: node.getStart(), length: node.getWidth() }, | ||
newText: suggestion | ||
}], | ||
}], | ||
}]; | ||
} | ||
} | ||
|
||
function convertSemanticMeaningToSymbolFlags(meaning: SemanticMeaning): SymbolFlags { | ||
let flags = 0; | ||
if (meaning & SemanticMeaning.Namespace) { | ||
flags |= SymbolFlags.Namespace; | ||
} | ||
if (meaning & SemanticMeaning.Type) { | ||
flags |= SymbolFlags.Type; | ||
} | ||
if (meaning & SemanticMeaning.Value) { | ||
flags |= SymbolFlags.Value; | ||
} | ||
return flags; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
tests/cases/compiler/badArrayIndex.ts(1,15): error TS2304: Cannot find name 'number'. | ||
tests/cases/compiler/badArrayIndex.ts(1,15): error TS2693: 'number' only refers to a type, but is being used as a value here. | ||
tests/cases/compiler/badArrayIndex.ts(1,22): error TS1109: Expression expected. | ||
|
||
|
||
==== tests/cases/compiler/badArrayIndex.ts (2 errors) ==== | ||
var results = number[]; | ||
~~~~~~ | ||
!!! error TS2304: Cannot find name 'number'. | ||
!!! error TS2693: 'number' only refers to a type, but is being used as a value here. | ||
~ | ||
!!! error TS1109: Expression expected. |
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
4 changes: 2 additions & 2 deletions
4
tests/baselines/reference/cannotInvokeNewOnIndexExpression.errors.txt
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts(1,23): error TS2304: Cannot find name 'any'. | ||
tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts(1,23): error TS2693: 'any' only refers to a type, but is being used as a value here. | ||
|
||
|
||
==== tests/cases/compiler/cannotInvokeNewOnIndexExpression.ts (1 errors) ==== | ||
var test: any[] = new any[1]; | ||
~~~ | ||
!!! error TS2304: Cannot find name 'any'. | ||
!!! error TS2693: 'any' only refers to a type, but is being used as a value here. |
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
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.
Did you intend for
levenshtein
to be in the public TypeScript API? This file has two top-levelts
namespaces, the first marked with/* @internal */
and the second without the marker. This code is in the second namespace currently.