дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
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]>
Цей коміт міститься в:
2 files changed
+40
-1
No files matched your search
@@ -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", () => {
|
||||
|
||||
@@ -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");
|
||||
},
|
||||
|
||||
Reference in new issue
Заблокувати користувача