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 <[email protected]>
Цей коміт міститься в:
王斌 authored and GitHub committed 2026-12-02 14:37:53 +08:00
1 джерело 34741f56c4
коміт befb4792ce
2 files changed
+40 -1

No files matched your search

+18
Переглянути файл
@@ -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", () => {
+22 -1
Переглянути файл
@@ -78,9 +78,27 @@ const TYPE_KIND_CLASS: Record<string, string> = {
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<Tree, boolean>();
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");
},