Skip to content

Commit f9f6ee6

Browse files
lubiensindresorhus
authored andcommitted
Handle lack of package.json in expiring-todo-comments rule (#397)
1 parent 3cea922 commit f9f6ee6

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

rules/expiring-todo-comments.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const MESSAGE_ID_ENGINE_MATCHES = 'engineMatches';
1717
const MESSAGE_ID_REMOVE_WHITESPACES = 'removeWhitespaces';
1818
const MESSAGE_ID_MISSING_AT_SYMBOL = 'missingAtSymbol';
1919

20-
const pkg = readPkgUp.sync().package;
20+
const hasPackage = readPkgUp.sync();
21+
const pkg = hasPackage ? hasPackage.package : {};
2122

2223
const pkgDependencies = {
2324
...pkg.dependencies,
@@ -68,7 +69,7 @@ function parseArgument(argumentString) {
6869
};
6970
}
7071

71-
if (DEPENDENCY_INCLUSION_RE.test(argumentString)) {
72+
if (hasPackage && DEPENDENCY_INCLUSION_RE.test(argumentString)) {
7273
const condition = argumentString[0] === '+' ? 'in' : 'out';
7374
const name = argumentString.slice(1).trim();
7475

@@ -81,7 +82,7 @@ function parseArgument(argumentString) {
8182
};
8283
}
8384

84-
if (VERSION_COMPARISON_RE.test(argumentString)) {
85+
if (hasPackage && VERSION_COMPARISON_RE.test(argumentString)) {
8586
const result = VERSION_COMPARISON_RE.exec(argumentString);
8687
const name = result[1].trim();
8788
const condition = result[2].trim();
@@ -112,7 +113,7 @@ function parseArgument(argumentString) {
112113
}
113114
}
114115

115-
if (PKG_VERSION_RE.test(argumentString)) {
116+
if (hasPackage && PKG_VERSION_RE.test(argumentString)) {
116117
const result = PKG_VERSION_RE.exec(argumentString);
117118
const condition = result[1].trim();
118119
const version = result[2].trim();
@@ -173,11 +174,7 @@ function semverComparisonForOperator(operator) {
173174

174175
const create = context => {
175176
const options = {
176-
terms: [
177-
'todo',
178-
'fixme',
179-
'xxx'
180-
],
177+
terms: ['todo', 'fixme', 'xxx'],
181178
ignoreDatesOnPullRequests: true,
182179
allowWarningComments: false,
183180
...context.options[0]
@@ -347,9 +344,7 @@ const create = context => {
347344
loc: comment.loc,
348345
messageId: MESSAGE_ID_VERSION_MATCHES,
349346
data: {
350-
comparison: `${dependency.name} ${dependency.condition} ${
351-
dependency.version
352-
}`,
347+
comparison: `${dependency.name} ${dependency.condition} ${dependency.version}`,
353348
message: parseTodoMessage(comment.value)
354349
}
355350
});
@@ -394,7 +389,10 @@ const create = context => {
394389
const comparisonIndex = unknown.indexOf('>');
395390

396391
if (!hasAt && comparisonIndex !== -1) {
397-
const testString = `${unknown.slice(0, comparisonIndex)}@${unknown.slice(comparisonIndex)}`;
392+
const testString = `${unknown.slice(
393+
0,
394+
comparisonIndex
395+
)}@${unknown.slice(comparisonIndex)}`;
398396

399397
if (parseArgument(testString).type !== 'unknowns') {
400398
uses++;

0 commit comments

Comments
 (0)