@markof/eslint-plugin-no-chinese 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # eslint-plugin-no-chinese
2
+ eslint plugin to forbid chinese
3
+
4
+ ## Install
5
+ ```
6
+ npm i @markof/eslint-plugin-no-chinese --save-dev
7
+ ```
8
+
9
+ ## Usage
10
+ Add '@markof/eslint-plugin-no-chinese' to plugins in .eslintrc file.
11
+ ```javascript
12
+ {
13
+ "plugins": [
14
+ "@markof/eslint-plugin-no-chinese"
15
+ ]
16
+ }
17
+ ```
18
+
19
+ Config rules in .eslintrc file.
20
+ ```javascript
21
+ {
22
+ "rules": {
23
+ "@markof/eslint-plugin-no-chinese/no-chinese-code": "error",
24
+ "@markof/eslint-plugin-no-chinese/no-chinese-comment": "error",
25
+ "@markof/eslint-plugin-no-chinese/no-chinese-console": "error",
26
+ }
27
+ }
28
+ ```
package/lib/index.js ADDED
@@ -0,0 +1,21 @@
1
+ const pkg = require('../package.json')
2
+
3
+ const plugin = {
4
+ meta: {
5
+ name: pkg.name,
6
+ version: pkg.version,
7
+ },
8
+ configs: {},
9
+ rules: {
10
+ "no-chinese-code": require('./rules/no-chinese-code.js'),
11
+ "no-chinese-comment": require('./rules/no-chinese-comment.js'),
12
+ "no-chinese-console": require('./rules/no-chinese-console.js'),
13
+ },
14
+ processors: {}
15
+ }
16
+
17
+ // for ESM
18
+ export default plugin;
19
+
20
+ // OR for CommonJS
21
+ module.exports = plugin;
@@ -0,0 +1,29 @@
1
+
2
+ const REGEX = /[\u4e00-\u9fff\uff00-\uffef\u3000-\u303f\u2014\u2018-\u2019\u2026\u201c\u201d]/
3
+
4
+ function hasChinese(value) {
5
+ return REGEX.test(value)
6
+ }
7
+
8
+ module.exports = {
9
+ create: function(context) {
10
+ return {
11
+ Literal: function(node) {
12
+ const { value } = node
13
+ // check if the node is in the console
14
+ if (node && node.parent && node.parent.callee && node.parent.callee.object && node.parent.callee.object.name === 'console') {
15
+ return;
16
+ }
17
+ if (hasChinese(value)) {
18
+ context.report({
19
+ node,
20
+ message: '{{ str }} Avoid using Chinese in the code',
21
+ data: {
22
+ str: node.value,
23
+ },
24
+ });
25
+ }
26
+ },
27
+ };
28
+ },
29
+ };
@@ -0,0 +1,29 @@
1
+ const REGEX = /[\u4e00-\u9fff\uff00-\uffef\u3000-\u303f\u2014\u2018-\u2019\u2026\u201c\u201d]/
2
+
3
+ function hasChinese(value) {
4
+ return REGEX.test(value)
5
+ }
6
+
7
+ module.exports = {
8
+ create: function (context) {
9
+
10
+ const sourceCode = context.getSourceCode()
11
+
12
+ return {
13
+ Program() {
14
+ const comments = sourceCode.getAllComments();
15
+ comments.forEach(comment => {
16
+ if (hasChinese(comment.value)) {
17
+ context.report({
18
+ node: comment,
19
+ message: '{{ str }} Avoid using Chinese in the comments.',
20
+ data: {
21
+ str: comment.value,
22
+ },
23
+ });
24
+ }
25
+ })
26
+ }
27
+ }
28
+ }
29
+ }
@@ -0,0 +1,27 @@
1
+
2
+ const REGEX = /[\u4e00-\u9fff\uff00-\uffef\u3000-\u303f\u2014\u2018-\u2019\u2026\u201c\u201d]/
3
+
4
+ function hasChinese(value) {
5
+ return REGEX.test(value)
6
+ }
7
+
8
+ module.exports = {
9
+ create: function(context) {
10
+ return {
11
+ Literal: function(node) {
12
+ const { value } = node;
13
+ if (node && node.parent && node.parent.callee && node.parent.callee.object && node.parent.callee.object.name === 'console') {
14
+ if (hasChinese(value)) {
15
+ context.report({
16
+ node,
17
+ message: '{{ str }} Avoid using Chinese in the console.',
18
+ data: {
19
+ str: node.value,
20
+ },
21
+ });
22
+ }
23
+ }
24
+ },
25
+ };
26
+ },
27
+ };
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@markof/eslint-plugin-no-chinese",
3
+ "version": "1.0.2",
4
+ "description": "eslint plugin to forbid chinese",
5
+ "keywords": ["eslint", "eslint-plugin", "chinese"],
6
+ "repository": "https://github.com/markof/eslint-plugin-no-chinese",
7
+ "author": "Markof",
8
+ "main": "lib/index.js",
9
+ "scripts": {
10
+ "lint": "eslint .",
11
+ "test": "mocha tests --recursive"
12
+ },
13
+ "dependencies": {
14
+ "requireindex": "^1.2.0"
15
+ },
16
+ "devDependencies": {
17
+ "eslint": "^8.0.1",
18
+ "eslint-plugin-eslint-plugin": "^4.0.1",
19
+ "eslint-plugin-node": "^11.1.0",
20
+ "mocha": "^9.2.2"
21
+ },
22
+ "engines": {
23
+ "node": ">= 16"
24
+ },
25
+ "peerDependencies": {
26
+ "eslint": ">=6"
27
+ },
28
+ "license": "ISC"
29
+ }
package/tests/index.js ADDED
@@ -0,0 +1,53 @@
1
+ const RuleTester = require("eslint").RuleTester
2
+ var ruleTester = new RuleTester()
3
+ const ruleNoChineseCode = require("../lib/rules/no-chinese-code")
4
+ const ruleNoChineseComments = require("../lib/rules/no-chinese-comments")
5
+ const ruleNoChineseConsole = require("../lib/rules/no-chinese-console")
6
+
7
+ ruleTester.run("no-chinese-code", ruleNoChineseCode, {
8
+ valid: ["function test(d, e, f) { console.log('中文'); return 'chinese' }"],
9
+ invalid: [
10
+ {
11
+ code: "function test(d, e, f) { console.log('中文'); return '中文' }",
12
+ errors: [{message: "中文 Avoid using Chinese in the code"}]
13
+ },
14
+ {
15
+ code: "function test(d, e, f) { console.log('chinese'); return 'it’s name is chinese' }",
16
+ errors: [{message: "it’s name is chinese Avoid using Chinese in the code"}]
17
+ },
18
+ {
19
+ code: "function test(d, e, f) {return 'it“s name is chinese'}",
20
+ errors: [{message: "it“s name is chinese Avoid using Chinese in the code"}]
21
+ },
22
+ ],
23
+ })
24
+
25
+ ruleTester.run("no-chinese-console", ruleNoChineseConsole, {
26
+ valid: ["function test(d, e, f) { console.log('chinese'); return 'chinese' }"],
27
+ invalid: [
28
+ {
29
+ code: "function test(d, e, f) { console.log('中文'); return 'chinese' }",
30
+ errors: [{
31
+ message: "中文 Avoid using Chinese in the console.",
32
+ }]
33
+ },
34
+ ],
35
+ })
36
+
37
+ ruleTester.run("no-chinese-comments", ruleNoChineseComments, {
38
+ valid: [`// this is a comment`, `/* this is a comment */`],
39
+ invalid: [
40
+ {
41
+ code: `// 中文`,
42
+ errors: [{
43
+ message: " 中文 Avoid using Chinese in the comments.",
44
+ }]
45
+ },
46
+ {
47
+ code: `/* 中文 */`,
48
+ errors: [{
49
+ message: " 中文 Avoid using Chinese in the comments.",
50
+ }]
51
+ },
52
+ ],
53
+ })