Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:新增连续if条件下换行规则 #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions lib/rules/if-statement-wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @fileoverview If statement with wrap
* @author
*/
"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: `problem`, // `problem`, `suggestion`, or `layout`
docs: {
description: "If statement with wrap",
recommended: false,
url: null, // URL to the documentation page for this rule
},
fixable: null, // Or `code` or `whitespace`
schema: [], // Add a schema if the rule has options
messages: {
newLineRequired: "There should be a newline between consecutive if statements.",

}, // Add messageId and message


},

create(context) {
// variables should be defined here

//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------

// any helper functions should go here or else delete this section

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------

return {
// visitor functions for different types of nodes
IfStatement(node) {
const sourceCode = context.sourceCode
// 获取当前 if 语句结束行的行号
const previousTokenEndLine = node.loc.end.line;

// 获取当前 if 语句后的第一个 token
const nextToken = sourceCode.getTokenAfter(node);

// 如果下一个 token 存在且是 'if' 关键字
if (nextToken && nextToken.value === 'if') {

// 获取下一个 if 语句开始行的行号
const nextTokenStartLine = nextToken.loc.start.line;

if(nextTokenStartLine - previousTokenEndLine < 2){
if(nextTokenStartLine - previousTokenEndLine===0){
context.report({node,messageId: 'newLineRequired',});
}

}
}
},
};
},
};
45 changes: 45 additions & 0 deletions tests/lib/rules/if-statement-wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @fileoverview If statement with wrap
* @author
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require("../../../lib/rules/if-statement-wrap"),
RuleTester = require("eslint").RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();
ruleTester.run("if-statement-wrap", rule, {
valid: [
`if(true){1}

if(true){2}`,
`if(true){1}

console.log(1)`,
`if(true){1}

if(false){2}`,
`if(true){

if(true){}

if(true){}
}`

],

invalid: [
{
code: 'if(true){1}\nif(true){2}',
errors: [{ messageId: 'newLineRequired' }],
},
],
});
Loading