Skip to content

Commit 9e7fd82

Browse files
authored
Fix some page detection (#2965)
1 parent 21972a1 commit 9e7fd82

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

source/index.ts

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable unicorn/prefer-starts-ends-with, @typescript-eslint/prefer-string-starts-ends-with */
21
/* The tested var might not be a string */
32

43
import select from 'select-dom';
@@ -11,10 +10,10 @@ const domBased = 'skip'; // To be used only to skip tests that are DOM-based rat
1110
export const is404 = (): boolean => document.title === 'Page not found · GitHub';
1211
export const _is404 = domBased; // They're specified in page-detect.ts
1312

14-
export const is500 = (): boolean => document.title === 'Server Error · GitHub' || document.title === 'Unicorn! · GitHub';
13+
export const is500 = (): boolean => document.title === 'Server Error · GitHub' || document.title === 'Unicorn! · GitHub' || document.title === '504 Gateway Time-out';
1514
export const _is500 = domBased; // They're specified in page-detect.ts
1615

17-
export const isBlame = (): boolean => /^blame\//.test(getRepoPath()!);
16+
export const isBlame = (): boolean => String(getRepoPath()).startsWith('blame/');
1817
export const _isBlame = [
1918
'https://github.com/sindresorhus/refined-github/blame/master/package.json'
2019
];
@@ -37,7 +36,7 @@ export const _isCommitList = [
3736
'https://github.com/sindresorhus/refined-github/pull/148/commits'
3837
];
3938

40-
export const isRepoCommitList = (): boolean => /^commits\//.test(getRepoPath()!);
39+
export const isRepoCommitList = (): boolean => String(getRepoPath()).startsWith('commits/');
4140
export const _isRepoCommitList = [
4241
'https://github.com/sindresorhus/refined-github/commits/master?page=2',
4342
'https://github.com/sindresorhus/refined-github/commits/test-branch',
@@ -46,7 +45,7 @@ export const _isRepoCommitList = [
4645
'https://github.com/sindresorhus/refined-github/commits/230c2935fc5aea9a681174ddbeba6255ca040d63'
4746
];
4847

49-
export const isCompare = (): boolean => /^compare/.test(getRepoPath()!);
48+
export const isCompare = (): boolean => String(getRepoPath()).startsWith('compare');
5049
export const _isCompare = [
5150
'https://github.com/sindresorhus/refined-github/compare',
5251
'https://github.com/sindresorhus/refined-github/compare/',
@@ -126,12 +125,12 @@ export const _isMilestoneList = [
126125
'https://github.com/sindresorhus/refined-github/milestones'
127126
];
128127

129-
export const isNewIssue = (): boolean => /^issues\/new/.test(getRepoPath()!);
128+
export const isNewIssue = (): boolean => getRepoPath() === 'issues/new';
130129
export const _isNewIssue = [
131130
'https://github.com/sindresorhus/refined-github/issues/new'
132131
];
133132

134-
export const isNewRelease = (): boolean => /^releases\/new/.test(getRepoPath()!);
133+
export const isNewRelease = (): boolean => getRepoPath() === 'releases/new';
135134
export const _isNewRelease = [
136135
'https://github.com/sindresorhus/refined-github/releases/new'
137136
];
@@ -222,19 +221,15 @@ export const _isQuickPR = [
222221
'https://github.com/sindresorhus/refined-github/compare/test-branch?quick_pull=1'
223222
];
224223

225-
export const isReleasesOrTags = (): boolean => {
226-
const parts = (getRepoPath() ?? '').split('/');
227-
return /^(releases|tags)$/.test(parts[0]) && parts[1] !== 'new';
228-
};
229-
224+
export const isReleasesOrTags = (): boolean => /^tags$|^releases($|\/tag)/.test(getRepoPath()!);
230225
export const _isReleasesOrTags = [
231226
'https://github.com/sindresorhus/refined-github/releases',
232227
'https://github.com/sindresorhus/refined-github/tags',
233228
'https://github.com/sindresorhus/refined-github/releases/tag/v1.0.0-beta.4',
234229
'https://github.com/sindresorhus/refined-github/releases/tag/0.2.1'
235230
];
236231

237-
export const isEditingFile = (): boolean => /^edit/.test(getRepoPath()!);
232+
export const isEditingFile = (): boolean => String(getRepoPath()).startsWith('edit');
238233
export const _isEditingFile = [
239234
'https://github.com/sindresorhus/refined-github/edit/master/readme.md',
240235
'https://github.com/sindresorhus/refined-github/edit/ghe-injection/source/background.ts'
@@ -247,49 +242,48 @@ export const isRepo = (): boolean => /^[^/]+\/[^/]+/.test(getCleanPathname()) &&
247242
!isGist() &&
248243
!isRepoSearch();
249244
export const _isRepo = [
245+
// Some of these are here simply as "gotchas" to other detections
250246
'https://github.com/sindresorhus/refined-github/blame/master/package.json',
251247
'https://github.com/sindresorhus/refined-github/issues/146',
252248
'https://github.com/sindresorhus/notifications/',
253-
'https://github.com/sindresorhus/refined-github/pull/148'
249+
'https://github.com/sindresorhus/refined-github/pull/148',
250+
'https://github.com/sindresorhus/refined-github/milestones/new', // Gotcha for isRepoTaxonomyDiscussionList
251+
'https://github.com/sindresorhus/refined-github/milestones/1/edit', // Gotcha for isRepoTaxonomyDiscussionList
252+
'https://github.com/sindresorhus/refined-github/issues/new/choose', // Gotcha for isRepoIssueList
253+
'https://github.com/sindresorhus/refined-github/issues/templates/edit' // Gotcha for isRepoIssueList
254254
];
255255
export const _isRepoSkipNegatives = true;
256256

257+
export const isRepoTaxonomyDiscussionList = (): boolean => /^labels\/.+|^milestones\/\d+(?!\/edit)/.test(getRepoPath()!);
258+
export const _isRepoTaxonomyDiscussionList = [
259+
'https://github.com/sindresorhus/refined-github/labels/Priority%3A%20critical',
260+
'https://github.com/sindresorhus/refined-github/milestones/1'
261+
];
262+
257263
export const isRepoDiscussionList = (): boolean =>
258264
isRepoPRList() ||
259265
isRepoIssueList() ||
260-
/^(labels|milestones)\/.+/.test(getRepoPath()!);
261-
export const _isRepoDiscussionList = [
262-
'http://github.com/sindresorhus/ava/issues',
263-
'https://github.com/sindresorhus/refined-github/pulls',
264-
'https://github.com/sindresorhus/refined-github/pulls/',
265-
'https://github.com/sindresorhus/refined-github/pulls/fregante',
266-
'https://github.com/sindresorhus/refined-github/issues/fregante',
267-
'https://github.com/sindresorhus/refined-github/labels/Priority%3A%20critical',
268-
'https://github.com/sindresorhus/refined-github/milestones/1.0',
269-
'https://github.com/sindresorhus/refined-github/issues?q=is%3Aclosed+sort%3Aupdated-desc',
270-
'https://github.com/sindresorhus/refined-github/pulls?q=is%3Aopen+is%3Apr',
271-
'https://github.com/sindresorhus/refined-github/pulls?q=is%3Apr+is%3Aclosed',
272-
'https://github.com/sindresorhus/refined-github/issues'
273-
];
266+
isRepoTaxonomyDiscussionList();
267+
export const _isRepoDiscussionList = skip;
274268

275-
export const isRepoPRList = (): boolean => (getRepoPath() ?? '').startsWith('pulls');
269+
export const isRepoPRList = (): boolean => String(getRepoPath()).startsWith('pulls');
276270
export const _isRepoPRList = [
277271
'https://github.com/sindresorhus/refined-github/pulls',
278272
'https://github.com/sindresorhus/refined-github/pulls/',
279-
'https://github.com/sindresorhus/refined-github/pulls/fregante',
280273
'https://github.com/sindresorhus/refined-github/pulls?q=is%3Aopen+is%3Apr',
281274
'https://github.com/sindresorhus/refined-github/pulls?q=is%3Apr+is%3Aclosed'
282275
];
283276

284-
export const isRepoIssueList = (): boolean => {
285-
const parts = (getRepoPath() ?? '').split('/');
286-
return parts[0] === 'issues' && parts[1] !== 'new' && !/\d/.test(parts[1]); // `issues/fregante` is a list but `issues/1` isn't
287-
};
288-
277+
// `issues/fregante` is a list but `issues/1`, `issues/new`, `issues/new/choose`, `issues/templates/edit` aren’t
278+
export const isRepoIssueList = (): boolean =>
279+
String(getRepoPath()).startsWith('issues') &&
280+
!/^issues\/(\d+|new|templates)($|\/)/.test(getRepoPath()!);
289281
export const _isRepoIssueList = [
290282
'http://github.com/sindresorhus/ava/issues',
291283
'https://github.com/sindresorhus/refined-github/issues',
292284
'https://github.com/sindresorhus/refined-github/issues/fregante',
285+
'https://github.com/sindresorhus/refined-github/issues/newton',
286+
'https://github.com/sindresorhus/refined-github/issues/wptemplates',
293287
'https://github.com/sindresorhus/refined-github/issues?q=is%3Aclosed+sort%3Aupdated-desc'
294288
];
295289

@@ -311,20 +305,22 @@ export const _isRepoRoot = [
311305
'https://github.com/sindresorhus/branches'
312306
];
313307

308+
// This can't use `getRepoPath` to avoid infinite recursion.
309+
// `getRepoPath` depends on `isRepo` and `isRepo` depends on `isRepoSearch`
314310
export const isRepoSearch = (): boolean => location.pathname.slice(1).split('/')[2] === 'search';
315311
export const _isRepoSearch = [
316312
'https://github.com/sindresorhus/refined-github/search?q=diff',
317313
'https://github.com/sindresorhus/refined-github/search?q=diff&unscoped_q=diff&type=Issues',
318314
'https://github.com/sindresorhus/refined-github/search'
319315
];
320316

321-
export const isRepoSettings = (): boolean => /^settings/.test(getRepoPath()!);
317+
export const isRepoSettings = (): boolean => String(getRepoPath()).startsWith('settings');
322318
export const _isRepoSettings = [
323319
'https://github.com/sindresorhus/refined-github/settings',
324320
'https://github.com/sindresorhus/refined-github/settings/branches'
325321
];
326322

327-
export const isRepoTree = (): boolean => isRepoRoot() || /^tree\//.test(getRepoPath()!);
323+
export const isRepoTree = (): boolean => isRepoRoot() || String(getRepoPath()).startsWith('tree/');
328324
export const _isRepoTree = [
329325
'https://github.com/sindresorhus/refined-github/tree/master/distribution',
330326
'https://github.com/sindresorhus/refined-github/tree/0.13.0/distribution',
@@ -340,14 +336,14 @@ export const _isSingleCommit = [
340336
'https://github.com/sindresorhus/refined-github/commit/5b614'
341337
];
342338

343-
export const isSingleFile = (): boolean => /^blob\//.test(getRepoPath()!);
339+
export const isSingleFile = (): boolean => String(getRepoPath()).startsWith('blob/');
344340
export const _isSingleFile = [
345341
'https://github.com/sindresorhus/refined-github/blob/master/.gitattributes',
346342
'https://github.com/sindresorhus/refined-github/blob/fix-narrow-diff/distribution/content.css',
347343
'https://github.com/sindresorhus/refined-github/blob/master/edit.txt'
348344
];
349345

350-
export const isFileFinder = (): boolean => /^find\//.test(getRepoPath()!);
346+
export const isFileFinder = (): boolean => String(getRepoPath()).startsWith('find/');
351347
export const _isFileFinder = [
352348
'https://github.com/sindresorhus/refined-github/find/master'
353349
];

test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/// <reference path="../source/globals.d.ts" />
44

55
import test from 'ava';
6+
import stripIndent from 'strip-indent';
67
import './fixtures/globals';
78
import * as pageDetect from '../source/libs/page-detect';
89

@@ -36,7 +37,13 @@ for (const [key, detect] of Object.entries(pageDetect)) {
3637
for (const url of validURLs) {
3738
test(`${key} ${++i}`, t => {
3839
location.href = url;
39-
t.true(detect(), `\n${url}\nisn’t matched by ${key}() but it’s in its tests array.`);
40+
t.true(detect(), stripIndent(`
41+
Is this URL \`${key}\`?
42+
${url.replace('https://github.com', '')}
43+
44+
• Yes? The \`${key}\` test is wrong and should be fixed.
45+
• No? Remove it to the \`_${key}\` array.
46+
`));
4047
});
4148
}
4249

@@ -49,7 +56,13 @@ for (const [key, detect] of Object.entries(pageDetect)) {
4956
if (!validURLs.includes(url)) {
5057
test(`${key} ${++i}`, t => {
5158
location.href = url;
52-
t.false(detect(), `\n${url}\nis matched by ${key.replace(/^is/, '')}, but it isn’t specified in its tests array. Add it or fix the test.`);
59+
t.false(detect(), stripIndent(`
60+
Is this URL \`${key}\`?
61+
${url.replace('https://github.com', '')}
62+
63+
• Yes? Add it to the \`_${key}\` array.
64+
• No? The \`${key}\` test is wrong and should be fixed.
65+
`));
5366
});
5467
}
5568
}

0 commit comments

Comments
 (0)