Skip to content

Commit

Permalink
feat(test): 新增和修改基础规则的测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicHacker committed Sep 28, 2020
1 parent d04b4d0 commit 9964f60
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 13 deletions.
15 changes: 15 additions & 0 deletions test/no-await-in-loop/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 禁止将await写在循环里,因为这样就无法发送多个异步请求了,无法并发
* 有时需要在循环中写 await,所以此规则暂时关闭
*/
function bar() {}
function baz() {}
async function foo(things) {
const results = [];
for (const thing of things) {
// Bad: each loop iteration is delayed until the entire asynchronous operation completes
results.push(await bar(thing));
}
return baz(results);
}
foo()
7 changes: 6 additions & 1 deletion test/no-cond-assign/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
* 禁止条件语句中出现赋值操作
* 除非这个赋值语句被括号包裹起来
*/
if ((x = 0)) {
let x = {}
let someNode = {}
if (x = x.a) {
// do something
}
if ((someNode = someNode.parentNode) !== null) {
// do
}
4 changes: 4 additions & 0 deletions test/no-console/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* 禁止调用console
*/
console.log()
7 changes: 7 additions & 0 deletions test/no-control-regex/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* 禁止在正则表达式中使用控制字符
*/
const pattern1 = /\x1f/;
const pattern2 = new RegExp("\x1f");

console.log(pattern1, pattern2)
8 changes: 8 additions & 0 deletions test/no-debugger/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 禁用debugger
*/
function isTruthy(x) {
debugger;
return Boolean(x);
}
isTruthy()
8 changes: 8 additions & 0 deletions test/no-dupe-args/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 禁止在函数参数中出现重复名称的参数
* 禁用后打包会出错
*/
function foo(a, b, a) {
console.log("value of the second a:", a);
}
foo()
9 changes: 7 additions & 2 deletions test/no-empty/error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* 禁止出现空的语句块
* catch语句中除外
* 允许catch语句为空语句块
*/
const arr = ''
if (arr) { }
if (arr) {}

try {
//doSomething();
}
catch (ex) {}
4 changes: 4 additions & 0 deletions test/no-extra-parens/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* 禁止冗余的括号
* 具体规则: https://cn.eslint.org/docs/rules/no-extra-parens
*/
5 changes: 5 additions & 0 deletions test/no-extra-semi/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* 禁止多余的分号
* 具体规则: https://cn.eslint.org/docs/rules/no-extra-semi
* prettier会自动修复
*/
6 changes: 2 additions & 4 deletions test/no-func-assign/error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* 禁止将函数声明重新赋值
*/
const bar = 0
function foo() {}
foo = bar;

function foo() {
foo = bar;
}
console.log(foo)
8 changes: 8 additions & 0 deletions test/no-irregular-whitespace/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 禁止使用不规则的空白符
* "skipStrings": true (默认) 允许在字符串字面量中出现任何空白字符
* "skipComments": true 允许在注释中出现任何空白字符
* "skipRegExps": true 允许在正则表达式中出现任何空白字符
* "skipTemplates": true 允许在模板字面量中出现任何空白字符
* 具体规则: https://cn.eslint.org/docs/rules/no-irregular-whitespace
*/
6 changes: 6 additions & 0 deletions test/no-prototype-builtins/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* 禁止直接调用 Object.prototypes 的内置属性如:hasOwnProperty,isPrototypeOf
*/
const foo = {}
const hasBarProperty = foo.hasOwnProperty("bar");
console.warn(hasBarProperty)
8 changes: 8 additions & 0 deletions test/no-unexpected-multiline/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* 禁止令人困惑的多行表达式
* 具体规则: https://cn.eslint.org/docs/rules/no-unexpected-multiline
*/
const bar = 0
const foo = bar
(1 || 2).baz();
console.log(foo)
2 changes: 2 additions & 0 deletions test/no-unsafe-finally/error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/**
* 禁止在 finally 中出现 return, throw, break 或 continue语句
* finally 中的语句会在 try 之前执行
* 具体规则: https://cn.eslint.org/docs/rules/no-unsafe-finally
*/
const foo = function () {
try {
Expand Down
5 changes: 5 additions & 0 deletions test/require-atomic-updates/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* 禁止将 await 或 yield 的结果做为运算符的第二项
* 具体规则:https://cn.eslint.org/docs/rules/require-atomic-updates
* 市面上大部分eslint规则库都关闭了此项
*/
20 changes: 14 additions & 6 deletions test/valid-typeof/error.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/**
* typeof 操作符比较的对象必须是 'undefined', 'object', 'boolean', 'number', 'string', 'function', 'symbol', 或 'bigint'
* typeof 操作符比较的对象必须是 'undefined', 'object', 'boolean', 'number',
* 'string', 'function', 'symbol', 或 'bigint'
*/
const foo = "";
const bar = "";
typeof foo === "strnig";
typeof foo === "undefimed";
typeof bar !== "nunber";
typeof bar !== "fucntion";
if (typeof foo === "strnig") {
//
}
if (typeof foo === "undefimed") {
//
}
if (typeof foo !== "nunber") {
//
}
if (typeof foo !== "fucntion") {
//
}

0 comments on commit 9964f60

Please sign in to comment.