@lvce-editor/eslint-plugin-regex 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +78 -0
  2. package/package.json +13 -0
package/dist/index.js ADDED
@@ -0,0 +1,78 @@
1
+ const isFunctionNode = node => {
2
+ return node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
3
+ };
4
+ const isInsideFunction = node => {
5
+ let current = node.parent;
6
+ while (current) {
7
+ if (isFunctionNode(current)) {
8
+ return true;
9
+ }
10
+ current = current.parent;
11
+ }
12
+ return false;
13
+ };
14
+ const meta = {
15
+ type: 'suggestion',
16
+ docs: {
17
+ description: 'Enforce hoisting regexes to module scope'
18
+ },
19
+ messages: {
20
+ hoistRegex: 'Regex should be hoisted.'
21
+ }
22
+ };
23
+ const create = context => {
24
+ return {
25
+ Literal(node) {
26
+ if (node.regex && isInsideFunction(node)) {
27
+ context.report({
28
+ node,
29
+ messageId: 'hoistRegex'
30
+ });
31
+ }
32
+ },
33
+ NewExpression(node) {
34
+ if (node.callee?.type === 'Identifier' && node.callee.name === 'RegExp' && isInsideFunction(node)) {
35
+ context.report({
36
+ node,
37
+ messageId: 'hoistRegex'
38
+ });
39
+ }
40
+ },
41
+ CallExpression(node) {
42
+ if (node.callee?.type === 'Identifier' && node.callee.name === 'RegExp' && isInsideFunction(node)) {
43
+ context.report({
44
+ node,
45
+ messageId: 'hoistRegex'
46
+ });
47
+ }
48
+ }
49
+ };
50
+ };
51
+
52
+ const hoistRegex = {
53
+ __proto__: null,
54
+ create,
55
+ meta
56
+ };
57
+
58
+ const plugin = {
59
+ meta: {
60
+ name: 'regex',
61
+ version: '0.0.1'
62
+ },
63
+ rules: {
64
+ 'hoist-regex': hoistRegex
65
+ },
66
+ configs: {}
67
+ };
68
+ const recommended = [{
69
+ files: ['**/*.{js,mjs,cjs,ts,mts,cts}'],
70
+ plugins: {
71
+ regex: plugin
72
+ },
73
+ rules: {
74
+ 'regex/hoist-regex': 'error'
75
+ }
76
+ }];
77
+
78
+ export { recommended as default };
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@lvce-editor/eslint-plugin-regex",
3
+ "version": "6.0.0",
4
+ "main": "dist/index.js",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/lvce-editor/eslint-config.git"
8
+ },
9
+ "type": "module",
10
+ "keywords": [],
11
+ "license": "MIT",
12
+ "description": ""
13
+ }