From f91082d9828f695d90266d9b754b7fbe74fcf385 Mon Sep 17 00:00:00 2001 From: lizhonghan Date: Mon, 4 Mar 2024 20:37:59 +0800 Subject: [PATCH] Fix ArkTSLinter readonly array bug Issue: https://gitee.com/openharmony/third_party_typescript/issues/I95QK4 Test: tsc tests bugfix: 1. Fix the problem of error when readonly is used as function modification internal: #15966 Change-Id: Ide8cbf598d6e6fc0454eea53d70075e13d4bd581 Signed-off-by: lizhonghan --- lib/tsserver.js | 8 +++++++- lib/tsserverlibrary.d.ts | 1 + lib/tsserverlibrary.js | 8 +++++++- lib/typescript.d.ts | 1 + lib/typescript.js | 8 +++++++- lib/typescriptServices.d.ts | 1 + lib/typescriptServices.js | 8 +++++++- src/linter/ArkTSLinter_1_1/Utils.ts | 9 ++++++++- tests/baselines/reference/api/tsserverlibrary.d.ts | 1 + tests/baselines/reference/api/typescript.d.ts | 1 + 10 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/tsserver.js b/lib/tsserver.js index ed96270644..3a47948e5b 100644 --- a/lib/tsserver.js +++ b/lib/tsserver.js @@ -193409,6 +193409,12 @@ var ts; ((_c = tsType.getSymbol()) === null || _c === void 0 ? void 0 : _c.getName()) === "Array"); } Utils.isGenericArrayType = isGenericArrayType; + function isReadonlyArrayType(tsType) { + var _a, _b, _c; + return (isTypeReference(tsType) && ((_a = tsType.typeArguments) === null || _a === void 0 ? void 0 : _a.length) === 1 && ((_b = tsType.target.typeParameters) === null || _b === void 0 ? void 0 : _b.length) === 1 && + (((_c = tsType.getSymbol()) === null || _c === void 0 ? void 0 : _c.getName()) === "ReadonlyArray")); + } + Utils.isReadonlyArrayType = isReadonlyArrayType; function isTypedArray(tsType) { var symbol = tsType.symbol; if (!symbol) { @@ -193419,7 +193425,7 @@ var ts; } Utils.isTypedArray = isTypedArray; function isArray(tsType) { - return isGenericArrayType(tsType) || isTypedArray(tsType); + return isGenericArrayType(tsType) || isReadonlyArrayType(tsType) || isTypedArray(tsType); } Utils.isArray = isArray; function isTuple(tsType) { diff --git a/lib/tsserverlibrary.d.ts b/lib/tsserverlibrary.d.ts index f03de1feab..5a9f47a265 100644 --- a/lib/tsserverlibrary.d.ts +++ b/lib/tsserverlibrary.d.ts @@ -13196,6 +13196,7 @@ declare namespace ts { function isPrimitiveType(type: Type): boolean; function isTypeSymbol(symbol: Symbol | undefined): boolean; function isGenericArrayType(tsType: Type): tsType is TypeReference; + function isReadonlyArrayType(tsType: Type): boolean; function isTypedArray(tsType: ts.Type): boolean; function isArray(tsType: ts.Type): boolean; function isTuple(tsType: ts.Type): boolean; diff --git a/lib/tsserverlibrary.js b/lib/tsserverlibrary.js index 40f51262f1..4c93cdcf9e 100644 --- a/lib/tsserverlibrary.js +++ b/lib/tsserverlibrary.js @@ -193154,6 +193154,12 @@ var ts; ((_c = tsType.getSymbol()) === null || _c === void 0 ? void 0 : _c.getName()) === "Array"); } Utils.isGenericArrayType = isGenericArrayType; + function isReadonlyArrayType(tsType) { + var _a, _b, _c; + return (isTypeReference(tsType) && ((_a = tsType.typeArguments) === null || _a === void 0 ? void 0 : _a.length) === 1 && ((_b = tsType.target.typeParameters) === null || _b === void 0 ? void 0 : _b.length) === 1 && + (((_c = tsType.getSymbol()) === null || _c === void 0 ? void 0 : _c.getName()) === "ReadonlyArray")); + } + Utils.isReadonlyArrayType = isReadonlyArrayType; function isTypedArray(tsType) { var symbol = tsType.symbol; if (!symbol) { @@ -193164,7 +193170,7 @@ var ts; } Utils.isTypedArray = isTypedArray; function isArray(tsType) { - return isGenericArrayType(tsType) || isTypedArray(tsType); + return isGenericArrayType(tsType) || isReadonlyArrayType(tsType) || isTypedArray(tsType); } Utils.isArray = isArray; function isTuple(tsType) { diff --git a/lib/typescript.d.ts b/lib/typescript.d.ts index 7b56d718a9..0d7b684d4f 100644 --- a/lib/typescript.d.ts +++ b/lib/typescript.d.ts @@ -9257,6 +9257,7 @@ declare namespace ts { function isPrimitiveType(type: Type): boolean; function isTypeSymbol(symbol: Symbol | undefined): boolean; function isGenericArrayType(tsType: Type): tsType is TypeReference; + function isReadonlyArrayType(tsType: Type): boolean; function isTypedArray(tsType: ts.Type): boolean; function isArray(tsType: ts.Type): boolean; function isTuple(tsType: ts.Type): boolean; diff --git a/lib/typescript.js b/lib/typescript.js index 5b649acc61..c4312b0d96 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -182248,6 +182248,12 @@ var ts; ((_c = tsType.getSymbol()) === null || _c === void 0 ? void 0 : _c.getName()) === "Array"); } Utils.isGenericArrayType = isGenericArrayType; + function isReadonlyArrayType(tsType) { + var _a, _b, _c; + return (isTypeReference(tsType) && ((_a = tsType.typeArguments) === null || _a === void 0 ? void 0 : _a.length) === 1 && ((_b = tsType.target.typeParameters) === null || _b === void 0 ? void 0 : _b.length) === 1 && + (((_c = tsType.getSymbol()) === null || _c === void 0 ? void 0 : _c.getName()) === "ReadonlyArray")); + } + Utils.isReadonlyArrayType = isReadonlyArrayType; function isTypedArray(tsType) { var symbol = tsType.symbol; if (!symbol) { @@ -182258,7 +182264,7 @@ var ts; } Utils.isTypedArray = isTypedArray; function isArray(tsType) { - return isGenericArrayType(tsType) || isTypedArray(tsType); + return isGenericArrayType(tsType) || isReadonlyArrayType(tsType) || isTypedArray(tsType); } Utils.isArray = isArray; function isTuple(tsType) { diff --git a/lib/typescriptServices.d.ts b/lib/typescriptServices.d.ts index cbce3761b8..c95a44a61b 100644 --- a/lib/typescriptServices.d.ts +++ b/lib/typescriptServices.d.ts @@ -9257,6 +9257,7 @@ declare namespace ts { function isPrimitiveType(type: Type): boolean; function isTypeSymbol(symbol: Symbol | undefined): boolean; function isGenericArrayType(tsType: Type): tsType is TypeReference; + function isReadonlyArrayType(tsType: Type): boolean; function isTypedArray(tsType: ts.Type): boolean; function isArray(tsType: ts.Type): boolean; function isTuple(tsType: ts.Type): boolean; diff --git a/lib/typescriptServices.js b/lib/typescriptServices.js index e9569e737a..96a53387aa 100644 --- a/lib/typescriptServices.js +++ b/lib/typescriptServices.js @@ -182248,6 +182248,12 @@ var ts; ((_c = tsType.getSymbol()) === null || _c === void 0 ? void 0 : _c.getName()) === "Array"); } Utils.isGenericArrayType = isGenericArrayType; + function isReadonlyArrayType(tsType) { + var _a, _b, _c; + return (isTypeReference(tsType) && ((_a = tsType.typeArguments) === null || _a === void 0 ? void 0 : _a.length) === 1 && ((_b = tsType.target.typeParameters) === null || _b === void 0 ? void 0 : _b.length) === 1 && + (((_c = tsType.getSymbol()) === null || _c === void 0 ? void 0 : _c.getName()) === "ReadonlyArray")); + } + Utils.isReadonlyArrayType = isReadonlyArrayType; function isTypedArray(tsType) { var symbol = tsType.symbol; if (!symbol) { @@ -182258,7 +182264,7 @@ var ts; } Utils.isTypedArray = isTypedArray; function isArray(tsType) { - return isGenericArrayType(tsType) || isTypedArray(tsType); + return isGenericArrayType(tsType) || isReadonlyArrayType(tsType) || isTypedArray(tsType); } Utils.isArray = isArray; function isTuple(tsType) { diff --git a/src/linter/ArkTSLinter_1_1/Utils.ts b/src/linter/ArkTSLinter_1_1/Utils.ts index a8d855fc3b..55f189b2c5 100644 --- a/src/linter/ArkTSLinter_1_1/Utils.ts +++ b/src/linter/ArkTSLinter_1_1/Utils.ts @@ -444,6 +444,13 @@ export function isGenericArrayType(tsType: Type): tsType is TypeReference { ); } +export function isReadonlyArrayType(tsType: Type): boolean { + return ( + isTypeReference(tsType) && tsType.typeArguments?.length === 1 && tsType.target.typeParameters?.length === 1 && + (tsType.getSymbol()?.getName() === "ReadonlyArray") + ); +} + export function isTypedArray(tsType: ts.Type): boolean { const symbol = tsType.symbol; if (!symbol) { @@ -454,7 +461,7 @@ export function isTypedArray(tsType: ts.Type): boolean { } export function isArray(tsType: ts.Type): boolean { - return isGenericArrayType(tsType) || isTypedArray(tsType); + return isGenericArrayType(tsType) || isReadonlyArrayType(tsType) || isTypedArray(tsType); } export function isTuple(tsType: ts.Type): boolean { diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index f03de1feab..5a9f47a265 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -13196,6 +13196,7 @@ declare namespace ts { function isPrimitiveType(type: Type): boolean; function isTypeSymbol(symbol: Symbol | undefined): boolean; function isGenericArrayType(tsType: Type): tsType is TypeReference; + function isReadonlyArrayType(tsType: Type): boolean; function isTypedArray(tsType: ts.Type): boolean; function isArray(tsType: ts.Type): boolean; function isTuple(tsType: ts.Type): boolean; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 7b56d718a9..0d7b684d4f 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -9257,6 +9257,7 @@ declare namespace ts { function isPrimitiveType(type: Type): boolean; function isTypeSymbol(symbol: Symbol | undefined): boolean; function isGenericArrayType(tsType: Type): tsType is TypeReference; + function isReadonlyArrayType(tsType: Type): boolean; function isTypedArray(tsType: ts.Type): boolean; function isArray(tsType: ts.Type): boolean; function isTuple(tsType: ts.Type): boolean;