diff --git a/CHANGELOG.md b/CHANGELOG.md index 53cc782407..9a801031d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 @@ -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 diff --git a/package.json b/package.json index 0bc4237988..ba2fc1e36f 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/src/core/packagePath.js b/src/core/packagePath.js index a8c3c67632..ff95302017 100644 --- a/src/core/packagePath.js +++ b/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) { diff --git a/src/rules/no-extraneous-dependencies.js b/src/rules/no-extraneous-dependencies.js index 9403931f53..651f4493b4 100644 --- a/src/rules/no-extraneous-dependencies.js +++ b/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'; diff --git a/src/rules/no-relative-packages.js b/src/rules/no-relative-packages.js index 90c1ecc701..714eb3f5e3 100644 --- a/src/rules/no-relative-packages.js +++ b/src/rules/no-relative-packages.js @@ -1,5 +1,5 @@ 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'; @@ -7,7 +7,7 @@ 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, '../..')); } diff --git a/src/rules/no-unused-modules.js b/src/rules/no-unused-modules.js index 205259eef9..7e6c1f66d3 100644 --- a/src/rules/no-unused-modules.js +++ b/src/rules/no-unused-modules.js @@ -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'; @@ -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 => { diff --git a/utils/CHANGELOG.md b/utils/CHANGELOG.md index 949fa8d582..a2c273099d 100644 --- a/utils/CHANGELOG.md +++ b/utils/CHANGELOG.md @@ -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 @@ -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 @@ -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 \ No newline at end of file +[@aladdin-add]: https://github.com/aladdin-add +======= +[@mgwalker]: https://github.com/mgwalker +>>>>>>> update changelogs diff --git a/utils/package.json b/utils/package.json index 2ec00e60a4..6c3d1193ee 100644 --- a/utils/package.json +++ b/utils/package.json @@ -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" } } diff --git a/utils/readPkgUp.js b/utils/readPkgUp.js new file mode 100644 index 0000000000..7fa412bd29 --- /dev/null +++ b/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 (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 {}; + } +};