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

feat: PnP package resolution support #273

Merged
merged 10 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
dist
coverage
.pnp.*
.yarn
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ coverage
/.vscode-test/**
/test/**/*.css
/package.json
.pnp.*
.yarn
# Unignore config files like .prettierrc.js, because they're ignored by default
!.*rc.js
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]

### Added

- If Stylelint is installed in a workspace using [Yarn](https://yarnpkg.com/) with [Plug-n-Play](https://yarnpkg.com/features/pnp), it can now be resolved without the need for Yarn's [editor SDKs](https://yarnpkg.com/getting-started/editor-sdks). ([#273](https://github.com/stylelint/vscode-stylelint/issues/273)).

### Changed

- The extension no longer blocks VS Code's startup. Thanks to [@robole](https://github.com/robole) for the idea. ([#257](https://github.com/stylelint/vscode-stylelint/pull/257))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`findPackageRoot should throw when encountering a file system error other than ENOENT 1`] = `"EACCES: permission denied, open 'foo/bar/baz'"`;
exports[`findPackageRoot with custom root file should throw when encountering a file system error other than ENOENT 1`] = `"EACCES: permission denied, open 'foo/bar/baz'"`;

exports[`findPackageRoot with defaults should throw when encountering a file system error other than ENOENT 1`] = `"EACCES: permission denied, open 'foo/bar/baz'"`;
199 changes: 152 additions & 47 deletions src/utils/packages/__tests__/find-package-root.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,181 @@ const mockedFS = /** @type {tests.mocks.FSPromisesModule} */ (require('fs/promis
const findPackageRoot = require('../find-package-root').findPackageRoot;

describe('findPackageRoot', () => {
it('should resolve the package directory when package.json is present', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
'package.json': '{}',
baz: {},
describe('with defaults', () => {
it('should resolve the package directory when package.json is present', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
'package.json': '{}',
baz: {},
},
},
},
});

expect(await findPackageRoot('foo/bar/baz')).toBe('foo/bar');
});

expect(await findPackageRoot('foo/bar/baz')).toBe('foo/bar');
});
it('should resolve the package directory when the starting path points to a file', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
'package.json': '{}',
baz: '',
},
},
});

it("should not resolve when package.json isn't in the tree", async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: {},
expect(await findPackageRoot('foo/bar/baz')).toBe('foo/bar');
});

it("should not resolve when package.json isn't in the tree", async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: {},
},
},
},
});

expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();
});

expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();
});
it('should not resolve folders with a directory named package.json', async () => {
mockedFS.__mockFileSystem({
foo: {
'package.json': '{}',
bar: {
'package.json': {},
baz: {},
},
},
});

expect(await findPackageRoot('foo/bar/baz')).toBe('foo');

it('should not resolve folders with a directory named package.json', async () => {
mockedFS.__mockFileSystem({
foo: {
'package.json': '{}',
bar: {
mockedFS.__mockFileSystem({
foo: {
'package.json': {},
baz: {},
bar: {
baz: {},
},
},
},
});
});

expect(await findPackageRoot('foo/bar/baz')).toBe('foo');
expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();

mockedFS.__mockFileSystem({
foo: {
mockedFS.__mockFileSystem({
'package.json': {},
bar: {
baz: {},
foo: {
bar: {
baz: {},
},
},
});

expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();
});

it('should throw when encountering a file system error other than ENOENT', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: createError('EACCES', 'foo/bar/baz', -13, 'stat'),
},
},
});

await expect(findPackageRoot('foo/bar/baz')).rejects.toThrowErrorMatchingSnapshot();
});
});

describe('with custom root file', () => {
it('should resolve the package directory when the root file is present', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
'yarn.lock': '',
baz: {},
},
},
},
});

expect(await findPackageRoot('foo/bar/baz', 'yarn.lock')).toBe('foo/bar');
});

expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();
it('should resolve the package directory when the starting path points to a file', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
'yarn.lock': '',
baz: '',
},
},
});

mockedFS.__mockFileSystem({
'package.json': {},
foo: {
bar: {
baz: {},
expect(await findPackageRoot('foo/bar/baz', 'yarn.lock')).toBe('foo/bar');
});

it("should not resolve when the root file isn't in the tree", async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: {},
},
},
},
});

expect(await findPackageRoot('foo/bar/baz', 'yarn.lock')).toBeUndefined();
});

expect(await findPackageRoot('foo/bar/baz')).toBeUndefined();
});
it('should not resolve folders with a directory with the same name as the root file', async () => {
mockedFS.__mockFileSystem({
foo: {
'yarn.lock': '',
bar: {
'yarn.lock': {},
baz: {},
},
},
});

expect(await findPackageRoot('foo/bar/baz', 'yarn.lock')).toBe('foo');

mockedFS.__mockFileSystem({
foo: {
'yarn.lock': {},
bar: {
baz: {},
},
},
});

expect(await findPackageRoot('foo/bar/baz', 'yarn.lock')).toBeUndefined();

it('should throw when encountering a file system error other than ENOENT', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: createError('EACCES', 'foo/bar/baz', -13, 'stat'),
mockedFS.__mockFileSystem({
'yarn.lock': {},
foo: {
bar: {
baz: {},
},
},
},
});

expect(await findPackageRoot('foo/bar/baz', 'yarn.lock')).toBeUndefined();
});

await expect(findPackageRoot('foo/bar/baz')).rejects.toThrowErrorMatchingSnapshot();
it('should throw when encountering a file system error other than ENOENT', async () => {
mockedFS.__mockFileSystem({
foo: {
bar: {
baz: createError('EACCES', 'foo/bar/baz', -13, 'stat'),
},
},
});

await expect(
findPackageRoot('foo/bar/baz', 'yarn.lock'),
).rejects.toThrowErrorMatchingSnapshot();
});
});
});
Loading