flatlint 1.13.0 β†’ 1.15.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.
package/ChangeLog CHANGED
@@ -1,3 +1,13 @@
1
+ 2025.01.02, v1.15.0
2
+
3
+ feature:
4
+ - e018137 flatlint: replacer: simplify
5
+
6
+ 2025.01.01, v1.14.0
7
+
8
+ feature:
9
+ - 2361298 flatlint: __expr
10
+
1
11
  2025.01.01, v1.13.0
2
12
 
3
13
  feature:
package/README.md CHANGED
@@ -95,8 +95,9 @@ npm i flatlint
95
95
  **FlatLint** uses language similar to 🐊[**PutoutScript**](https://github.com/coderaiser/putout/blob/master/docs/putout-script.md#-putoutscript).
96
96
 
97
97
  It can look similar, but has a couple differences:
98
- - βœ…it may not be valid **JavaScript**, it can be couple tokens that can be fixed;
99
- - βœ…it counts each symbol as a token;
98
+
99
+ - βœ… it may not be valid **JavaScript**, it can be couple tokens that can be fixed;
100
+ - βœ… it counts each symbol as a token;
100
101
 
101
102
  ### `__a`
102
103
 
@@ -108,6 +109,10 @@ it can be only one quote `'__a` - this is valid, since **FlatLint** is tokens ba
108
109
  Collects everything that looks like array elements, it can start from squire brace `[__array;`, but that's not important
109
110
  to end with it, since it used to fix error patterns.
110
111
 
112
+ ### `__expr`
113
+
114
+ Collects everything that looks like expression.
115
+
111
116
  ## API
112
117
 
113
118
  ```js
@@ -1,7 +1,7 @@
1
1
  import {Punctuator} from '#types';
2
2
  import {equal} from './equal.js';
3
3
 
4
- export const collectArray = ({currentTokenIndex, tokens, nextTemplateToken}) => {
4
+ export const collectArray = ({currentTokenIndex, tokens, nextTemplateToken = Punctuator(';')}) => {
5
5
  const n = tokens.length;
6
6
  const brace = Punctuator(']');
7
7
  let index = currentTokenIndex;
@@ -18,4 +18,3 @@ export const collectArray = ({currentTokenIndex, tokens, nextTemplateToken}) =>
18
18
 
19
19
  return --index;
20
20
  };
21
-
@@ -0,0 +1,28 @@
1
+ import {Punctuator} from '#types';
2
+ import {equal} from './equal.js';
3
+
4
+ export const collectExpression = ({currentTokenIndex, tokens, nextTemplateToken = Punctuator(';')}) => {
5
+ const n = tokens.length;
6
+ const closeBrace = Punctuator(')');
7
+ const openBrace = Punctuator('(');
8
+ const semicolon = Punctuator(';');
9
+ let index = currentTokenIndex;
10
+
11
+ for (; index < n; index++) {
12
+ const token = tokens[index];
13
+
14
+ if (equal(token, semicolon))
15
+ break;
16
+
17
+ if (equal(token, nextTemplateToken))
18
+ break;
19
+
20
+ if (equal(token, openBrace))
21
+ break;
22
+
23
+ if (equal(token, closeBrace))
24
+ break;
25
+ }
26
+
27
+ return --index;
28
+ };
@@ -1,6 +1,10 @@
1
1
  import {prepare} from '#parser';
2
- import {isTemplateArrayToken} from '#types';
2
+ import {
3
+ isTemplateArrayToken,
4
+ isTemplateExpressionToken,
5
+ } from '#types';
3
6
  import {collectArray} from './collect-array.js';
7
+ import {collectExpression} from './collect-expression.js';
4
8
  import {
5
9
  equal,
6
10
  equalAny,
@@ -26,7 +30,17 @@ export const compare = (source, template) => {
26
30
  const templateToken = templateTokens[templateIndex];
27
31
  const currentToken = tokens[currentTokenIndex];
28
32
 
29
- if (isTemplateArrayToken(templateToken)) {
33
+ if (isTemplateExpressionToken(templateToken)) {
34
+ const indexOfExpressionEnd = collectExpression({
35
+ currentTokenIndex,
36
+ tokens,
37
+ templateToken,
38
+ nextTemplateToken: templateTokens[templateIndex + 1],
39
+ });
40
+
41
+ delta = indexOfExpressionEnd - currentTokenIndex;
42
+ index = indexOfExpressionEnd - templateIndex;
43
+ } else if (isTemplateArrayToken(templateToken)) {
30
44
  const indexOfArrayEnd = collectArray({
31
45
  currentTokenIndex,
32
46
  tokens,
@@ -1,7 +1,6 @@
1
1
  export const report = () => 'Remove useless round brace';
2
2
 
3
3
  export const replace = () => ({
4
- 'const __a = __b)': 'const __a = __b',
5
- 'const __a = "__b")': 'const __a = "__b"',
4
+ 'const __a = __expr);': 'const __a = __expr;',
6
5
  'from "__b")': 'from "__b"',
7
6
  });
@@ -1,14 +1,14 @@
1
+ import {compare} from '#compare';
2
+ import {prepare} from '#parser';
3
+ import {traverse} from '#traverser';
1
4
  import {
2
5
  is,
3
6
  isTemplateArray,
4
- Punctuator,
7
+ isTemplateExpression,
5
8
  } from '#types';
6
- import {prepare} from '#parser';
7
- import {compare} from '#compare';
8
- import {traverse} from '#traverser';
9
9
  import {collectArray} from '../compare/collect-array.js';
10
+ import {collectExpression} from '../compare/collect-expression.js';
10
11
 
11
- const {isArray} = Array;
12
12
  const {entries} = Object;
13
13
 
14
14
  export const replace = (tokens, {fix, rule, plugin}) => {
@@ -85,24 +85,20 @@ function getValues(tokens, waysFrom) {
85
85
  const values = {};
86
86
 
87
87
  for (const [name, index] of entries(waysFrom)) {
88
- if (isTemplateArray(name)) {
89
- debugger;
90
- const endOfArray = collectArray({
88
+ let end = index;
89
+
90
+ if (isTemplateArray(name))
91
+ end = collectArray({
92
+ currentTokenIndex: index,
93
+ tokens,
94
+ });
95
+ else if (isTemplateExpression(name))
96
+ end = collectExpression({
91
97
  currentTokenIndex: index,
92
98
  tokens,
93
- nextTemplateToken: Punctuator(';'),
94
99
  });
95
-
96
- if (index === endOfArray) {
97
- values[name] = tokens[index];
98
- continue;
99
- }
100
-
101
- values[name] = tokens.slice(index, endOfArray + 1);
102
- continue;
103
- }
104
100
 
105
- values[name] = tokens[index];
101
+ values[name] = tokens.slice(index, end + 1);
106
102
  }
107
103
 
108
104
  return values;
@@ -110,14 +106,6 @@ function getValues(tokens, waysFrom) {
110
106
 
111
107
  function setValues({to, waysTo, values}) {
112
108
  for (const [name, index] of entries(waysTo)) {
113
- const current = values[name];
114
-
115
- if (!isArray(current)) {
116
- to[index] = values[name];
117
- continue;
118
- }
119
-
120
109
  to.splice(index, 1, ...values[name]);
121
110
  }
122
111
  }
123
-
@@ -1,7 +1,6 @@
1
1
  import {replace} from './replacer.js';
2
2
 
3
3
  export const run = (tokens, {fix, fixCount = 10, plugins}) => {
4
- debugger;
5
4
  const places = [];
6
5
 
7
6
  while (--fixCount >= 0) {
@@ -12,7 +11,7 @@ export const run = (tokens, {fix, fixCount = 10, plugins}) => {
12
11
  plugin,
13
12
  }));
14
13
 
15
- if (!fix || places.length) {
14
+ if (!fix) {
16
15
  fixCount = 0;
17
16
  break;
18
17
  }
@@ -31,11 +31,13 @@ const LINKED_NODE = /^__[a-z]$/;
31
31
  const ANY = '__';
32
32
  const QUOTE = /^['"]$/;
33
33
  const ARRAY = '__array';
34
+ const EXPR = '__expr';
34
35
 
35
36
  const ALL = [
36
37
  ANY,
37
38
  LINKED_NODE,
38
39
  ARRAY,
40
+ EXPR,
39
41
  ];
40
42
 
41
43
  function check(str, item) {
@@ -48,4 +50,6 @@ function check(str, item) {
48
50
  export const isId = (a) => LINKED_NODE.test(a);
49
51
  export const isQuote = (a) => QUOTE.test(a);
50
52
  export const isTemplateArray = (a) => a === ARRAY;
53
+ export const isTemplateExpression = (a) => a === EXPR;
51
54
  export const isTemplateArrayToken = (a) => isIdentifier(a) && isTemplateArray(a.value);
55
+ export const isTemplateExpressionToken = (a) => isIdentifier(a) && isTemplateExpression(a.value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.13.0",
3
+ "version": "1.15.0",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",