Skip to content

Commit

Permalink
remove dependency on read-pkg-up
Browse files Browse the repository at this point in the history
  • Loading branch information
mgwalker authored and ljharb committed Aug 4, 2021
1 parent 00d7bc8 commit 6e9723d
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [Docs] `extensions`: removed incorrect cases ([#2138], thanks [@wenfangdu])
- [Tests] `order`: add tests for `pathGroupsExcludedImportTypes: ['type']` ([#2158], thanks [@atav32])
- [Docs] `order`: improve the documentation for the `pathGroupsExcludedImportTypes` option ([#2156], thanks [@liby])
- switched to an internal replacement for `read-pkg-up` ([#2047], [@mgwalker])

## [2.23.4] - 2021-05-29

Expand Down Expand Up @@ -825,6 +826,7 @@ for info on changes for earlier releases.
[#2083]: https://github.com/benmosher/eslint-plugin-import/pull/2083
[#2075]: https://github.com/benmosher/eslint-plugin-import/pull/2075
[#2071]: https://github.com/benmosher/eslint-plugin-import/pull/2071
[#2047]: https://github.com/benmosher/eslint-plugin-import/pull/2047
[#2034]: https://github.com/benmosher/eslint-plugin-import/pull/2034
[#2028]: https://github.com/benmosher/eslint-plugin-import/pull/2028
[#2026]: https://github.com/benmosher/eslint-plugin-import/pull/2026
Expand Down Expand Up @@ -1371,6 +1373,7 @@ for info on changes for earlier releases.
[@mattijsbliek]: https://github.com/mattijsbliek
[@Maxim-Mazurok]: https://github.com/Maxim-Mazurok
[@maxmalov]: https://github.com/maxmalov
[@mgwalker]: https://github.com/mgwalker
[@MikeyBeLike]: https://github.com/MikeyBeLike
[@mplewis]: https://github.com/mplewis
[@nickofthyme]: https://github.com/nickofthyme
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -112,7 +112,6 @@
"minimatch": "^3.0.4",
"object.values": "^1.1.3",
"pkg-up": "^2.0.0",
"read-pkg-up": "^3.0.0",
"resolve": "^1.20.0",
"tsconfig-paths": "^3.9.0"
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/packagePath.js
@@ -1,6 +1,6 @@
import { dirname } from 'path';
import findUp from 'find-up';
import readPkgUp from 'read-pkg-up';
import readPkgUp from 'eslint-module-utils/readPkgUp';


export function getContextPackagePath(context) {
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-extraneous-dependencies.js
@@ -1,6 +1,6 @@
import path from 'path';
import fs from 'fs';
import readPkgUp from 'read-pkg-up';
import readPkgUp from 'eslint-module-utils/readPkgUp';
import minimatch from 'minimatch';
import resolve from 'eslint-module-utils/resolve';
import moduleVisitor from 'eslint-module-utils/moduleVisitor';
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-relative-packages.js
@@ -1,13 +1,13 @@
import path from 'path';
import readPkgUp from 'read-pkg-up';
import readPkgUp from 'eslint-module-utils/readPkgUp';

import resolve from 'eslint-module-utils/resolve';
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';
import importType from '../core/importType';
import docsUrl from '../docsUrl';

function findNamedPackage(filePath) {
const found = readPkgUp.sync({ cwd: filePath, normalize: false });
const found = readPkgUp({ cwd: filePath });
if (found.pkg && !found.pkg.name) {
return findNamedPackage(path.join(found.path, '../..'));
}
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-unused-modules.js
Expand Up @@ -9,7 +9,7 @@ import { getFileExtensions } from 'eslint-module-utils/ignore';
import resolve from 'eslint-module-utils/resolve';
import docsUrl from '../docsUrl';
import { dirname, join } from 'path';
import readPkgUp from 'read-pkg-up';
import readPkgUp from 'eslint-module-utils/readPkgUp';
import values from 'object.values';
import includes from 'array-includes';

Expand Down Expand Up @@ -332,7 +332,7 @@ const newDefaultImportExists = specifiers =>
specifiers.some(({ type }) => type === IMPORT_DEFAULT_SPECIFIER);

const fileIsInPkg = file => {
const { path, pkg } = readPkgUp.sync({ cwd: file, normalize: false });
const { path, pkg } = readPkgUp({ cwd: file });
const basePath = dirname(path);

const checkPkgFieldString = pkgField => {
Expand Down
13 changes: 12 additions & 1 deletion utils/CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## Unreleased

### Added
[New] Added a slim replacement for `read-pkg-up` ([#2047], [@mgwalker])

## v2.6.1 - 2021-05-13

### Fixed
Expand Down Expand Up @@ -85,8 +88,12 @@ Yanked due to critical issue with cache key resulting from #839.
### Fixed
- `unambiguous.test()` regex is now properly in multiline mode

<<<<<<< HEAD
[#2026]: https://github.com/benmosher/eslint-plugin-import/pull/2026
[#1786]: https://github.com/benmosher/eslint-plugin-import/pull/1786
=======
[#2047]: https://github.com/benmosher/eslint-plugin-import/pull/2047
>>>>>>> update changelogs
[#1671]: https://github.com/benmosher/eslint-plugin-import/pull/1671
[#1606]: https://github.com/benmosher/eslint-plugin-import/pull/1606
[#1602]: https://github.com/benmosher/eslint-plugin-import/pull/1602
Expand All @@ -113,5 +120,9 @@ Yanked due to critical issue with cache key resulting from #839.
[@sompylasar]: https://github.com/sompylasar
[@iamnapo]: https://github.com/iamnapo
[@kaiyoma]: https://github.com/kaiyoma
<<<<<<< HEAD
[@manuth]: https://github.com/manuth
[@aladdin-add]: https://github.com/aladdin-add
[@aladdin-add]: https://github.com/aladdin-add
=======
[@mgwalker]: https://github.com/mgwalker
>>>>>>> update changelogs
1 change: 1 addition & 0 deletions utils/package.json
Expand Up @@ -27,6 +27,7 @@
"homepage": "https://github.com/benmosher/eslint-plugin-import#readme",
"dependencies": {
"debug": "^3.2.7",
"find-up": "^2.1.0",
"pkg-dir": "^2.0.0"
}
}
52 changes: 52 additions & 0 deletions utils/readPkgUp.js
@@ -0,0 +1,52 @@
'use strict';
exports.__esModule = true;

const fs = require('fs');
const findUp = require('find-up');

function stripBOM(str) {
return str.replace(/^\uFEFF/, '');
}

/**
* Derived significantly from read-pkg-up@2.0.0. See license below.
*
* @copyright Sindre Sorhus
* MIT License
*
* Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
exports.default = function readPkgUp(opts) {
const fp = findUp.sync('package.json', opts);

if (!fp) {
return {};
}

try {
return {
pkg: JSON.parse(stripBOM(fs.readFileSync(fp, { encoding: 'utf-8' }))),
path: fp,
};
} catch (e) {
return {};
}
};

0 comments on commit 6e9723d

Please sign in to comment.