-
Notifications
You must be signed in to change notification settings - Fork 1
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
Merged
Viijay-Kr
merged 1 commit into
main
from
fix-mdx-analyzer-conflicts-by-updating-typescript-server-plugin-2
Aug 22, 2024
Merged
fix(mdx-analyzer-conflicts): proxy language service #5
Viijay-Kr
merged 1 commit into
main
from
fix-mdx-analyzer-conflicts-by-updating-typescript-server-plugin-2
Aug 22, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was referenced Aug 20, 2024
I applied the patch via editing the extension files:
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes Viijay-Kr/react-ts-css#160
Related to mdx-js/mdx-analyzer#451 (comment)