Skip to content

Commit 3fec593

Browse files
committed
Apply recommended ESLint rules
Getting rid of unused arguments, etc.
1 parent 0aa7ae5 commit 3fec593

10 files changed

+114
-75
lines changed

.eslintrc.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,43 @@
22

33
module.exports = {
44
root: true,
5-
extends: ['plugin:prettier/recommended'],
5+
extends: ['eslint:recommended', 'plugin:prettier/recommended'],
66
parserOptions: {
7-
ecmaVersion: 2017,
7+
ecmaVersion: 2018,
88
sourceType: 'module',
99
},
1010
parser: 'babel-eslint',
1111

12+
globals: {
13+
console: true,
14+
},
15+
1216
rules: {
1317
strict: [2, 'global'],
18+
19+
'no-param-reassign': 2,
1420
},
1521

16-
overrides: {
17-
files: ['.babelrc.js', '.eslintrc.js', 'jest.config.js'],
22+
overrides: [
23+
{
24+
files: ['.babelrc.js', '.eslintrc.js', 'jest.config.js'],
25+
26+
parserOptions: {
27+
sourceType: 'script',
28+
},
1829

19-
parserOptions: {
20-
sourceType: 'script',
30+
env: {
31+
node: true,
32+
},
2133
},
22-
},
34+
{
35+
files: '*.test.js',
36+
37+
env: {
38+
es6: true,
39+
jest: true,
40+
node: true,
41+
},
42+
},
43+
],
2344
};

src/TemplateTag/TemplateTag.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default class TemplateTag {
1515
*/
1616
constructor(...transformers) {
1717
if (!deprecationWarningPrinted) {
18+
// eslint-disable-next-line no-console
1819
console.warn(
1920
'TemplateTag is deprecated and will be removed in the next major version. Use createTag instead.',
2021
);

src/TemplateTag/TemplateTag.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import TemplateTag from '../TemplateTag';
22

3+
/* eslint-disable no-console */
4+
35
beforeEach(() => {
46
console.warn = jest.fn();
57
});
@@ -19,6 +21,8 @@ test('a warning should be printed the first time a TemplateTag is constructed',
1921
expect(console.warn).toHaveBeenCalledTimes(1);
2022
});
2123

24+
/* eslint-enable no-console */
25+
2226
test('performs a transformation & provides correct values to transform methods', () => {
2327
const tag = new TemplateTag({
2428
onString(str) {

src/inlineArrayTransformer/inlineArrayTransformer.js

+22-31
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
const defaults = {
2-
separator: '',
3-
conjunction: '',
4-
serial: false,
5-
};
6-
71
/**
82
* Converts an array substitution to a string containing a list
93
* @param {String} [opts.separator = ''] - the character that separates each item
@@ -12,34 +6,31 @@ const defaults = {
126
*
137
* @return {Object} - a TemplateTag transformer
148
*/
15-
const inlineArrayTransformer = (opts = defaults) => ({
9+
const inlineArrayTransformer = ({
10+
conjunction = '',
11+
separator = '',
12+
serial = false,
13+
} = {}) => ({
1614
onSubstitution(substitution, resultSoFar) {
1715
// only operate on arrays
18-
if (Array.isArray(substitution)) {
19-
const arrayLength = substitution.length;
20-
const separator = opts.separator;
21-
const conjunction = opts.conjunction;
22-
const serial = opts.serial;
23-
// join each item in the array into a string where each item is separated by separator
24-
// be sure to maintain indentation
25-
const indent = resultSoFar.match(/(\n?[^\S\n]+)$/);
26-
if (indent) {
27-
substitution = substitution.join(separator + indent[1]);
28-
} else {
29-
substitution = substitution.join(separator + ' ');
30-
}
31-
// if conjunction is set, replace the last separator with conjunction, but only if there is more than one substitution
32-
if (conjunction && arrayLength > 1) {
33-
const separatorIndex = substitution.lastIndexOf(separator);
34-
substitution =
35-
substitution.slice(0, separatorIndex) +
36-
(serial ? separator : '') +
37-
' ' +
38-
conjunction +
39-
substitution.slice(separatorIndex + 1);
40-
}
16+
if (!Array.isArray(substitution)) {
17+
return substitution;
4118
}
42-
return substitution;
19+
20+
// be sure to maintain indentation
21+
const indent = resultSoFar.match(/(\n?[^\S\n]+)$/);
22+
const fullSeparator = separator.concat(indent ? indent[1] : ' ');
23+
const fullConjunction = ''.concat(conjunction, ' ');
24+
const conjunctionIndex = conjunction ? substitution.length - 1 : -1;
25+
26+
return substitution.reduce((result, part, index) =>
27+
''.concat(
28+
result,
29+
index !== conjunctionIndex || serial ? fullSeparator : ' ',
30+
index === conjunctionIndex ? fullConjunction : '',
31+
part,
32+
),
33+
);
4334
},
4435
});
4536

src/inlineArrayTransformer/inlineArrayTransformer.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ test('replaces last separator with a conjunction', () => {
2222
);
2323
});
2424

25+
test('replaces last separator with a conjunction', () => {
26+
const tag = createTag(
27+
inlineArrayTransformer({ separator: ',', conjunction: 'and' }),
28+
);
29+
expect(
30+
tag`I like ${['apple', 'banana', 'a fruit that has "," in the name']}`,
31+
).toBe('I like apple, banana and a fruit that has "," in the name');
32+
});
33+
2534
test('does not use a conjunction if there is only one item in an array', () => {
2635
const tag = createTag(
2736
inlineArrayTransformer({ separator: ',', conjunction: 'and' }),

src/replaceSubstitutionTransformer/replaceSubstitutionTransformer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const replaceSubstitutionTransformer = (replaceWhat, replaceWith) => ({
2-
onSubstitution(substitution, resultSoFar) {
2+
onSubstitution(substitution) {
33
if (replaceWhat == null || replaceWith == null) {
44
throw new Error(
55
'replaceSubstitutionTransformer requires at least 2 arguments.',
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
const splitStringTransformer = splitBy => ({
2-
onSubstitution(substitution, resultSoFar) {
3-
if (splitBy != null && typeof splitBy === 'string') {
1+
const splitStringTransformer = splitBy => {
2+
if (typeof splitBy !== 'string') {
3+
throw new Error('You need to specify a string character to split by.');
4+
}
5+
6+
return {
7+
onSubstitution(substitution) {
48
if (typeof substitution === 'string' && substitution.includes(splitBy)) {
5-
substitution = substitution.split(splitBy);
9+
return substitution.split(splitBy);
610
}
7-
} else {
8-
throw new Error('You need to specify a string character to split by.');
9-
}
10-
return substitution;
11-
},
12-
});
11+
return substitution;
12+
},
13+
};
14+
};
1315

1416
export default splitStringTransformer;

src/splitStringTransformer/splitStringTransformer.test.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ test('ignores substitution if it is not a string', () => {
1717
expect(tag`foo ${5}`).toBe('foo 5');
1818
});
1919

20-
test('throws an error if splitBy param is undefined or not a string', () => {
21-
const tag1 = createTag(splitStringTransformer());
22-
const tag2 = createTag(splitStringTransformer(5));
23-
expect(() => tag1`foo ${'bar'}`).toThrow();
24-
expect(() => tag2`foo ${'bar'}`).toThrow();
20+
test('throws an error if splitBy param is undefined', () => {
21+
expect(() => {
22+
splitStringTransformer();
23+
}).toThrow(/specify a string character to split by/);
24+
});
25+
26+
test('throws an error if splitBy param is not a string', () => {
27+
expect(() => {
28+
splitStringTransformer(42);
29+
}).toThrow(/specify a string character to split by/);
2530
});
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1+
const supportedSides = ['', 'start', 'left', 'end', 'right'];
2+
13
/**
24
* TemplateTag transformer that trims whitespace on the end result of a tagged template
35
* @param {String} side = '' - The side of the string to trim. Can be 'start' or 'end' (alternatively 'left' or 'right')
46
* @return {Object} - a TemplateTag transformer
57
*/
6-
const trimResultTransformer = (side = '') => ({
7-
onEndResult(endResult) {
8-
if (side === '') {
9-
return endResult.trim();
10-
}
11-
12-
side = side.toLowerCase();
8+
const trimResultTransformer = (side = '') => {
9+
if (!supportedSides.includes(side)) {
10+
throw new Error(`Side not supported: ${side}`);
11+
}
1312

14-
if (side === 'start' || side === 'left') {
15-
return endResult.replace(/^\s*/, '');
16-
}
13+
return {
14+
onEndResult(endResult) {
15+
switch (side) {
16+
case '':
17+
return endResult.trim();
1718

18-
if (side === 'end' || side === 'right') {
19-
return endResult.replace(/\s*$/, '');
20-
}
19+
case 'start':
20+
case 'left':
21+
return endResult.replace(/^\s*/, '');
2122

22-
throw new Error(`Side not supported: ${side}`);
23-
},
24-
});
23+
case 'end':
24+
case 'right':
25+
return endResult.replace(/\s*$/, '');
26+
}
27+
},
28+
};
29+
};
2530

2631
export default trimResultTransformer;

src/trimResultTransformer/trimResultTransformer.test.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ test('trims right padding', () => {
2727
expect(trimRight` foo `).toBe(' foo');
2828
});
2929

30-
test('throws an error if invalid side supplied', () => {
31-
const trimUp = createTag(trimResultTransformer('up'));
32-
expect(() => trimUp`foo`).toThrow();
33-
});
34-
3530
test('can be used sequentially', () => {
3631
const trimStart = createTag(
3732
stripIndentTransformer(),
@@ -40,3 +35,9 @@ test('can be used sequentially', () => {
4035
expect(trimStart` foo `).toBe('foo ');
4136
expect(trimStart` bar `).toBe('bar ');
4237
});
38+
39+
test('throws an error if invalid side supplied', () => {
40+
expect(() => {
41+
trimResultTransformer('up');
42+
}).toThrow(/not supported/);
43+
});

0 commit comments

Comments
 (0)