Skip to content

Commit

Permalink
feat: resolve plugins path in symlink node_modules structure dirs (p…
Browse files Browse the repository at this point in the history
…rettier#2566)

* feat: support symlink `node_modules` structure load plugin

* test: add test for pnpm scene

* docs: add changelog

* chore: remove temp data
  • Loading branch information
fz6m authored Aug 20, 2022
1 parent 07344cc commit 22257ff
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ jobs:
os: [windows-latest, ubuntu-latest, macos-latest]
name: Test on ${{ matrix.os }}
steps:
- uses: pnpm/[email protected]
with:
version: 7
run_install: false
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"scripts": {
"clean": "node ./scripts/clean.js",
"lint": "eslint -c .eslintrc.js --ext .ts .",
"pretest": "yarn test-compile && cd test-fixtures/plugins && yarn install && cd ../outdated && yarn install && cd ../module && yarn install && cd ../specific-version && yarn install && cd ../explicit-dep && yarn install && cd implicit-dep && yarn install",
"pretest": "yarn test-compile && cd test-fixtures/plugins && yarn install && cd ../plugins-pnpm && pnpm i && cd ../outdated && yarn install && cd ../module && yarn install && cd ../specific-version && yarn install && cd ../explicit-dep && yarn install && cd implicit-dep && yarn install",
"prettier": "prettier --write '**/*.{ts,json,md,hbs,yml,js}'",
"test-compile": "yarn clean && tsc -p ./ && yarn webpack",
"test": "node ./out/test/runTests.js",
Expand Down
53 changes: 48 additions & 5 deletions src/ModuleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ export class ModuleResolver implements ModuleResolverInterface {
);
}

if (resolvedConfig) {
resolvedConfig = this.resolveConfigPlugins(resolvedConfig, fileName);
}

if (!isVirtual && !resolvedConfig && vscodeConfig.requireConfig) {
this.loggingService.logInfo(
"Require config set to true and no config present. Skipping file."
Expand All @@ -326,14 +330,16 @@ export class ModuleResolver implements ModuleResolverInterface {
this.path2Module.clear();
}

private get nodeModuleLoader() {
return typeof __webpack_require__ === "function"
? __non_webpack_require__
: require;
}

// Source: https://github.com/microsoft/vscode-eslint/blob/master/server/src/eslintServer.ts
private loadNodeModule<T>(moduleName: string): T | undefined {
const r =
typeof __webpack_require__ === "function"
? __non_webpack_require__
: require;
try {
return r(moduleName);
return this.nodeModuleLoader(moduleName);
} catch (error) {
this.loggingService.logError(
`Error loading node module '${moduleName}'`,
Expand All @@ -343,6 +349,43 @@ export class ModuleResolver implements ModuleResolverInterface {
return undefined;
}

private resolveNodeModule(moduleName: string, options?: { paths: string[] }) {
try {
return this.nodeModuleLoader.resolve(moduleName, options);
} catch (error) {
this.loggingService.logError(
`Error resolve node module '${moduleName}'`,
error
);
}
return undefined;
}

/**
* Resolve plugin package path for symlink structure dirs
* See https://github.com/prettier/prettier/issues/8056
*/
private resolveConfigPlugins(
config: PrettierOptions,
fileName: string
): PrettierOptions {
if (config?.plugins?.length) {
config.plugins = config.plugins.map((plugin) => {
if (
typeof plugin === "string" &&
!plugin.startsWith(".") &&
!path.isAbsolute(plugin)
) {
return (
this.resolveNodeModule(plugin, { paths: [fileName] }) || plugin
);
}
return plugin;
});
}
return config;
}

private isInternalTestRoot(dir: string): boolean {
if (process.env.NODE_ENV !== "production") {
// This is for testing purposes only. This code is removed in the
Expand Down
6 changes: 6 additions & 0 deletions src/test/suite/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ suite("Test plugins", function () {
const expected = await getText("plugins", "index.result.php");
assert.equal(actual, expected);
});

test("it correctly resolved plugin in pnpm node_modules dirs structure", async () => {
const { actual } = await format("plugins-pnpm", "index.js");
const expected = await getText("plugins-pnpm", "index.result.js");
assert.equal(actual, expected);
});
});
3 changes: 3 additions & 0 deletions test-fixtures/plugins-pnpm/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["prettier-plugin-organize-imports"]
}
2 changes: 2 additions & 0 deletions test-fixtures/plugins-pnpm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'prettier'
import { a } from './unused-module'
1 change: 1 addition & 0 deletions test-fixtures/plugins-pnpm/index.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "prettier";
15 changes: 15 additions & 0 deletions test-fixtures/plugins-pnpm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "plugins-pnpm",
"version": "1.0.0",
"description": "Test resolve plugin in pnpm",
"private": true,
"devDependencies": {
"prettier": "^2.6.2",
"prettier-plugin-organize-imports": "2.3.4"
},
"pnpm": {
"peerDependencyRules": {
"ignoreMissing": ["typescript"]
}
}
}
34 changes: 34 additions & 0 deletions test-fixtures/plugins-pnpm/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test-fixtures/plugins-pnpm/unused-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 1
3 changes: 3 additions & 0 deletions test-fixtures/test.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
{
"path": "plugins"
},
{
"path": "plugins-pnpm"
},
{
"path": "config"
},
Expand Down

0 comments on commit 22257ff

Please sign in to comment.