Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mdx-analyzer-conflicts): proxy language service #5

Conversation

Viijay-Kr
Copy link
Owner

@Viijay-Kr Viijay-Kr commented Aug 20, 2024

@karlhorky
Copy link
Collaborator

I got a chance to test this today, and it appears to be not conflicting with the error positions of the MDX extension:

Screenshot 2024-08-21 at 21 58 52

@karlhorky
Copy link
Collaborator

I applied the patch via editing the extension files:

code ~/.vscode/extensions/viijay-kr.react-ts-css-3.2.3/node_modules/typescript-cleanup-definitions/index.js

I used this version, which I derived from the diff of this PR:

"use strict";
let settings = {
    modules: [],
    name: "typescript-cleanup-definitions",
    enable: true,
};
function init({ typescript: ts, }) {
    try {
        function create(info) {
            const { proxy, initialize } = createProxyLanguageService(
                info.languageService,
                ts
            )
            info.languageService = proxy
            initialize()
            return info.languageService
        }
        function onConfigurationChanged(_cfg) {
            settings = _cfg;
        }
        return { create, onConfigurationChanged };
    }
    catch (e) {
        console.error(e);
        throw new Error("Cannot load `typescript-cleanup-definitions`");
    }
}

const createProxyLanguageService = (languageService, ts) => {
    const proxyCache = new Map();
    let getProxyMethod;
    const proxy = new Proxy(languageService, {
        get(target, p, receiver) {
            if (getProxyMethod) {
                if (!proxyCache.has(p)) {
                    proxyCache.set(p, getProxyMethod(target, p));
                }
                const proxyMethod = proxyCache.get(p);
                if (proxyMethod) {
                    return proxyMethod;
                }
            }
            return Reflect.get(target, p, receiver);
        },
        set(target, p, value, receiver) {
            return Reflect.set(target, p, value, receiver);
        },
    });
    const initialize = () => {
        getProxyMethod = (target, p) => {
            switch (p) {
                case 'getDefinitionAndBoundSpan':
                    return (filename, position) => {
                        var _a;
                        const prior = target.getDefinitionAndBoundSpan(filename, position);
                        if (!prior) {
                            return;
                        }
                        if (settings.enable) {
                            prior.definitions = (_a = prior.definitions) === null || _a === void 0 ? void 0 : _a.filter(({ fileName, textSpan, kind, name, containerName, }) => {
                                if (kind ===
                                    ts.ScriptElementKind
                                        .indexSignatureElement &&
                                    name === '__index') {
                                    if (containerName === 'CSSModule' ||
                                        containerName === 'CSSModuleClasses') {
                                        return false;
                                    }
                                }
                                if (kind === 'index' &&
                                    name === '__index') {
                                    const definitionNode = findNodeAtPosition(ts, languageService
                                        .getProgram()
                                        .getSourceFile(fileName), textSpan.start);
                                    let moduleDeclaration;
                                    ts.findAncestor(definitionNode, (node) => {
                                        if (ts.isModuleDeclaration(node)) {
                                            moduleDeclaration = node;
                                            return 'quit';
                                        }
                                        return false;
                                    });
                                    if ((moduleDeclaration === null || moduleDeclaration === void 0 ? void 0 : moduleDeclaration.name.text) &&
                                        settings.modules.includes(moduleDeclaration.name.text)) {
                                        return false;
                                    }
                                }
                                return true;
                            });
                        }
                        return prior;
                    };
            }
        };
    };
    return { proxy, initialize };
};

const findNodeAtPosition = (ts, sourceFile, position) => {
    function find(node) {
        if (position >= node.getStart() && position <= node.getEnd()) {
            return ts.forEachChild(node, find) || node;
        }
        return;
    }
    return find(sourceFile);
};
module.exports = init;
//# sourceMappingURL=index.js.map

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.

Errors in .mdx files with mdx.checkMdx in tsconfig.json appear at end of file
2 participants