Skip to content

Commit 19c53f5

Browse files
authored
Merge pull request #54 from htmlacademy/feature/add-new-rules
Added rules for Vanila JS
2 parents 2fd7915 + af3e5ad commit 19c53f5

18 files changed

+297
-101
lines changed

test/vanilla/accessor-pairs.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const objectSample = {
2+
set key(value) {
3+
this.valueThisObject = value;
4+
},
5+
get key() {
6+
return this.valueThisObject;
7+
},
8+
};
9+
10+
const objectSampleOther = {key: 1};
11+
12+
Object.defineProperty(objectSampleOther, 'anythingString', {
13+
set: function(value) {
14+
this.valueThisObject = value;
15+
},
16+
get: function() {
17+
return this.valueThisObject;
18+
},
19+
});
20+
21+
throw new Error(objectSample);

test/vanilla/arrow-parens.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
const a = '';
2-
31
() => {};
42

5-
(a) => {return a;};
6-
7-
(a) => a;
3+
(anything) => anything;
84

9-
(a) => {
10-
return `${a} \n`;
5+
(anything) => {
6+
anything++;
7+
return anything;
118
};
129

13-
a.then((foo) => {
14-
return foo;
10+
(anything) => `${anything} \n`;
11+
12+
const emptyString = '';
13+
14+
emptyString.then((anything) => {
15+
anything = `${anything}!`;
16+
return anything;
1517
});
1618

17-
a.then((foo) => {
18-
if (foo === true) {
19-
return foo;
19+
emptyString.then((anything) => {
20+
if (typeof anything === 'string') {
21+
return anything;
2022
}
2123
});

test/vanilla/camelcase.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const textColor = '#112C85';
2+
const _textColor = '#112C85';
3+
const textColor_ = '#112C85';
4+
const TEXT_COLOR = '#112C85';
5+
6+
const objectSample ={
7+
key: 'key',
8+
};
9+
10+
const doSomething = () => {};
11+
12+
const value = objectSample.key;
13+
const instance = {
14+
value: objectSample.key,
15+
};
16+
17+
objectSample.do_something();
18+
objectSample.doSomething();
19+
doSomething();
20+
new doSomething();
21+
22+
const categoryItem = {
23+
categoryId: '001',
24+
isCategory: true,
25+
};
26+
const {categoryId: categoryExample} = categoryItem;
27+
28+
function doSomethingFirst({isCamelCasedInnerScore}) {
29+
return isCamelCasedInnerScore;
30+
}
31+
32+
function doSomethingSecond({isCamelCased: isAlsoCamelCased}) {
33+
return isAlsoCamelCased;
34+
}
35+
36+
function doSomethingThird({isCamelCasedInnerScore = 'default value'}) {
37+
return isCamelCasedInnerScore;
38+
}
39+
40+
const {categoryId = 1} = categoryItem;
41+
42+
const {isCategory: isCamelCased} = categoryItem;
43+
44+
const {isCategory: isCamelCasedOther = true} = categoryItem;
45+
46+
throw new Error(textColor, _textColor, textColor_, TEXT_COLOR, value, instance, categoryExample, doSomethingFirst, doSomethingSecond, doSomethingThird, isCamelCased, isCamelCasedOther, categoryId);

test/vanilla/comma-dangles.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
const arr0 = ['a', 'b', 'c'];
2-
const arr1 = [
1+
const characters = ['a', 'b', 'c'];
2+
const letters = [
33
'a',
44
'b',
55
'c',
66
];
77

8-
const obj0 = {q: 'q', w: 'w'};
9-
const obj1 = {
10-
q: 'q',
11-
w: 'w',
8+
const category = {id: 0, name: 'office'};
9+
const product = {
10+
id: 0,
11+
categoryId: 0,
12+
name: 'table',
1213
};
1314

14-
const func0 = (a, b) => {
15-
return a + b;
16-
};
17-
const func1 = (
18-
a,
19-
b,
20-
) => {
21-
return a + b;
22-
};
15+
const matchesCategory = (categoryItem, productItem) => categoryItem.id === productItem.categoryId;
16+
17+
const includesPattern = (
18+
productName,
19+
pattern,
20+
) => (typeof productName === 'string' && productName.includes(pattern));
2321

24-
func0(
25-
obj0.a,
26-
obj0.b,
22+
matchesCategory(
23+
category,
24+
product,
2725
);
2826

29-
throw new Error(arr0, arr1, obj0, obj1, func0, func1);
27+
throw new Error(characters, letters, includesPattern);

test/vanilla/curly.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const isTrue = true;
2+
3+
const doSomething = () => {};
4+
const doSomethingElse = () => {};
5+
6+
let counter = 1;
7+
8+
if (isTrue) {
9+
counter++;
10+
}
11+
12+
while (isTrue) {
13+
doSomething(counter);
14+
}
15+
16+
if (isTrue) {
17+
doSomething();
18+
} else {
19+
doSomethingElse();
20+
}

test/vanilla/eqeqeq.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const conditionLeft = 'str';
2+
const conditionRight = 'str';
3+
4+
conditionLeft === conditionRight;
5+
conditionLeft === true;
6+
conditionLeft !== 1;
7+
conditionLeft === undefined;
8+
typeof undefinedVariable === 'undefined';
9+
'hello' !== 'world';
10+
0 === 0;
11+
true === true;
12+
conditionLeft === null;

test/vanilla/indent.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
1-
'use strict';
2-
31
// Continuation, aka MemberExpression
42
const promise = window.Promise.resolve(true);
53
promise.
6-
then((data) => {
7-
return data;
8-
}).
9-
then((truthy) => {
10-
return !truthy;
11-
}).
12-
catch(() => {
13-
return false;
14-
});
4+
then((data) => data).
5+
then((truthy) => !truthy).
6+
catch(() => false);
157

168
// Function expression
17-
const fun = function (first, second) {
18-
return first + second;
9+
const calculateSum = function (sum, price) {
10+
return sum + price;
1911
};
2012

2113
// Function declaration
22-
function Constructor(first, second) {
14+
function Constructor(name, price) {
2315
this.data = {
24-
first: first,
25-
second: second,
16+
name: name,
17+
price: price,
2618
};
2719
}
2820

2921
// Calling site arguments
30-
const myObject = new Constructor(
22+
const objectSample = new Constructor(
3123
'Petya',
3224
'Vasya',
3325
);
34-
myObject.toString();
26+
objectSample.toString();
3527

36-
const result = fun(
37-
'one',
38-
'two',
28+
const result = calculateSum(
29+
10000,
30+
1000,
3931
);
4032
result.toString();
4133

4234
// Switch
43-
const a = 'a';
35+
const symbol = 'a';
4436

45-
switch(a) {
37+
switch(symbol) {
4638
case 'a':
4739
break;
4840
case 'b':

test/vanilla/no-shadow.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const isTrue = true;
2+
3+
const doEnything = () => {};
4+
5+
if (isTrue) {
6+
const coordinateX = 3;
7+
const coordinateY = 6;
8+
9+
doEnything(coordinateX, coordinateY);
10+
}
11+
12+
const coordinateZ = 5;
13+
14+
function doSomething() {}
15+
16+
throw new Error(coordinateZ, doSomething);

test/vanilla/no-useless-concat.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const price = 100;
2+
const discount = 50;
3+
const count = 5;
4+
5+
const fullPrice = price - discount;
6+
const discountMessage = `Discount is ${price * (discount / 100)}%`;
7+
const fullPriceMessage = `${fullPrice} - FULL price!`;
8+
const countAfterBuy = count - 1;
9+
const countAfterRefund = count + 1;
10+
11+
// when the string concatenation is multiline
12+
const fullMessage = 'It' +
13+
'will' +
14+
'make you happy!';
15+
16+
throw new Error(discountMessage, fullPriceMessage, countAfterBuy, countAfterRefund, fullMessage);

test/vanilla/no-useless-return.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const isTrue = true;
2+
3+
const doSomething = () => {};
4+
const doSomethingElse = () => {};
5+
const doDefault = () => {};
6+
7+
function getNumberFive() { return 5; }
8+
9+
function getFunction() {
10+
return () => {};
11+
}
12+
13+
function doSomethingWithIf() {
14+
if (isTrue) {
15+
doSomething();
16+
return;
17+
} else {
18+
doSomethingElse();
19+
}
20+
doDefault();
21+
}
22+
23+
function doSomethingWithSwitch() {
24+
switch (isTrue) {
25+
case 1:
26+
doSomething();
27+
return;
28+
default:
29+
doSomethingElse();
30+
}
31+
}
32+
33+
function doSomethingWithArray() {
34+
const numbers = [1, 2, 3];
35+
for (const number of numbers) {
36+
if (number === 1) {
37+
return number;
38+
}
39+
}
40+
}
41+
42+
throw new Error(getNumberFive, getFunction, doSomethingWithIf, doSomethingWithSwitch, doSomethingWithArray);

0 commit comments

Comments
 (0)