How can a plugin override the language for a specific filename? #16621
|
Hey, Is it possible for a plugin to override the language for a preconfigured list of file names, even when their file extension matches a built-in language? For example, if a plugin adds the new language export const languages: Partial<SupportLanguage>[] = [
{
name: "SomethingScript",
parsers: ["something-script"]
}
];
export const parsers: Record<string, Parser> = {
"something-script": {
astFormat: "something-script-ast"
// TODO
}
};
export const printers: Record<string, Printer> = {
"something-script-ast": {
// TODO
}
};I've seen the export const languages: Partial<SupportLanguage>[] = [
{
name: "SomethingScript",
parsers: ["something-script"],
filenames: ["something.js"]
}
];Thanks in advance! |
Replies: 1 comment 2 replies
|
Yes, A few things to check if it's not kicking in:
Also worth knowing: on newer Prettier versions a language can define an |
Yes,
filenamesis the right mechanism, and it does beat extension inference. When Prettier infers a language for a file it matches in two passes: first it checks every language'sfilenamesfor an exact basename match, and only if nothing matched does it fall back to matchingextensions. So a plugin language withfilenames: ["something.js"]wins over the built-in JavaScript language for a file namedsomething.js, even though.jswould otherwise match babel. Plugin languages also take priority over the core ones (the search list is built with the plugins reversed, so later-loaded plugins win over earlier ones and over the defaults).A few things to check if it's not kicking in: