From 716abf148f6063f653be1d9c4d0882c34a1dcbd6 Mon Sep 17 00:00:00 2001 From: zengxiaoluan Date: Fri, 12 Jul 2024 19:54:15 +0800 Subject: [PATCH] v0.0.4 --- docs/rules/comments-need-space.md | 16 ++++++++++++++++ docs/rules/prefer-template-literal.md | 14 ++++++++++++++ lib/rules/comments-need-space.js | 17 ++++++++++++++++- package.json | 2 +- tests/lib/rules/comments-need-space.js | 7 +++++++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/docs/rules/comments-need-space.md b/docs/rules/comments-need-space.md index b1f166a..fe65f0d 100644 --- a/docs/rules/comments-need-space.md +++ b/docs/rules/comments-need-space.md @@ -17,3 +17,19 @@ function hello() { // comments } ``` + +Comments like below is not ok: + +```js +function hello() { + // comments中英文混杂没有空格 +} +``` + +That is good: + +```js +function hello() { + // comments 中英文混杂有空格 +} +``` diff --git a/docs/rules/prefer-template-literal.md b/docs/rules/prefer-template-literal.md index 25c9e8a..049d408 100644 --- a/docs/rules/prefer-template-literal.md +++ b/docs/rules/prefer-template-literal.md @@ -1,3 +1,17 @@ # Prefer template literal (`jlc/prefer-template-literal`) + +That is bad: + +```js +const name = "tom"; +const message = "Hello" + name; +``` + +That is good: + +```js +const name = "tom"; +const message = `Hello ${name}`; +``` diff --git a/lib/rules/comments-need-space.js b/lib/rules/comments-need-space.js index 9bdc7fa..11751ca 100644 --- a/lib/rules/comments-need-space.js +++ b/lib/rules/comments-need-space.js @@ -21,6 +21,7 @@ module.exports = { schema: [], // Add a schema if the rule has options messages: { needSpace: "comments need space", + spaceWithEnZh: "Need space between english and chinese", }, // Add messageId and message }, @@ -41,12 +42,26 @@ module.exports = { // visitor functions for different types of nodes Program(node) { for (const comment of node.comments || []) { - if (comment.type === "Line" && !comment.value.startsWith(" ")) { + let value = comment.value; + + if (comment.type === "Line" && !value.startsWith(" ")) { context.report({ loc: comment.loc, messageId: "needSpace", }); } + + let matches = value.match(/(\w[^\w\s'"])/g) || []; + + for (const item of matches) { + let maxAsciiCode = 128; + if (item.length === 2 && item.charCodeAt(1) > maxAsciiCode) { + context.report({ + loc: comment.loc, + messageId: "spaceWithEnZh", + }); + } + } } }, }; diff --git a/package.json b/package.json index cd7c4ce..e8a461a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-jlc", - "version": "0.0.3", + "version": "0.0.4", "description": "Preferences of eslint for jlc and jlc eda", "keywords": [ "eslint", diff --git a/tests/lib/rules/comments-need-space.js b/tests/lib/rules/comments-need-space.js index 235e098..3c2f4d9 100644 --- a/tests/lib/rules/comments-need-space.js +++ b/tests/lib/rules/comments-need-space.js @@ -20,6 +20,7 @@ ruleTester.run("comments-need-space", rule, { valid: [ // give me some code that won't trigger a warning "// give me some code that won't trigger a warning", + "// give me some code that won't trigger a warning 中英文混排,需要加空格", ], invalid: [ @@ -31,5 +32,11 @@ ruleTester.run("comments-need-space", rule, { }`, errors: [{ messageId: "needSpace", type: null }], }, + { + code: `function hello() { + // give me some code that won't trigger a warning中英文混排,需要加空格 + }`, + errors: [{ messageId: "spaceWithEnZh", type: null }], + }, ], });