Skip to content

Commit 756dbbf

Browse files
beeequeuesindresorhusfisker
authored
Replace read-package-up with find-up-simple (#2619)
Co-authored-by: Sindre Sorhus <[email protected]> Co-authored-by: fisker Cheung <[email protected]>
1 parent 9e50568 commit 756dbbf

File tree

5 files changed

+69
-43
lines changed

5 files changed

+69
-43
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@
6464
"clean-regexp": "^1.0.0",
6565
"core-js-compat": "^3.41.0",
6666
"esquery": "^1.6.0",
67+
"find-up-simple": "^1.0.1",
6768
"globals": "^16.0.0",
6869
"indent-string": "^5.0.0",
6970
"is-builtin-module": "^5.0.0",
7071
"jsesc": "^3.1.0",
7172
"pluralize": "^8.0.0",
72-
"read-package-up": "^11.0.0",
7373
"regexp-tree": "^0.1.27",
7474
"regjsparser": "^0.12.0",
7575
"semver": "^7.7.1",

Diff for: rules/expiring-todo-comments.js

+5-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import path from 'node:path';
22
import {isRegExp} from 'node:util/types';
3-
import {readPackageUpSync} from 'read-package-up';
43
import semver from 'semver';
54
import * as ci from 'ci-info';
65
import getBuiltinRule from './utils/get-builtin-rule.js';
6+
import {readPackageJson} from './shared/package-json.js';
77

88
const baseRule = getBuiltinRule('no-warning-comments');
99

@@ -50,20 +50,9 @@ const messages = {
5050

5151
/** @param {string} dirname */
5252
function getPackageHelpers(dirname) {
53-
// We don't need to normalize the package.json data, because we are only using 2 properties and those 2 properties
54-
// aren't validated by the normalization. But when this plugin is used in a monorepo, the name field in the
55-
// package.json can be invalid and would make this plugin throw an error. See also #1871
56-
/** @type {readPkgUp.ReadResult | undefined} */
57-
let packageResult;
58-
try {
59-
packageResult = readPackageUpSync({normalize: false, cwd: dirname});
60-
} catch {
61-
// This can happen if package.json files have comments in them etc.
62-
packageResult = undefined;
63-
}
64-
65-
const hasPackage = Boolean(packageResult);
66-
const packageJson = packageResult ? packageResult.packageJson : {};
53+
const packageJsonResult = readPackageJson(dirname);
54+
const packageJson = packageJsonResult?.packageJson ?? {};
55+
const hasPackage = Boolean(packageJsonResult);
6756

6857
const packageDependencies = {
6958
...packageJson.dependencies,
@@ -187,7 +176,7 @@ function getPackageHelpers(dirname) {
187176
}
188177

189178
return {
190-
packageResult,
179+
packageResult: packageJsonResult,
191180
hasPackage,
192181
packageJson,
193182
packageDependencies,

Diff for: rules/no-unnecessary-polyfills.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import path from 'node:path';
2-
import {readPackageUpSync} from 'read-package-up';
32
import coreJsCompat from 'core-js-compat';
43
import {camelCase} from './utils/lodash.js';
54
import isStaticRequire from './ast/is-static-require.js';
5+
import {readPackageJson} from './shared/package-json.js';
66

77
const {data: compatData, entries: coreJsEntries} = coreJsCompat;
88

@@ -57,18 +57,13 @@ function getTargets(options, dirname) {
5757
return options.targets;
5858
}
5959

60-
/** @type {readPkgUp.ReadResult | undefined} */
61-
let packageResult;
62-
try {
63-
// It can fail if, for example, the package.json file has comments.
64-
packageResult = readPackageUpSync({normalize: false, cwd: dirname});
65-
} catch {}
60+
const packageJsonResult = readPackageJson(dirname);
6661

67-
if (!packageResult) {
62+
if (!packageJsonResult) {
6863
return;
6964
}
7065

71-
const {browserslist, engines} = packageResult.packageJson;
66+
const {browserslist, engines} = packageJsonResult.packageJson;
7267
return browserslist ?? engines;
7368
}
7469

Diff for: rules/shared/package-json.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import fs from 'node:fs';
2+
import {findUpSync} from 'find-up-simple';
3+
4+
const directoryCache = new Map();
5+
const dataCache = new Map();
6+
7+
/**
8+
Finds the closest package.json file to the given directory and returns its path and contents.
9+
10+
Caches the result for future lookups.
11+
12+
@param dirname {string}
13+
@return {{ path: string, packageJson: Record<string, unknown> } | undefined}
14+
*/
15+
export function readPackageJson(dirname) {
16+
let packageJsonPath;
17+
if (directoryCache.has(dirname)) {
18+
packageJsonPath = directoryCache.get(dirname);
19+
} else {
20+
packageJsonPath = findUpSync('package.json', {cwd: dirname, type: 'file'});
21+
directoryCache.set(dirname, packageJsonPath);
22+
}
23+
24+
if (!packageJsonPath) {
25+
return;
26+
}
27+
28+
let packageJson;
29+
if (dataCache.has(packageJsonPath)) {
30+
packageJson = dataCache.get(packageJsonPath);
31+
} else {
32+
try {
33+
packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
34+
dataCache.set(packageJsonPath, packageJson);
35+
} catch {
36+
// This can happen if package.json files have comments in them etc.
37+
return;
38+
}
39+
}
40+
41+
return {path: packageJsonPath, packageJson};
42+
}

Diff for: test/expiring-todo-comments.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ test({
6060
},
6161
'// Expire Condition [2000-01-01]: new term name',
6262
'// TODO [>2000]: We sure didnt past this version',
63-
'// TODO [-read-package-up]: We actually use this.',
63+
'// TODO [-find-up-simple]: We actually use this.',
6464
'// TODO [+popura]: I think we wont need a broken package.',
6565
'// TODO [semver@>1000]: Welp hopefully we wont get at that.',
6666
'// TODO [semver@>=1000]: Welp hopefully we wont get at that.',
6767
'// TODO [@lubien/fixture-beta-package@>=1.0.0]: we are using a pre-release',
6868
'// TODO [@lubien/fixture-beta-package@>=1.0.0-gamma.1]: beta comes first from gamma',
6969
'// TODO [@lubien/fixture-beta-package@>=1.0.0-beta.2]: we are in beta.1',
70-
'// TODO [2200-12-12, -read-package-up]: Combo',
71-
'// TODO [2200-12-12, -read-package-up, +popura]: Combo',
72-
'// TODO [2200-12-12, -read-package-up, +popura, semver@>=1000]: Combo',
70+
'// TODO [2200-12-12, -find-up-simple]: Combo',
71+
'// TODO [2200-12-12, -find-up-simple, +popura]: Combo',
72+
'// TODO [2200-12-12, -find-up-simple, +popura, semver@>=1000]: Combo',
7373
'// TODO [engine:node@>=100]: When we start supporting only >= 10',
7474
`// TODO [2200-12-12]: Multiple
7575
// TODO [2200-12-12]: Lines`,
@@ -209,24 +209,24 @@ test({
209209
errors: [reachedPackageVersionError('>=1', 'if your package.json version is >=1')],
210210
},
211211
{
212-
code: '// TODO [+read-package-up]: when you install `read-package-up`',
213-
errors: [havePackageError('read-package-up', 'when you install `read-package-up`')],
212+
code: '// TODO [+find-up-simple]: when you install `find-up-simple`',
213+
errors: [havePackageError('find-up-simple', 'when you install `find-up-simple`')],
214214
},
215215
{
216216
code: '// TODO [-popura]: when you uninstall `popura`',
217217
errors: [dontHavePackageError('popura', 'when you uninstall `popura`')],
218218
},
219219
{
220-
code: '// TODO [read-package-up@>1]: when `read-package-up` version is > 1',
221-
errors: [versionMatchesError('read-package-up > 1', 'when `read-package-up` version is > 1')],
220+
code: '// TODO [find-up-simple@>=1]: when `find-up-simple` version is >= 1',
221+
errors: [versionMatchesError('find-up-simple >= 1', 'when `find-up-simple` version is >= 1')],
222222
},
223223
{
224224
code: '// TODO [engine:node@>=8]: when support is for node >= 8',
225225
errors: [engineMatchesError('node>=8', 'when support is for node >= 8')],
226226
},
227227
{
228-
code: '// TODO [read-package-up@>=5.1.1]: when `read-package-up` version is >= 5.1.1',
229-
errors: [versionMatchesError('read-package-up >= 5.1.1', 'when `read-package-up` version is >= 5.1.1')],
228+
code: '// TODO [find-up-simple@>0.2.0]: when `find-up-simple` version is > 0.2.0',
229+
errors: [versionMatchesError('find-up-simple > 0.2.0', 'when `find-up-simple` version is > 0.2.0')],
230230
},
231231
{
232232
code: '// TODO [@lubien/fixture-beta-package@>=1.0.0-alfa.1]: when `@lubien/fixture-beta-package` version is >= 1.0.0-alfa.1',
@@ -323,16 +323,16 @@ test({
323323
],
324324
},
325325
{
326-
code: '// TODO [-popura, read-package-up@>1]: Combine not having a package with version match',
326+
code: '// TODO [-popura, find-up-simple@>=1]: Combine not having a package with version match',
327327
errors: [
328328
dontHavePackageError('popura', 'Combine not having a package with version match'),
329-
versionMatchesError('read-package-up > 1', 'Combine not having a package with version match'),
329+
versionMatchesError('find-up-simple >= 1', 'Combine not having a package with version match'),
330330
],
331331
},
332332
{
333-
code: '// TODO [+read-package-up, -popura]: Combine presence/absence of packages',
333+
code: '// TODO [+find-up-simple, -popura]: Combine presence/absence of packages',
334334
errors: [
335-
havePackageError('read-package-up', 'Combine presence/absence of packages'),
335+
havePackageError('find-up-simple', 'Combine presence/absence of packages'),
336336
dontHavePackageError('popura', 'Combine presence/absence of packages'),
337337
],
338338
},
@@ -352,13 +352,13 @@ test({
352352
],
353353
},
354354
{
355-
code: '// HUGETODO [semver @>=1, engine:node@>=8, 2000-01-01, -popura, >1, +read-package-up, read-package-up@>1]: Big mix',
355+
code: '// HUGETODO [semver @>=1, engine:node@>=8, 2000-01-01, -popura, >1, +find-up-simple, find-up-simple@>=1]: Big mix',
356356
errors: [
357357
expiredTodoError('2000-01-01', 'Big mix'),
358358
reachedPackageVersionError('>1', 'Big mix'),
359359
dontHavePackageError('popura', 'Big mix'),
360-
havePackageError('read-package-up', 'Big mix'),
361-
versionMatchesError('read-package-up > 1', 'Big mix'),
360+
havePackageError('find-up-simple', 'Big mix'),
361+
versionMatchesError('find-up-simple >= 1', 'Big mix'),
362362
engineMatchesError('node>=8', 'Big mix'),
363363
removeWhitespaceError('semver @>=1', 'Big mix'),
364364
],

0 commit comments

Comments
 (0)