From fbe417b5371baa683616fce06053c8b48780bd17 Mon Sep 17 00:00:00 2001 From: c30058867 Date: Wed, 5 Feb 2025 14:35:00 +0800 Subject: [PATCH] Fix globalThis Issue:https://gitee.com/openharmony/third_party_typescript/issues/IBK54L Signed-off-by: caiy --- lib/tsserver.js | 14 +++++++++++--- lib/tsserverlibrary.d.ts | 2 ++ lib/tsserverlibrary.js | 14 +++++++++++--- lib/typescript.d.ts | 2 ++ lib/typescript.js | 14 +++++++++++--- lib/typescriptServices.d.ts | 2 ++ lib/typescriptServices.js | 14 +++++++++++--- lib/typingsInstaller.js | 2 +- src/linter/ArkTSLinter_1_1/TypeScriptLinter.ts | 13 +++++++++++-- .../arkts-no-globalthis-6-error.ets | 16 ++++++++++++++++ .../arkts-no-globalthis-6-error.json | 12 ++++++++++++ .../baselines/reference/api/tsserverlibrary.d.ts | 2 ++ tests/baselines/reference/api/typescript.d.ts | 2 ++ 13 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 tests/arkTSTest/testcase/arkts-no-globalthis/arkts-no-globalthis-6-error.ets create mode 100644 tests/arkTSTest/testcase/arkts-no-globalthis/arkts-no-globalthis-6-error.json diff --git a/lib/tsserver.js b/lib/tsserver.js index b578cf4c18..b83eaa54c3 100644 --- a/lib/tsserver.js +++ b/lib/tsserver.js @@ -39144,7 +39144,7 @@ var ts; return withJSDoc(finishNode(node, pos), hasJSDoc); } function isSetLazy(identifier) { - // 1. import lazy { export } from "mod"; + // 1. import lazy { export } from "mod"; // 2. import lazy defaultExport from "mod"; // 3. import lazy defaultExport, { export, /* ... */ } from "mod"; return (identifier === null || identifier === void 0 ? void 0 : identifier.escapedText) === 'lazy' && @@ -198073,6 +198073,7 @@ var ts; this.tscStrictDiagnostics = tscStrictDiagnostics; this.compatibleSdkVersionStage = 'beta1'; this.compatibleSdkVersion = 12; + this.globalThis = 'globalThis'; this.handlersMap = new ts.Map([ [210 /* SyntaxKind.ObjectLiteralExpression */, this.handleObjectLiteralExpression], [209 /* SyntaxKind.ArrayLiteralExpression */, this.handleArrayLiteralExpression], @@ -198121,7 +198122,8 @@ var ts; [94 /* ts.SyntaxKind.ExportKeyword */, this.handleExportKeyword], [281 /* ts.SyntaxKind.ExportDeclaration */, this.handleExportDeclaration], [254 /* ts.SyntaxKind.ReturnStatement */, this.handleReturnStatement], - [169 /* ts.SyntaxKind.Decorator */, this.handleDecorator] + [169 /* ts.SyntaxKind.Decorator */, this.handleDecorator], + [109 /* ts.SyntaxKind.ThisKeyword */, this.handleThisKeyword] ]); this.validatedTypesSet = new ts.Set(); TypeScriptLinter.tsTypeChecker = tsProgram.getLinterTypeChecker(); @@ -199435,7 +199437,7 @@ var ts; } if ((tsIdentSym.flags & 1536 /* ts.SymbolFlags.Module */) !== 0 && (tsIdentSym.flags & 33554432 /* ts.SymbolFlags.Transient */) !== 0 && - tsIdentifier.text === "globalThis") { + tsIdentifier.text === this.globalThis) { this.incrementCounters(node, FaultID.GlobalThis); } else { @@ -200248,6 +200250,12 @@ var ts; } return false; }; + TypeScriptLinter.prototype.handleThisKeyword = function (node) { + var thisSym = ArkTSLinter_1_1.Utils.trueSymbolAtLocation(node); + if (!!thisSym && thisSym.escapedName === this.globalThis) { + this.incrementCounters(node, FaultID.GlobalThis); + } + }; TypeScriptLinter.reportDiagnostics = true; // The SyntaxKind enum defines additional elements at the end of the enum // that serve as markers (FirstX/LastX). Those elements are initialized diff --git a/lib/tsserverlibrary.d.ts b/lib/tsserverlibrary.d.ts index c90562b81b..5fa0a01336 100644 --- a/lib/tsserverlibrary.d.ts +++ b/lib/tsserverlibrary.d.ts @@ -13601,6 +13601,7 @@ declare namespace ts { private fileExportDeclCaches?; private compatibleSdkVersionStage; private compatibleSdkVersion; + private globalThis; constructor(sourceFile: SourceFile, tsProgram: Program, tscStrictDiagnostics?: Map | undefined); static clearTsTypeChecker(): void; readonly handlersMap: ESMap void>; @@ -13725,6 +13726,7 @@ declare namespace ts { private checkAssignmentMatching; private handleDecorator; private isSendableDecoratorValid; + private handleThisKeyword; } } } diff --git a/lib/tsserverlibrary.js b/lib/tsserverlibrary.js index ebee2765fb..62e98572f1 100644 --- a/lib/tsserverlibrary.js +++ b/lib/tsserverlibrary.js @@ -39143,7 +39143,7 @@ var ts; return withJSDoc(finishNode(node, pos), hasJSDoc); } function isSetLazy(identifier) { - // 1. import lazy { export } from "mod"; + // 1. import lazy { export } from "mod"; // 2. import lazy defaultExport from "mod"; // 3. import lazy defaultExport, { export, /* ... */ } from "mod"; return (identifier === null || identifier === void 0 ? void 0 : identifier.escapedText) === 'lazy' && @@ -197818,6 +197818,7 @@ var ts; this.tscStrictDiagnostics = tscStrictDiagnostics; this.compatibleSdkVersionStage = 'beta1'; this.compatibleSdkVersion = 12; + this.globalThis = 'globalThis'; this.handlersMap = new ts.Map([ [210 /* SyntaxKind.ObjectLiteralExpression */, this.handleObjectLiteralExpression], [209 /* SyntaxKind.ArrayLiteralExpression */, this.handleArrayLiteralExpression], @@ -197866,7 +197867,8 @@ var ts; [94 /* ts.SyntaxKind.ExportKeyword */, this.handleExportKeyword], [281 /* ts.SyntaxKind.ExportDeclaration */, this.handleExportDeclaration], [254 /* ts.SyntaxKind.ReturnStatement */, this.handleReturnStatement], - [169 /* ts.SyntaxKind.Decorator */, this.handleDecorator] + [169 /* ts.SyntaxKind.Decorator */, this.handleDecorator], + [109 /* ts.SyntaxKind.ThisKeyword */, this.handleThisKeyword] ]); this.validatedTypesSet = new ts.Set(); TypeScriptLinter.tsTypeChecker = tsProgram.getLinterTypeChecker(); @@ -199180,7 +199182,7 @@ var ts; } if ((tsIdentSym.flags & 1536 /* ts.SymbolFlags.Module */) !== 0 && (tsIdentSym.flags & 33554432 /* ts.SymbolFlags.Transient */) !== 0 && - tsIdentifier.text === "globalThis") { + tsIdentifier.text === this.globalThis) { this.incrementCounters(node, FaultID.GlobalThis); } else { @@ -199993,6 +199995,12 @@ var ts; } return false; }; + TypeScriptLinter.prototype.handleThisKeyword = function (node) { + var thisSym = ArkTSLinter_1_1.Utils.trueSymbolAtLocation(node); + if (!!thisSym && thisSym.escapedName === this.globalThis) { + this.incrementCounters(node, FaultID.GlobalThis); + } + }; TypeScriptLinter.reportDiagnostics = true; // The SyntaxKind enum defines additional elements at the end of the enum // that serve as markers (FirstX/LastX). Those elements are initialized diff --git a/lib/typescript.d.ts b/lib/typescript.d.ts index 1692b17ff5..2c0b5c515d 100644 --- a/lib/typescript.d.ts +++ b/lib/typescript.d.ts @@ -9655,6 +9655,7 @@ declare namespace ts { private fileExportDeclCaches?; private compatibleSdkVersionStage; private compatibleSdkVersion; + private globalThis; constructor(sourceFile: SourceFile, tsProgram: Program, tscStrictDiagnostics?: Map | undefined); static clearTsTypeChecker(): void; readonly handlersMap: ESMap void>; @@ -9779,6 +9780,7 @@ declare namespace ts { private checkAssignmentMatching; private handleDecorator; private isSendableDecoratorValid; + private handleThisKeyword; } } } diff --git a/lib/typescript.js b/lib/typescript.js index ec37e2214a..f34118d30d 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -39134,7 +39134,7 @@ var ts; return withJSDoc(finishNode(node, pos), hasJSDoc); } function isSetLazy(identifier) { - // 1. import lazy { export } from "mod"; + // 1. import lazy { export } from "mod"; // 2. import lazy defaultExport from "mod"; // 3. import lazy defaultExport, { export, /* ... */ } from "mod"; return (identifier === null || identifier === void 0 ? void 0 : identifier.escapedText) === 'lazy' && @@ -186912,6 +186912,7 @@ var ts; this.tscStrictDiagnostics = tscStrictDiagnostics; this.compatibleSdkVersionStage = 'beta1'; this.compatibleSdkVersion = 12; + this.globalThis = 'globalThis'; this.handlersMap = new ts.Map([ [210 /* SyntaxKind.ObjectLiteralExpression */, this.handleObjectLiteralExpression], [209 /* SyntaxKind.ArrayLiteralExpression */, this.handleArrayLiteralExpression], @@ -186960,7 +186961,8 @@ var ts; [94 /* ts.SyntaxKind.ExportKeyword */, this.handleExportKeyword], [281 /* ts.SyntaxKind.ExportDeclaration */, this.handleExportDeclaration], [254 /* ts.SyntaxKind.ReturnStatement */, this.handleReturnStatement], - [169 /* ts.SyntaxKind.Decorator */, this.handleDecorator] + [169 /* ts.SyntaxKind.Decorator */, this.handleDecorator], + [109 /* ts.SyntaxKind.ThisKeyword */, this.handleThisKeyword] ]); this.validatedTypesSet = new ts.Set(); TypeScriptLinter.tsTypeChecker = tsProgram.getLinterTypeChecker(); @@ -188274,7 +188276,7 @@ var ts; } if ((tsIdentSym.flags & 1536 /* ts.SymbolFlags.Module */) !== 0 && (tsIdentSym.flags & 33554432 /* ts.SymbolFlags.Transient */) !== 0 && - tsIdentifier.text === "globalThis") { + tsIdentifier.text === this.globalThis) { this.incrementCounters(node, FaultID.GlobalThis); } else { @@ -189087,6 +189089,12 @@ var ts; } return false; }; + TypeScriptLinter.prototype.handleThisKeyword = function (node) { + var thisSym = ArkTSLinter_1_1.Utils.trueSymbolAtLocation(node); + if (!!thisSym && thisSym.escapedName === this.globalThis) { + this.incrementCounters(node, FaultID.GlobalThis); + } + }; TypeScriptLinter.reportDiagnostics = true; // The SyntaxKind enum defines additional elements at the end of the enum // that serve as markers (FirstX/LastX). Those elements are initialized diff --git a/lib/typescriptServices.d.ts b/lib/typescriptServices.d.ts index 65bc4903dc..c334ee2358 100644 --- a/lib/typescriptServices.d.ts +++ b/lib/typescriptServices.d.ts @@ -9655,6 +9655,7 @@ declare namespace ts { private fileExportDeclCaches?; private compatibleSdkVersionStage; private compatibleSdkVersion; + private globalThis; constructor(sourceFile: SourceFile, tsProgram: Program, tscStrictDiagnostics?: Map | undefined); static clearTsTypeChecker(): void; readonly handlersMap: ESMap void>; @@ -9779,6 +9780,7 @@ declare namespace ts { private checkAssignmentMatching; private handleDecorator; private isSendableDecoratorValid; + private handleThisKeyword; } } } diff --git a/lib/typescriptServices.js b/lib/typescriptServices.js index 70e94c0313..ac1cf23f90 100644 --- a/lib/typescriptServices.js +++ b/lib/typescriptServices.js @@ -39134,7 +39134,7 @@ var ts; return withJSDoc(finishNode(node, pos), hasJSDoc); } function isSetLazy(identifier) { - // 1. import lazy { export } from "mod"; + // 1. import lazy { export } from "mod"; // 2. import lazy defaultExport from "mod"; // 3. import lazy defaultExport, { export, /* ... */ } from "mod"; return (identifier === null || identifier === void 0 ? void 0 : identifier.escapedText) === 'lazy' && @@ -186912,6 +186912,7 @@ var ts; this.tscStrictDiagnostics = tscStrictDiagnostics; this.compatibleSdkVersionStage = 'beta1'; this.compatibleSdkVersion = 12; + this.globalThis = 'globalThis'; this.handlersMap = new ts.Map([ [210 /* SyntaxKind.ObjectLiteralExpression */, this.handleObjectLiteralExpression], [209 /* SyntaxKind.ArrayLiteralExpression */, this.handleArrayLiteralExpression], @@ -186960,7 +186961,8 @@ var ts; [94 /* ts.SyntaxKind.ExportKeyword */, this.handleExportKeyword], [281 /* ts.SyntaxKind.ExportDeclaration */, this.handleExportDeclaration], [254 /* ts.SyntaxKind.ReturnStatement */, this.handleReturnStatement], - [169 /* ts.SyntaxKind.Decorator */, this.handleDecorator] + [169 /* ts.SyntaxKind.Decorator */, this.handleDecorator], + [109 /* ts.SyntaxKind.ThisKeyword */, this.handleThisKeyword] ]); this.validatedTypesSet = new ts.Set(); TypeScriptLinter.tsTypeChecker = tsProgram.getLinterTypeChecker(); @@ -188274,7 +188276,7 @@ var ts; } if ((tsIdentSym.flags & 1536 /* ts.SymbolFlags.Module */) !== 0 && (tsIdentSym.flags & 33554432 /* ts.SymbolFlags.Transient */) !== 0 && - tsIdentifier.text === "globalThis") { + tsIdentifier.text === this.globalThis) { this.incrementCounters(node, FaultID.GlobalThis); } else { @@ -189087,6 +189089,12 @@ var ts; } return false; }; + TypeScriptLinter.prototype.handleThisKeyword = function (node) { + var thisSym = ArkTSLinter_1_1.Utils.trueSymbolAtLocation(node); + if (!!thisSym && thisSym.escapedName === this.globalThis) { + this.incrementCounters(node, FaultID.GlobalThis); + } + }; TypeScriptLinter.reportDiagnostics = true; // The SyntaxKind enum defines additional elements at the end of the enum // that serve as markers (FirstX/LastX). Those elements are initialized diff --git a/lib/typingsInstaller.js b/lib/typingsInstaller.js index d65de5bf86..4e5b1434b4 100644 --- a/lib/typingsInstaller.js +++ b/lib/typingsInstaller.js @@ -39124,7 +39124,7 @@ var ts; return withJSDoc(finishNode(node, pos), hasJSDoc); } function isSetLazy(identifier) { - // 1. import lazy { export } from "mod"; + // 1. import lazy { export } from "mod"; // 2. import lazy defaultExport from "mod"; // 3. import lazy defaultExport, { export, /* ... */ } from "mod"; return (identifier === null || identifier === void 0 ? void 0 : identifier.escapedText) === 'lazy' && diff --git a/src/linter/ArkTSLinter_1_1/TypeScriptLinter.ts b/src/linter/ArkTSLinter_1_1/TypeScriptLinter.ts index fd8d346829..3dad42fb7d 100644 --- a/src/linter/ArkTSLinter_1_1/TypeScriptLinter.ts +++ b/src/linter/ArkTSLinter_1_1/TypeScriptLinter.ts @@ -137,6 +137,7 @@ export class TypeScriptLinter { private fileExportDeclCaches?: Set; private compatibleSdkVersionStage: string = 'beta1'; private compatibleSdkVersion: number = 12; + private globalThis: string = 'globalThis'; constructor(private sourceFile: SourceFile, /* private */ tsProgram: Program, @@ -211,7 +212,8 @@ export class TypeScriptLinter { [ts.SyntaxKind.ExportKeyword, this.handleExportKeyword], [ts.SyntaxKind.ExportDeclaration, this.handleExportDeclaration], [ts.SyntaxKind.ReturnStatement, this.handleReturnStatement], - [ts.SyntaxKind.Decorator, this.handleDecorator] + [ts.SyntaxKind.Decorator, this.handleDecorator], + [ts.SyntaxKind.ThisKeyword, this.handleThisKeyword] ]); public incrementCounters(node: Node | CommentRange, faultId: number, autofixable = false, autofix?: Autofix[]): void { @@ -1630,7 +1632,7 @@ export class TypeScriptLinter { if ( (tsIdentSym.flags & ts.SymbolFlags.Module) !== 0 && (tsIdentSym.flags & ts.SymbolFlags.Transient) !== 0 && - tsIdentifier.text === "globalThis" + tsIdentifier.text === this.globalThis ) { this.incrementCounters(node, FaultID.GlobalThis); } else { @@ -2566,6 +2568,13 @@ export class TypeScriptLinter { } return false; } + + private handleThisKeyword(node: ts.Node): void { + const thisSym = Utils.trueSymbolAtLocation(node); + if (!!thisSym && thisSym.escapedName === this.globalThis) { + this.incrementCounters(node, FaultID.GlobalThis); + } + } } } } diff --git a/tests/arkTSTest/testcase/arkts-no-globalthis/arkts-no-globalthis-6-error.ets b/tests/arkTSTest/testcase/arkts-no-globalthis/arkts-no-globalthis-6-error.ets new file mode 100644 index 0000000000..5672c5b9e9 --- /dev/null +++ b/tests/arkTSTest/testcase/arkts-no-globalthis/arkts-no-globalthis-6-error.ets @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const a: number = this.aaa; \ No newline at end of file diff --git a/tests/arkTSTest/testcase/arkts-no-globalthis/arkts-no-globalthis-6-error.json b/tests/arkTSTest/testcase/arkts-no-globalthis/arkts-no-globalthis-6-error.json new file mode 100644 index 0000000000..46b73fec77 --- /dev/null +++ b/tests/arkTSTest/testcase/arkts-no-globalthis/arkts-no-globalthis-6-error.json @@ -0,0 +1,12 @@ +{ + "arktsVersion_1_0": [], + "arktsVersion_1_1": [ + { + "messageText": "\"globalThis\" is not supported (arkts-no-globalthis)", + "expectLineAndCharacter": { + "line": 16, + "character": 19 + } + } + ] +} \ No newline at end of file diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index c90562b81b..5fa0a01336 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -13601,6 +13601,7 @@ declare namespace ts { private fileExportDeclCaches?; private compatibleSdkVersionStage; private compatibleSdkVersion; + private globalThis; constructor(sourceFile: SourceFile, tsProgram: Program, tscStrictDiagnostics?: Map | undefined); static clearTsTypeChecker(): void; readonly handlersMap: ESMap void>; @@ -13725,6 +13726,7 @@ declare namespace ts { private checkAssignmentMatching; private handleDecorator; private isSendableDecoratorValid; + private handleThisKeyword; } } } diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 1692b17ff5..2c0b5c515d 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -9655,6 +9655,7 @@ declare namespace ts { private fileExportDeclCaches?; private compatibleSdkVersionStage; private compatibleSdkVersion; + private globalThis; constructor(sourceFile: SourceFile, tsProgram: Program, tscStrictDiagnostics?: Map | undefined); static clearTsTypeChecker(): void; readonly handlersMap: ESMap void>; @@ -9779,6 +9780,7 @@ declare namespace ts { private checkAssignmentMatching; private handleDecorator; private isSendableDecoratorValid; + private handleThisKeyword; } } } -- Gitee