From 89dda35e0787c1ebbd1e777f1d928e2cf10b59c9 Mon Sep 17 00:00:00 2001 From: Evgeniy Okolnov Date: Wed, 25 Oct 2023 16:58:41 +0300 Subject: [PATCH] [ArkTS Linter] Relax 'identifier-as-property-name' report for anonymous types from interop. Change-Id: I25b10f026bac0e585f2efbdbd890c952637463f3 Signed-off-by: Evgeniy Okolnov --- linter-4.2/src/TypeScriptLinter.ts | 18 +++++++----------- linter-4.2/test/dynamic_lib.d.ts | 4 ++++ linter-4.2/test/dynamic_object_literals.ts | 14 ++++++++++++-- linter/src/TypeScriptLinter.ts | 10 ++++++---- linter/test/dynamic_lib.d.ts | 4 ++++ linter/test/dynamic_object_literals.ts | 14 ++++++++++++-- 6 files changed, 45 insertions(+), 19 deletions(-) diff --git a/linter-4.2/src/TypeScriptLinter.ts b/linter-4.2/src/TypeScriptLinter.ts index 27442da9f..5651cbcfb 100644 --- a/linter-4.2/src/TypeScriptLinter.ts +++ b/linter-4.2/src/TypeScriptLinter.ts @@ -693,20 +693,16 @@ export class TypeScriptLinter { ) { // We can use literals as property names only when creating Record or any interop instances. let isRecordObjectInitializer = false; - let isDynamicLiteralInitializer = false; + let isDynamic = false; if (ts.isPropertyAssignment(node)) { - let objectLiteralType = this.tsTypeChecker.getContextualType( - node.parent - ); - isRecordObjectInitializer = - !!objectLiteralType && - this.tsUtils.isStdRecordType(objectLiteralType); - isDynamicLiteralInitializer = this.tsUtils.isDynamicLiteralInitializer( - node.parent - ); + let objectLiteralType = this.tsTypeChecker.getContextualType(node.parent); + if (objectLiteralType) { + isRecordObjectInitializer = this.tsUtils.isStdRecordType(objectLiteralType); + isDynamic = this.tsUtils.isLibraryType(objectLiteralType) || this.tsUtils.isDynamicLiteralInitializer(node.parent); + } } - if (!isRecordObjectInitializer && !isDynamicLiteralInitializer) { + if (!isRecordObjectInitializer && !isDynamic) { let autofix: Autofix[] | undefined = Autofixer.fixLiteralAsPropertyName(node); let autofixable = autofix != undefined; diff --git a/linter-4.2/test/dynamic_lib.d.ts b/linter-4.2/test/dynamic_lib.d.ts index e3da589b5..843931397 100644 --- a/linter-4.2/test/dynamic_lib.d.ts +++ b/linter-4.2/test/dynamic_lib.d.ts @@ -93,3 +93,7 @@ declare class B { } export declare function bad_func(): A & B; + +export type IndexedSignatureType = { + [key: string]: string; +} \ No newline at end of file diff --git a/linter-4.2/test/dynamic_object_literals.ts b/linter-4.2/test/dynamic_object_literals.ts index ea7f9759a..fae9413ea 100644 --- a/linter-4.2/test/dynamic_object_literals.ts +++ b/linter-4.2/test/dynamic_object_literals.ts @@ -24,7 +24,8 @@ import { dynamic_array, padding, margin, - position + position, + IndexedSignatureType } from "./dynamic_lib" function main(): void { @@ -84,4 +85,13 @@ dynamic_array.splice(2, 0, {a: 1, b: '2'}); // #13550 - allow literals as property names in dynamic context padding({'top': '0px', 'right': '5px', 'bottom': '10px', 'left': '15px'}); margin({'top': '10px', 'right': '20px', 'bottom': '30px', 'left': '40px'}); -position({'x': '20', 'y': '40'}); \ No newline at end of file +position({'x': '20', 'y': '40'}); + +// allow literal as property name for type aliases that come from interop +function typeAliasLitAsPropName(): IndexedSignatureType { + return { + 'a': '1', + 'b': '2', + 'c': '3' + } +} \ No newline at end of file diff --git a/linter/src/TypeScriptLinter.ts b/linter/src/TypeScriptLinter.ts index 15d018ed5..039e904b8 100644 --- a/linter/src/TypeScriptLinter.ts +++ b/linter/src/TypeScriptLinter.ts @@ -596,14 +596,16 @@ export class TypeScriptLinter { if (propName && (propName.kind === ts.SyntaxKind.NumericLiteral || propName.kind === ts.SyntaxKind.StringLiteral)) { // We can use literals as property names only when creating Record or any interop instances. let isRecordObjectInitializer = false; - let isDynamicLiteralInitializer = false; + let isDynamic = false; if (ts.isPropertyAssignment(node)) { let objectLiteralType = this.tsTypeChecker.getContextualType(node.parent); - isRecordObjectInitializer = !!objectLiteralType && this.tsUtils.isStdRecordType(objectLiteralType); - isDynamicLiteralInitializer = this.tsUtils.isDynamicLiteralInitializer(node.parent); + if (objectLiteralType) { + isRecordObjectInitializer = this.tsUtils.isStdRecordType(objectLiteralType); + isDynamic = this.tsUtils.isLibraryType(objectLiteralType) || this.tsUtils.isDynamicLiteralInitializer(node.parent); + } } - if (!isRecordObjectInitializer && !isDynamicLiteralInitializer) { + if (!isRecordObjectInitializer && !isDynamic) { let autofix : Autofix[] | undefined = Autofixer.fixLiteralAsPropertyName(node); let autofixable = autofix != undefined; if (!this.autofixesInfo.shouldAutofix(node, FaultID.LiteralAsPropertyName)) { diff --git a/linter/test/dynamic_lib.d.ts b/linter/test/dynamic_lib.d.ts index e3da589b5..843931397 100644 --- a/linter/test/dynamic_lib.d.ts +++ b/linter/test/dynamic_lib.d.ts @@ -93,3 +93,7 @@ declare class B { } export declare function bad_func(): A & B; + +export type IndexedSignatureType = { + [key: string]: string; +} \ No newline at end of file diff --git a/linter/test/dynamic_object_literals.ts b/linter/test/dynamic_object_literals.ts index ea7f9759a..fae9413ea 100644 --- a/linter/test/dynamic_object_literals.ts +++ b/linter/test/dynamic_object_literals.ts @@ -24,7 +24,8 @@ import { dynamic_array, padding, margin, - position + position, + IndexedSignatureType } from "./dynamic_lib" function main(): void { @@ -84,4 +85,13 @@ dynamic_array.splice(2, 0, {a: 1, b: '2'}); // #13550 - allow literals as property names in dynamic context padding({'top': '0px', 'right': '5px', 'bottom': '10px', 'left': '15px'}); margin({'top': '10px', 'right': '20px', 'bottom': '30px', 'left': '40px'}); -position({'x': '20', 'y': '40'}); \ No newline at end of file +position({'x': '20', 'y': '40'}); + +// allow literal as property name for type aliases that come from interop +function typeAliasLitAsPropName(): IndexedSignatureType { + return { + 'a': '1', + 'b': '2', + 'c': '3' + } +} \ No newline at end of file -- Gitee