From c4c69ad120f9183d881d7df44452b07db49c87ec Mon Sep 17 00:00:00 2001 From: yaohaosen Date: Tue, 17 Jun 2025 22:02:45 +0800 Subject: [PATCH] [LSP] Fix getFileSource from emty file Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICFWG0 Signed-off-by: yaohaosen --- ets2panda/bindings/src/lsp_helper.ts | 4 ++-- ets2panda/bindings/test/cases.ts | 4 ++++ .../bindings/test/expected/getFileSource.json | 3 +++ .../testcases/getFileSource/getFileSource1.ets | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 ets2panda/bindings/test/expected/getFileSource.json create mode 100644 ets2panda/bindings/test/testcases/getFileSource/getFileSource1.ets diff --git a/ets2panda/bindings/src/lsp_helper.ts b/ets2panda/bindings/src/lsp_helper.ts index 1731499f55..8ebf5c49f2 100644 --- a/ets2panda/bindings/src/lsp_helper.ts +++ b/ets2panda/bindings/src/lsp_helper.ts @@ -107,8 +107,8 @@ export class Lsp { } private getFileSource(filePath: string): string { - const getSource = this.filesMap.get(filePath) || this.getFileContent(filePath); - if (!getSource) { + const getSource = this.filesMap.get(filePath) || this.getFileContent(filePath) || fs.readFileSync(filePath, 'utf8'); + if (getSource === undefined) { throw new Error(`File content not found for path: ${filePath}`); } return getSource.replace(/\r\n/g, '\n'); diff --git a/ets2panda/bindings/test/cases.ts b/ets2panda/bindings/test/cases.ts index 3c4cde6076..56f4d57b8d 100644 --- a/ets2panda/bindings/test/cases.ts +++ b/ets2panda/bindings/test/cases.ts @@ -63,6 +63,10 @@ export const testCases: TestCases = { expectedFilePath: resolveTestPath('test/expected/getFileReferences.json'), '1': [resolveTestPath('test/testcases/getFileReferences/getFileReferences1_export.ets')] }, + getFileSource: { + expectedFilePath: resolveTestPath('test/expected/getFileSource.json'), + '1': [resolveTestPath('test/testcases/getFileSource/getFileSource1.ets')] + }, getReferencesAtPosition: { expectedFilePath: resolveTestPath('test/expected/getReferencesAtPosition.json'), '1': [resolveTestPath('test/testcases/getReferencesAtPosition/getReferencesAtPosition1.ets'), 613], diff --git a/ets2panda/bindings/test/expected/getFileSource.json b/ets2panda/bindings/test/expected/getFileSource.json new file mode 100644 index 0000000000..e893655add --- /dev/null +++ b/ets2panda/bindings/test/expected/getFileSource.json @@ -0,0 +1,3 @@ +{ + "1": "/*\n * Copyright (c) 2025 Huawei Device Co., Ltd.\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nlet a = 1;" +} \ No newline at end of file diff --git a/ets2panda/bindings/test/testcases/getFileSource/getFileSource1.ets b/ets2panda/bindings/test/testcases/getFileSource/getFileSource1.ets new file mode 100644 index 0000000000..119751e901 --- /dev/null +++ b/ets2panda/bindings/test/testcases/getFileSource/getFileSource1.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. + */ + +let a = 1; \ No newline at end of file -- Gitee