flatlint 1.13.0 β†’ 1.14.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,8 @@
1
+ 2025.01.01, v1.14.0
2
+
3
+ feature:
4
+ - 2361298 flatlint: __expr
5
+
1
6
  2025.01.01, v1.13.0
2
7
 
3
8
  feature:
package/README.md CHANGED
@@ -95,6 +95,7 @@ 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
+
98
99
  - βœ…it may not be valid **JavaScript**, it can be couple tokens that can be fixed;
99
100
  - βœ…it counts each symbol as a token;
100
101
 
@@ -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
@@ -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}) => {
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,12 +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,
7
+ isTemplateExpression,
4
8
  Punctuator,
5
9
  } from '#types';
6
- import {prepare} from '#parser';
7
- import {compare} from '#compare';
8
- import {traverse} from '#traverser';
9
10
  import {collectArray} from '../compare/collect-array.js';
11
+ import {collectExpression} from '../compare/collect-expression.js';
10
12
 
11
13
  const {isArray} = Array;
12
14
  const {entries} = Object;
@@ -86,7 +88,6 @@ function getValues(tokens, waysFrom) {
86
88
 
87
89
  for (const [name, index] of entries(waysFrom)) {
88
90
  if (isTemplateArray(name)) {
89
- debugger;
90
91
  const endOfArray = collectArray({
91
92
  currentTokenIndex: index,
92
93
  tokens,
@@ -102,6 +103,22 @@ function getValues(tokens, waysFrom) {
102
103
  continue;
103
104
  }
104
105
 
106
+ if (isTemplateExpression(name)) {
107
+ const endOfArray = collectExpression({
108
+ currentTokenIndex: index,
109
+ tokens,
110
+ nextTemplateToken: Punctuator(';'),
111
+ });
112
+
113
+ if (index === endOfArray) {
114
+ values[name] = tokens[index];
115
+ continue;
116
+ }
117
+
118
+ values[name] = tokens.slice(index, endOfArray + 1);
119
+ continue;
120
+ }
121
+
105
122
  values[name] = tokens[index];
106
123
  }
107
124
 
@@ -120,4 +137,3 @@ function setValues({to, waysTo, values}) {
120
137
  to.splice(index, 1, ...values[name]);
121
138
  }
122
139
  }
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.14.0",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",