Skip to content

Commit 9f68284

Browse files
committed
Make eslint config stricter and fix subsequent issues
Eslint is now particular about indentation, quotes, and spacing.
1 parent 596efe1 commit 9f68284

24 files changed

+253
-232
lines changed

‎__tests__/background.ts‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ describe('translators', () => {
1717
describe('googleTranslate', () => {
1818
it('translates', async () => {
1919
const translation = await googleTranslate(
20-
'Hello',
21-
{
22-
from: 'en',
23-
to: 'de',
24-
},
20+
'Hello',
21+
{
22+
from: 'en',
23+
to: 'de',
24+
},
2525
);
2626
expect(translation).toEqual('Hallo');
2727
});
@@ -52,11 +52,11 @@ describe('translators', () => {
5252

5353
it('translates', async () => {
5454
const translation = await bingTranslate(
55-
'Hello',
56-
{
57-
from: 'en',
58-
to: 'de',
59-
},
55+
'Hello',
56+
{
57+
from: 'en',
58+
to: 'de',
59+
},
6060
);
6161
expect(translation).toEqual('Hallo');
6262
});
@@ -66,27 +66,27 @@ describe('translators', () => {
6666
it('translates sentence with lots of \'i\'s', async () => {
6767
await expect(Promise.all([
6868
deepL(
69-
'iguanas ingest insects',
70-
{from: 'EN', to: 'ZH'},
69+
'iguanas ingest insects',
70+
{from: 'EN', to: 'ZH'},
7171
),
7272
deepL(
73-
'icy igloos invite imagination',
74-
{from: 'EN', to: 'FR'},
73+
'icy igloos invite imagination',
74+
{from: 'EN', to: 'FR'},
7575
),
7676
deepL(
77-
'incredible iguanas illuminate intricate, imaginary islands',
78-
{from: 'EN', to: 'PL'},
77+
'incredible iguanas illuminate intricate, imaginary islands',
78+
{from: 'EN', to: 'PL'},
7979
),
8080
])).resolves.toBeTruthy();
8181
});
8282

8383
it('translates', async () => {
8484
const translation = await deepL(
85-
'Hello',
86-
{
87-
from: 'EN',
88-
to: 'DE',
89-
},
85+
'Hello',
86+
{
87+
from: 'EN',
88+
to: 'DE',
89+
},
9090
);
9191
expect(translation).toEqual('Hallo');
9292
});

‎__tests__/content.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ describe('translateWord', () => {
9595
it('only sends one request for one word sentence', async () => {
9696
const sendMessageSpy = jest.spyOn(global.chrome.runtime, 'sendMessage');
9797
await translateWord(
98-
['Cześć!'],
99-
0,
100-
{
101-
name: 'polish',
102-
defaultOutOfContextTranslator: 'bing',
103-
defaultInContextTranslator: 'bing',
104-
bingCode: 'pl',
105-
},
98+
['Cześć!'],
99+
0,
100+
{
101+
name: 'polish',
102+
defaultOutOfContextTranslator: 'bing',
103+
defaultInContextTranslator: 'bing',
104+
bingCode: 'pl',
105+
},
106106
);
107107
expect(sendMessageSpy).toHaveBeenCalledTimes(1);
108108
});

‎babel.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
],
66
env: {
77
test: {
8-
plugins: ["@babel/plugin-transform-modules-commonjs"],
8+
plugins: ['@babel/plugin-transform-modules-commonjs'],
99
},
1010
},
1111
};

‎background/index.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import notifyAboutURLChange from './lib/notifyAboutURLChange';
1616
chrome.runtime.onMessage.addListener(respondToTranslationRequest);
1717

1818
chrome.webNavigation.onHistoryStateUpdated.addListener(
19-
notifyAboutURLChange,
20-
// Unfortunately, there doesn't seem to be a way to limit the
21-
// "webNavigation" permission to specific hostnames only. Therefore, the
22-
// background script is notified about all URL changes, and we need to
23-
// filter out those which happen in non-YouTube tabs.
24-
{url: [{hostSuffix: '.youtube.com'}]},
19+
notifyAboutURLChange,
20+
// Unfortunately, there doesn't seem to be a way to limit the
21+
// "webNavigation" permission to specific hostnames only. Therefore, the
22+
// background script is notified about all URL changes, and we need to
23+
// filter out those which happen in non-YouTube tabs.
24+
{url: [{hostSuffix: '.youtube.com'}]},
2525
);

‎background/lib/notifyAboutURLChange.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export default function notifyAboutURLChange(
77
eventDetails: { tabId: number; url: string }
88
) {
99
chrome.tabs.sendMessage(
10-
eventDetails.tabId,
11-
{
12-
url: eventDetails.url,
13-
},
10+
eventDetails.tabId,
11+
{
12+
url: eventDetails.url,
13+
},
1414
);
1515
}

‎background/lib/respondToTranslationRequest.ts‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,26 @@ export default function respondToTranslationRequest(
6767
// promise is fulfilled. When an error occurs, it is 'JSON serialized'
6868
// before it is sent.
6969
translationPromise
70-
.then((translation) => {
71-
sendResponse({
72-
type: 'translation',
73-
translation,
74-
});
75-
})
76-
.catch((error: Error & { code?: string }) => {
77-
console.error('An error occurred while attempting to translate ' +
70+
.then((translation) => {
71+
sendResponse({
72+
type: 'translation',
73+
translation,
74+
});
75+
})
76+
.catch((error: Error & { code?: string }) => {
77+
console.error('An error occurred while attempting to translate ' +
7878
`"${text}":\n`, error);
79-
// Delete entry from `dict`. This prevents e.g. a temporary lack of
80-
// internet connection from returning an error even when the
81-
// connection is restored.
82-
delete dicts[dictName][text];
83-
sendResponse({
84-
// 'JSON serialize' error before sending.
85-
type: 'error',
86-
name: error.name,
87-
message: error.message,
88-
});
79+
// Delete entry from `dict`. This prevents e.g. a temporary lack of
80+
// internet connection from returning an error even when the
81+
// connection is restored.
82+
delete dicts[dictName][text];
83+
sendResponse({
84+
// 'JSON serialize' error before sending.
85+
type: 'error',
86+
name: error.name,
87+
message: error.message,
8988
});
89+
});
9090
};
9191

9292
// Translation already cached?

‎background/lib/translators/bingTranslate.ts‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function fetchAuthData(): Promise<Record<string, string>> {
2525
authData.ig = translatePage.match(/IG:"(.*?)"/)?.[1] ?? '';
2626
authData.iid = translatePage.match(/data-iid="(.*?)"/)?.[1] ?? '';
2727
const abusePreventionHelper = JSON.parse(
28-
translatePage.match(/params_AbusePreventionHelper = (.*?);/)?.[1]
28+
translatePage.match(/params_AbusePreventionHelper = (.*?);/)?.[1]
2929
??
3030
'{}');
3131
authData.token = abusePreventionHelper[1];
@@ -39,12 +39,12 @@ async function fetchAuthData(): Promise<Record<string, string>> {
3939
* Obtain authentication data for the Bing Translate API from storage if it has
4040
* previously been stored, otherwise fetch authentication data and store it.
4141
*/
42-
async function getAuthData(refresh=false): Promise<Record<string, string>> {
42+
async function getAuthData(refresh = false): Promise<Record<string, string>> {
4343
const authDataStorageKey = 'bingTranslateAuthData';
4444
let authData = await getStoredData(authDataStorageKey) as
4545
Record<string, string> | undefined;
4646
if (!authData || refresh) {
47-
authData= await fetchAuthData();
47+
authData = await fetchAuthData();
4848
await storeData(authDataStorageKey, authData);
4949
}
5050
return authData;
@@ -70,20 +70,20 @@ async function requestTranslation(
7070
};
7171

7272
const response = await fetch(
73-
`https://www.bing.com/ttranslatev3?isVertical=1&IG=${authData.ig}` +
73+
`https://www.bing.com/ttranslatev3?isVertical=1&IG=${authData.ig}` +
7474
`&IID=${authData.iid}`,
75-
{
76-
method: 'POST',
77-
headers: {
78-
'Content-Type': 'application/x-www-form-urlencoded',
79-
// The User-agent needs to be set for testing purposes in node, as
80-
// Bing Translate appears to have introduced UA checks in Jan 2024.
81-
// PS: These checks appear to have been removed again in Feb 2024.
82-
// Anyway, it's safest to keep setting the UA for testing.
83-
'User-agent': navigator.userAgent,
84-
},
85-
body: new URLSearchParams(payload),
75+
{
76+
method: 'POST',
77+
headers: {
78+
'Content-Type': 'application/x-www-form-urlencoded',
79+
// The User-agent needs to be set for testing purposes in node, as
80+
// Bing Translate appears to have introduced UA checks in Jan 2024.
81+
// PS: These checks appear to have been removed again in Feb 2024.
82+
// Anyway, it's safest to keep setting the UA for testing.
83+
'User-agent': navigator.userAgent,
8684
},
85+
body: new URLSearchParams(payload),
86+
},
8787
);
8888

8989
const responseData = await response.json();

‎background/lib/translators/deepL.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ export default async function deepL(
3333

3434
// The observant web developer will notice one or two seemingly random spaces
3535
// sprinkled into the raw JSON sent to the DeepL servers. They are not random.
36-
body = ( (id + 3) % 13 == 0 || (id + 5) % 29 == 0 ) ?
36+
body = ((id + 3) % 13 == 0 || (id + 5) % 29 == 0) ?
3737
body.replace('"method":"', '"method" : "') :
3838
body.replace('"method":"', '"method": "');
3939

4040
const response = await fetch('https://www2.deepl.com/jsonrpc?client=chrome-extension,1.14.0', {
4141
method: 'POST',
4242
headers: {
43-
'Authorization': 'None',
43+
Authorization: 'None',
4444
'Content-Type': 'application/json; charset=utf-8',
4545
},
4646
referrer: 'https://www.deepl.com/',

‎background/lib/translators/googleTranslate.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ export default function googleTranslate(
1515
'https://translate.googleapis.com/translate_a/single?client=gtx&dt=t' +
1616
urlAppendix;
1717
return fetch(url)
18-
.then((response) => response.json())
19-
.then((json) => json[0][0][0] as string);
18+
.then((response) => response.json())
19+
.then((json) => json[0][0][0] as string);
2020
}

‎background/lib/utils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ export async function isPromiseResolved<T>(promise: Promise<T>):
3535
Promise<boolean> {
3636
const notAPromise = 'unlikely value';
3737
return Promise.race([promise, notAPromise])
38-
.then((value) => (value !== notAPromise));
38+
.then((value) => (value !== notAPromise));
3939
}

0 commit comments

Comments
 (0)