From befb4792ce95037839166157d3fc73b5816d2ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=8C?= <31638637+kitter960324@users.noreply.github.com> Date: Wed, 2 Dec 2026 14:37:53 +0800 Subject: [PATCH] fix(editor): colour a module's top-level bindings as variables, not globals (#7409) Fixes #7047. tsserver marks only function- and block-scoped bindings local, so every top-level const, let, function and import of a JS/TS file got cm-sem-global. A file with an import or export statement is an ES module, whose top-level bindings WebStorm colours as variables; only a script's top-level bindings keep the global classes. The module check is cached per syntax tree. Co-authored-by: kitter960324 <31638637+kitter960324@users.noreply.github.com> --- .../lsp/semantic/javascript/index.test.ts | 18 +++++++++++++++ src/editor/lsp/semantic/javascript/index.ts | 23 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/editor/lsp/semantic/javascript/index.test.ts b/src/editor/lsp/semantic/javascript/index.test.ts index 9073f134b..1832b1a0a 100644 --- a/src/editor/lsp/semantic/javascript/index.test.ts +++ b/src/editor/lsp/semantic/javascript/index.test.ts @@ -86,6 +86,24 @@ describe("JavaScript and TypeScript semantic tokens (#4059)", () => { expect(at(1, "Math")).toBe("cm-sem-builtin"); expect(at(1, "parseInt")).toBe("cm-sem-function cm-sem-builtin"); }); + + it("colours a module's top-level bindings as variables, not globals", () => { + const lines = [ + "import { FETCH_ITEMS } from './actions';", + "const initialState = { items: [] };", + "export function itemsReducer(state = initialState, action: any) {", + " return action.type === FETCH_ITEMS ? state : state;", + "}", + ]; + const at = classes("typescript", lines, [ + [0, "FETCH_ITEMS", "variable", ["readonly"]], + [1, "initialState", "variable", ["declaration", "readonly"]], + [2, "itemsReducer", "function", ["declaration"]], + ]); + expect(at(0, "FETCH_ITEMS")).toBeUndefined(); + expect(at(1, "initialState")).toBe("cm-sem-declaration"); + expect(at(2, "itemsReducer")).toBe("cm-sem-function cm-sem-declaration"); + }); }); describe("decorator names after an edit", () => { diff --git a/src/editor/lsp/semantic/javascript/index.ts b/src/editor/lsp/semantic/javascript/index.ts index aaa25096e..18b98e4a0 100644 --- a/src/editor/lsp/semantic/javascript/index.ts +++ b/src/editor/lsp/semantic/javascript/index.ts @@ -78,9 +78,27 @@ const TYPE_KIND_CLASS: Record = { typeParameter: "cm-sem-js-type-parameter", }; +/** + * Whether the file is an ES module (it has an `import` or `export` statement). tsserver attaches + * `local` only to bindings declared inside a function or block, so a top-level `const` or + * `function` differs by file kind: a global in a script, an exported variable in a module. + * Kept per tree: the parse is reused across token calls, and an edit re-parses only a new tree. + */ +const moduleFiles = new WeakMap(); + +function isModule(tree: Tree): boolean { + const cached = moduleFiles.get(tree); + if (cached !== undefined) return cached; + let found = false; + for (let node = tree.topNode.firstChild; node && !found; node = node.nextSibling) + found = node.name === "ImportDeclaration" || node.name === "ExportDeclaration"; + moduleFiles.set(tree, found); + return found; +} + const javascriptSemantics: LanguageSemantics = { syntaxRanges: (tree) => decoratorRanges(tree), - tokenClasses({ type, modifiers, classes, marksLocals }) { + tokenClasses({ type, modifiers, classes, marksLocals, tree }) { // WebStorm's Instance / Static member function: a method's declaration and its calls (a // library's own keep the Predefined style). if ((type === "method" || type === "member") && !modifiers.includes("defaultLibrary")) @@ -89,6 +107,9 @@ const javascriptSemantics: LanguageSemantics = { if (!modifiers.includes("defaultLibrary") && Object.hasOwn(TYPE_KIND_CLASS, type)) classes.push(TYPE_KIND_CLASS[type]!); if (!marksLocals || modifiers.includes("local") || modifiers.includes("defaultLibrary")) return; + // A module's top-level bindings are exported variables (`JS.EXPORTED.VARIABLE`), which + // WebStorm colours as variables; only a script's top-level bindings are globals (#7047). + if (tree && isModule(tree)) return; if (type === "variable" && !classes.includes("cm-sem-constant")) classes.push("cm-sem-global"); else if (type === "function") classes.push("cm-sem-global-function"); },