Skip to content

减少不必要的行数#1246

Merged
CodFrm merged 2 commits intoscriptscat:release/v1.3from
cyfung1031:pr-reduce-excessive-newlines
Feb 15, 2026
Merged

减少不必要的行数#1246
CodFrm merged 2 commits intoscriptscat:release/v1.3from
cyfung1031:pr-reduce-excessive-newlines

Conversation

@cyfung1031
Copy link
Collaborator

行数影响 console.log console.debug 那个行数显示
跟 userscript 写的行数相差很远

Before

Screenshot 2026-02-14 at 15 35 38

After

Screenshot 2026-02-14 at 15 32 32

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 旨在通过压缩 compileScriptCodeByResource 生成的包装代码(try/catch + with + async IIFE)的额外换行数,减少 DevTools 中 console.log/debug 与堆栈行号相对用户脚本源码的偏移,提升调试体验。

Changes:

  • 抽出 addTryCatch(),并对 try/catch 包装模板进行“去换行 + 压缩空白”处理以减少引入行数
  • 将脚本主体拼接逻辑改为数组 join,便于控制换行位置
  • 更新单测中对生成代码片段的断言以匹配新的输出格式

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/app/service/content/utils.ts 引入 addTryCatch 并调整脚本拼接方式,以减少包装层新增行数
src/app/service/content/utils.test.ts 更新断言以匹配 return(async function(){ 的新输出

Comment on lines +91 to +93
if (e.message && e.stack) {
console.error("ERROR: Execution of script '" + arguments[1] + "' failed! " + e.message);
console.log(e.stack);
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch 分支里直接访问 e.message / e.stack 在 throw null / undefined / 原始类型时会二次抛出 TypeError,导致原始异常被吞掉且日志缺失。建议先判断 e 是否为对象且非 null,再读取 message/stack(或用可选链 e?.message / e?.stack 并配合 typeof)。

Suggested change
if (e.message && e.stack) {
console.error("ERROR: Execution of script '" + arguments[1] + "' failed! " + e.message);
console.log(e.stack);
if (e && typeof e === "object" && "message" in e && "stack" in e) {
console.error("ERROR: Execution of script '" + arguments[1] + "' failed! " + (e as any).message);
console.log((e as any).stack);

Copilot uses AI. Check for mistakes.
Comment on lines +115 to 125
const joinedCode = [
"with(arguments[0]||this.$){",
`${preCode}`,
"return(async function(){",
`${code}`,
"}).call(this);}",
]
.filter(Boolean)
.join("\n");
const codeBody = addTryCatch(joinedCode);
return `${codeBody}${sourceMapTo(`${resource.name}.user.js`)}\n`;
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 PR 的目标是减少包装代码引入的额外行数以改善 console 输出的行号,但当前测试只校验了部分片段字符串。建议补充针对“try/catch 包装被压缩成单行”的断言(例如校验 try 后立刻跟 with、以及 with 结束后紧跟 catch),以防后续格式化/重构又把多余换行加回来。

Copilot generated this review using guidance from repository custom instructions.
@CodFrm CodFrm merged commit 1fe8d20 into scriptscat:release/v1.3 Feb 15, 2026
9 checks passed
@CodFrm
Copy link
Member

CodFrm commented Feb 15, 2026

我跑了一下,一些脚本有问题啊,你是不是都没怎么做过测试呀

@cyfung1031
Copy link
Collaborator Author

我跑了一下,一些脚本有问题啊,你是不是都没怎么做过测试呀

有什么问题?
我跑了一下呀
可能我的脚本比较简单?

@cyfung1031
Copy link
Collaborator Author

呀,我好像知道问题了
我提交一下PR

@CodFrm
Copy link
Member

CodFrm commented Feb 15, 2026

cyfung1031 added a commit to cyfung1031/scriptcat that referenced this pull request Feb 15, 2026
@cyfung1031 cyfung1031 mentioned this pull request Feb 15, 2026
@cyfung1031
Copy link
Collaborator Author

cyfung1031 commented Feb 15, 2026

CodFrm added a commit that referenced this pull request Feb 15, 2026
* 修复 #1246 错误

* .

* 让replace不识别$

* 让replace不识别$

* Revert "让replace不识别$"

This reverts commit 2370609.

---------

Co-authored-by: 王一之 <yz@ggnb.top>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants