feat: autoload plugins that are located in `@*/prettier-plugin-* (#5945)

* add tests

* implement the feature

* update docs

* update changelog

* typo
master
Hugo Alliaume 2019-03-10 10:34:04 +01:00 committed by Lucas Azzola
parent a6ca8201da
commit b38319740a
6 changed files with 59 additions and 3 deletions

View File

@ -143,3 +143,5 @@ Examples:
(a?.b[c]).c();
let value = /** @type {string} */ (this.members[0]).functionCall();
```
- CLI: Plugins published as a scoped NPM package (e.g.: `@name/prettier-plugin-foo`) are now automatically registered ([#5945] by [@Kocal])

View File

@ -11,7 +11,9 @@ Plugins are ways of adding new languages to Prettier. Prettier's own implementat
## Using Plugins
Plugins are automatically loaded if you have them installed in the same `node_modules` directory where `prettier` is located. Plugin package names must start with `@prettier/plugin-` or `prettier-plugin-` to be registered.
Plugins are automatically loaded if you have them installed in the same `node_modules` directory where `prettier` is located. Plugin package names must start with `@prettier/plugin-` or `prettier-plugin-` or `@<scope>/prettier-plugin-` to be registered.
> `<scope>` should be replaced by a name, read more about [NPM scope](https://docs.npmjs.com/misc/scope.html).
When plugins cannot be found automatically, you can load them with:
@ -33,7 +35,7 @@ When plugins cannot be found automatically, you can load them with:
});
```
Prettier expects each of `pluginSearchDirs` to contain `node_modules` subdirectory, where `@prettier/plugin-*` and `prettier-plugin-*` will be searched. For instance, this can be your project directory or the location of global npm modules.
Prettier expects each of `pluginSearchDirs` to contain `node_modules` subdirectory, where `@prettier/plugin-*`, `@*/prettier-plugin-*` and `prettier-plugin-*` will be searched. For instance, this can be your project directory or the location of global npm modules.
Providing at least one path to `--plugin-search-dir`/`pluginSearchDirs` turns off plugin autoloading in the default directory (i.e. `node_modules` above `prettier` binary).

View File

@ -95,7 +95,11 @@ function loadPlugins(plugins, pluginSearchDirs) {
function findPluginsInNodeModules(nodeModulesDir) {
const pluginPackageJsonPaths = globby.sync(
["prettier-plugin-*/package.json", "@prettier/plugin-*/package.json"],
[
"prettier-plugin-*/package.json",
"@*/prettier-plugin-*/package.json",
"@prettier/plugin-*/package.json"
],
{ cwd: nodeModulesDir }
);
return pluginPackageJsonPaths.map(path.dirname);

View File

@ -21,6 +21,15 @@ describe("automatically loads '@prettier/plugin-*'", () => {
});
});
describe("automatically loads '@<name>/prettier-plugin-*'", () => {
runPrettier("plugins/automatic", ["file.txt", "--parser=foobar"]).test({
stdout: "foobar+contents" + EOL,
stderr: "",
status: 0,
write: []
});
});
describe("automatically loads 'prettier-plugin-*' from --plugin-search-dir (same as autoload dir)", () => {
runPrettier("plugins/automatic", [
"file.txt",
@ -47,6 +56,19 @@ describe("automatically loads '@prettier/plugin-*' from --plugin-search-dir (sam
});
});
describe("automatically loads '@<name>/prettier-plugin-*' from --plugin-search-dir (same as autoload dir)", () => {
runPrettier("plugins/automatic", [
"file.txt",
"--parser=foobar",
"--plugin-search-dir=."
]).test({
stdout: "foobar+contents" + EOL,
stderr: "",
status: 0,
write: []
});
});
describe("automatically loads 'prettier-plugin-*' from --plugin-search-dir (different to autoload dir)", () => {
runPrettier("plugins", [
"automatic/file.txt",

View File

@ -0,0 +1,25 @@
"use strict";
const prettier = require("prettier/local");
const concat = prettier.doc.builders.concat;
module.exports = {
languages: [
{
name: "foobar",
parsers: ["foobar"],
extensions: [".foobar"]
}
],
parsers: {
foobar: {
parse: text => ({ text }),
astFormat: "foobar"
}
},
printers: {
foobar: {
print: path => concat(["foobar+", path.getValue().text])
}
}
};

View File

@ -0,0 +1 @@
{}