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"); },