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

Handle lack of package.json in expiring-todo-comments rule #397

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Changes from all 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
24 changes: 11 additions & 13 deletions rules/expiring-todo-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const MESSAGE_ID_ENGINE_MATCHES = 'engineMatches';
const MESSAGE_ID_REMOVE_WHITESPACES = 'removeWhitespaces';
const MESSAGE_ID_MISSING_AT_SYMBOL = 'missingAtSymbol';

const pkg = readPkgUp.sync().package;
const hasPackage = readPkgUp.sync();
const pkg = hasPackage ? hasPackage.package : {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This two line seems weird.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first thinking

const {package: pkg = {}} = readPkgUp.sync() || {};
const hasPackage = ??? // can't detect

next might better, but extra line

const result= readPkgUp.sync();
const hasPackage = Boolean(result)
const pkg = hasPackage ? result.package : {};

or maybe this one

const {
  package: pkg = {}, 
  hasPackage = true
} = readPkgUp.sync() || {hasPackage: false}

@lubien what do you think

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I just needed to get this out.

Should probably have been:

const packageResult = readPkgUp.sync();
const hasPackage = Boolean(packageResult);
const packageJson = hasPackage ? packageResult.package : {};

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we comment at the same time? LOL
what do you think my last code

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe. Seems so. I didn't see your comment until now. Let's go with mine, which is your second one, just with more verbose naming.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @lubien Could you please send another PR to apply this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! But I need a few hours before I can do this if it's not an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const pkgDependencies = {
...pkg.dependencies,
Expand Down Expand Up @@ -68,7 +69,7 @@ function parseArgument(argumentString) {
};
}

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

Expand All @@ -81,7 +82,7 @@ function parseArgument(argumentString) {
};
}

if (VERSION_COMPARISON_RE.test(argumentString)) {
if (hasPackage && VERSION_COMPARISON_RE.test(argumentString)) {
const result = VERSION_COMPARISON_RE.exec(argumentString);
const name = result[1].trim();
const condition = result[2].trim();
Expand Down Expand Up @@ -112,7 +113,7 @@ function parseArgument(argumentString) {
}
}

if (PKG_VERSION_RE.test(argumentString)) {
if (hasPackage && PKG_VERSION_RE.test(argumentString)) {
const result = PKG_VERSION_RE.exec(argumentString);
const condition = result[1].trim();
const version = result[2].trim();
Expand Down Expand Up @@ -173,11 +174,7 @@ function semverComparisonForOperator(operator) {

const create = context => {
const options = {
terms: [
'todo',
'fixme',
'xxx'
],
terms: ['todo', 'fixme', 'xxx'],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, don't do unrelated changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. Might have been the linter but might bad for leaving it, sorry. Won't happen again.

ignoreDatesOnPullRequests: true,
allowWarningComments: false,
...context.options[0]
Expand Down Expand Up @@ -347,9 +344,7 @@ const create = context => {
loc: comment.loc,
messageId: MESSAGE_ID_VERSION_MATCHES,
data: {
comparison: `${dependency.name} ${dependency.condition} ${
dependency.version
}`,
comparison: `${dependency.name} ${dependency.condition} ${dependency.version}`,
message: parseTodoMessage(comment.value)
}
});
Expand Down Expand Up @@ -394,7 +389,10 @@ const create = context => {
const comparisonIndex = unknown.indexOf('>');

if (!hasAt && comparisonIndex !== -1) {
const testString = `${unknown.slice(0, comparisonIndex)}@${unknown.slice(comparisonIndex)}`;
const testString = `${unknown.slice(
0,
comparisonIndex
)}@${unknown.slice(comparisonIndex)}`;

if (parseArgument(testString).type !== 'unknowns') {
uses++;
Expand Down