mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Add support for pedantic property access (#40171)
* Add support for pedantic property access * accept baseline * Update diag message * Avoid pedantic Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
This commit is contained in:
co-authored by
Nathan Shively-Sanders
parent
056d01afda
commit
ce8d702586
@@ -25865,6 +25865,9 @@ namespace ts {
|
||||
}
|
||||
|
||||
propType = (compilerOptions.noUncheckedIndexedAccess && !isAssignmentTarget(node)) ? getUnionType([indexInfo.type, undefinedType]) : indexInfo.type;
|
||||
if (compilerOptions.noPropertyAccessFromIndexSignature && isPropertyAccessExpression(node)) {
|
||||
error(node, Diagnostics.Property_0_comes_from_an_index_signature_so_it_must_be_accessed_with_0, unescapeLeadingUnderscores(right.escapedText));
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (getDeclarationNodeFlagsFromSymbol(prop) & NodeFlags.Deprecated && isUncalledFunctionReference(node, prop)) {
|
||||
|
||||
@@ -635,6 +635,15 @@ namespace ts {
|
||||
category: Diagnostics.Additional_Checks,
|
||||
description: Diagnostics.Include_undefined_in_index_signature_results
|
||||
},
|
||||
{
|
||||
name: "noPropertyAccessFromIndexSignature",
|
||||
type: "boolean",
|
||||
affectsBindDiagnostics: true,
|
||||
affectsSemanticDiagnostics: true,
|
||||
showInSimplifiedHelpView: false,
|
||||
category: Diagnostics.Additional_Checks,
|
||||
description: Diagnostics.Require_undeclared_properties_from_index_signatures_to_use_element_accesses
|
||||
},
|
||||
|
||||
// Module Resolution
|
||||
{
|
||||
|
||||
@@ -3428,6 +3428,10 @@
|
||||
"category": "Error",
|
||||
"code": 4110
|
||||
},
|
||||
"Property '{0}' comes from an index signature, so it must be accessed with ['{0}'].": {
|
||||
"category": "Error",
|
||||
"code": 4111
|
||||
},
|
||||
|
||||
"The current host does not support the '{0}' option.": {
|
||||
"category": "Error",
|
||||
@@ -4744,6 +4748,10 @@
|
||||
"category": "Error",
|
||||
"code": 6504
|
||||
},
|
||||
"Require undeclared properties from index signatures to use element accesses.": {
|
||||
"category": "Error",
|
||||
"code": 6803
|
||||
},
|
||||
|
||||
"Include 'undefined' in index signature results": {
|
||||
"category": "Message",
|
||||
@@ -5959,10 +5967,18 @@
|
||||
"category": "Message",
|
||||
"code": 95144
|
||||
},
|
||||
"Delete all unused imports": {
|
||||
"Use element access for '{0}'": {
|
||||
"category": "Message",
|
||||
"code": 95145
|
||||
},
|
||||
"Use element access for all undeclared properties.": {
|
||||
"category": "Message",
|
||||
"code": 95146
|
||||
},
|
||||
"Delete all unused imports": {
|
||||
"category": "Message",
|
||||
"code": 95147
|
||||
},
|
||||
|
||||
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
|
||||
"category": "Error",
|
||||
|
||||
@@ -5784,6 +5784,7 @@ namespace ts {
|
||||
noUnusedLocals?: boolean;
|
||||
noUnusedParameters?: boolean;
|
||||
noImplicitUseStrict?: boolean;
|
||||
noPropertyAccessFromIndexSignature?: boolean;
|
||||
assumeChangesOnlyAffectDirectDependencies?: boolean;
|
||||
noLib?: boolean;
|
||||
noResolve?: boolean;
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/* @internal */
|
||||
namespace ts.codefix {
|
||||
const fixId = "fixNoPropertyAccessFromIndexSignature";
|
||||
const errorCodes = [
|
||||
Diagnostics.Property_0_comes_from_an_index_signature_so_it_must_be_accessed_with_0.code
|
||||
];
|
||||
|
||||
registerCodeFix({
|
||||
errorCodes,
|
||||
fixIds: [fixId],
|
||||
getCodeActions(context) {
|
||||
const { sourceFile, span } = context;
|
||||
const property = getPropertyAccessExpression(sourceFile, span.start);
|
||||
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, property));
|
||||
return [createCodeFixAction(fixId, changes, [Diagnostics.Use_element_access_for_0, property.name.text], fixId, Diagnostics.Use_element_access_for_all_undeclared_properties)];
|
||||
},
|
||||
getAllCodeActions: context =>
|
||||
codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, getPropertyAccessExpression(diag.file, diag.start)))
|
||||
});
|
||||
|
||||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: PropertyAccessExpression): void {
|
||||
const argumentsExpression = factory.createStringLiteral(node.name.text);
|
||||
changes.replaceNode(
|
||||
sourceFile,
|
||||
node,
|
||||
isPropertyAccessChain(node) ?
|
||||
factory.createElementAccessChain(node.expression, node.questionDotToken, argumentsExpression) :
|
||||
factory.createElementAccessExpression(node.expression, argumentsExpression)
|
||||
);
|
||||
}
|
||||
|
||||
function getPropertyAccessExpression(sourceFile: SourceFile, pos: number): PropertyAccessExpression {
|
||||
return cast(getTokenAtPosition(sourceFile, pos).parent, isPropertyAccessExpression);
|
||||
}
|
||||
}
|
||||
@@ -65,6 +65,7 @@
|
||||
"codefixes/convertLiteralTypeToMappedType.ts",
|
||||
"codefixes/fixClassIncorrectlyImplementsInterface.ts",
|
||||
"codefixes/importFixes.ts",
|
||||
"codefixes/fixNoPropertyAccessFromIndexSignature.ts",
|
||||
"codefixes/fixImplicitThis.ts",
|
||||
"codefixes/fixIncorrectNamedTupleSyntax.ts",
|
||||
"codefixes/fixSpelling.ts",
|
||||
|
||||
Reference in New Issue
Block a user