flatlint 1.16.0 → 1.18.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.18.0
2
+
3
+ feature:
4
+ - 99d6dbf flatlint: remove-useless-arrow: add
5
+
6
+ 2025.01.02, v1.17.0
7
+
8
+ feature:
9
+ - a4056ef flatlint: remove-useless-semicolon: add
10
+
1
11
  2025.01.02, v1.16.0
2
12
 
3
13
  feature:
package/README.md CHANGED
@@ -87,6 +87,17 @@ npm i flatlint
87
87
 
88
88
  </details>
89
89
 
90
+ <details><summary>remove useless semicolon</summary>
91
+
92
+ ```diff
93
+ const a = {
94
+ - b: 'hello';
95
+ + b: 'hello',
96
+ }
97
+ ```
98
+
99
+ </details>
100
+
90
101
  <details><summary>add missing quote</summary>
91
102
 
92
103
  ```diff
@@ -99,6 +110,17 @@ npm i flatlint
99
110
 
100
111
  </details>
101
112
 
113
+ <details><summary>Remove useless arrow</summary>
114
+
115
+ ```diff
116
+ -function parse(source) => {
117
+ +function parse(source) {
118
+ return source;
119
+ }
120
+ ```
121
+
122
+ </details>
123
+
102
124
  ## Template literals
103
125
 
104
126
  **FlatLint** uses language similar to 🐊[**PutoutScript**](https://github.com/coderaiser/putout/blob/master/docs/putout-script.md#-putoutscript).
@@ -1,4 +1,7 @@
1
- import {Punctuator} from '#types';
1
+ import {
2
+ CloseRoundBrace,
3
+ Punctuator,
4
+ } from '#types';
2
5
  import {equal} from './equal.js';
3
6
 
4
7
  export const collectArray = ({currentTokenIndex, tokens, nextTemplateToken = Punctuator(';')}) => {
@@ -9,6 +12,9 @@ export const collectArray = ({currentTokenIndex, tokens, nextTemplateToken = Pun
9
12
  for (; index < n; index++) {
10
13
  const token = tokens[index];
11
14
 
15
+ if (equal(token, CloseRoundBrace))
16
+ break;
17
+
12
18
  if (equal(token, nextTemplateToken))
13
19
  break;
14
20
 
@@ -27,6 +27,12 @@ export const compare = (source, template) => {
27
27
  for (let index = 0; index < n; index++) {
28
28
  for (let templateIndex = 0; templateIndex < templateTokensLength; templateIndex++) {
29
29
  const currentTokenIndex = index + templateIndex;
30
+
31
+ if (currentTokenIndex > n) {
32
+ isEqual = false;
33
+ break;
34
+ }
35
+
30
36
  const templateToken = templateTokens[templateIndex];
31
37
  const currentToken = tokens[currentTokenIndex];
32
38
 
@@ -0,0 +1,5 @@
1
+ export const report = () => 'Remove useless arrow';
2
+
3
+ export const replace = () => ({
4
+ 'function __a(__array) => {': 'function __a(__array) {',
5
+ });
@@ -0,0 +1,18 @@
1
+ import {isIdentifier} from '#types';
2
+
3
+ export const report = () => 'Remove useless semicolon';
4
+
5
+ export const match = () => ({
6
+ '__a: __expr;': (vars, path) => {
7
+ for (const token of path.getPrev()) {
8
+ if (isIdentifier(token) && token.value === 'interface')
9
+ return false;
10
+ }
11
+
12
+ return true;
13
+ },
14
+ });
15
+
16
+ export const replace = () => ({
17
+ '__a: __expr;': '__a: __expr,',
18
+ });
@@ -0,0 +1,13 @@
1
+ export const createPath = ({tokens, start, end}) => {
2
+ return {
3
+ getPrev: createGetPrev({tokens, start}),
4
+ };
5
+ };
6
+
7
+ function createGetPrev({tokens, start}) {
8
+ return function*() {
9
+ for (let i = start; i >= 0; --i) {
10
+ yield tokens[i];
11
+ }
12
+ };
13
+ }
@@ -8,9 +8,35 @@ import {
8
8
  } from '#types';
9
9
  import {collectArray} from '../compare/collect-array.js';
10
10
  import {collectExpression} from '../compare/collect-expression.js';
11
+ import {createPath} from './path.js';
11
12
 
13
+ const returns = (a) => () => a;
12
14
  const {entries} = Object;
13
15
 
16
+ export const match = (tokens, {plugin}) => {
17
+ const match = plugin.match ?? returns([]);
18
+
19
+ for (const [from, fn] of entries(match())) {
20
+ const [ok, start, end] = compare(tokens, from);
21
+
22
+ if (!ok)
23
+ continue;
24
+
25
+ const current = tokens.slice(start, end);
26
+
27
+ const waysFrom = findVarsWays(prepare(from));
28
+ const values = getValues(current, waysFrom);
29
+
30
+ return fn(values, createPath({
31
+ tokens,
32
+ start,
33
+ end,
34
+ }));
35
+ }
36
+
37
+ return true;
38
+ };
39
+
14
40
  export const replace = (tokens, {fix, rule, plugin}) => {
15
41
  const places = [];
16
42
 
@@ -109,3 +135,4 @@ function setValues({to, waysTo, values}) {
109
135
  to.splice(index, 1, ...values[name]);
110
136
  }
111
137
  }
138
+
@@ -1,15 +1,16 @@
1
- import {replace} from './replacer.js';
1
+ import {match, replace} from './replacer.js';
2
2
 
3
3
  export const run = (tokens, {fix, fixCount = 10, plugins}) => {
4
4
  const places = [];
5
5
 
6
6
  while (--fixCount >= 0) {
7
7
  for (const {rule, plugin} of plugins) {
8
- places.push(...replace(tokens, {
9
- fix,
10
- rule,
11
- plugin,
12
- }));
8
+ if (match(tokens, {plugin}))
9
+ places.push(...replace(tokens, {
10
+ fix,
11
+ rule,
12
+ plugin,
13
+ }));
13
14
 
14
15
  if (!fix) {
15
16
  fixCount = 0;
@@ -53,3 +53,6 @@ export const isTemplateArray = (a) => a === ARRAY;
53
53
  export const isTemplateExpression = (a) => a === EXPR;
54
54
  export const isTemplateArrayToken = (a) => isIdentifier(a) && isTemplateArray(a.value);
55
55
  export const isTemplateExpressionToken = (a) => isIdentifier(a) && isTemplateExpression(a.value);
56
+
57
+ export const OpenRoundBrace = Punctuator('(');
58
+ export const CloseRoundBrace = Punctuator(')');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.16.0",
3
+ "version": "1.18.0",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",