flatlint 1.15.0 → 1.17.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.17.0
2
+
3
+ feature:
4
+ - a4056ef flatlint: remove-useless-semicolon: add
5
+
6
+ 2025.01.02, v1.16.0
7
+
8
+ feature:
9
+ - bad8496 flatlint: remove-useless-squire-brace: add
10
+
1
11
  2025.01.02, v1.15.0
2
12
 
3
13
  feature:
package/README.md CHANGED
@@ -78,6 +78,26 @@ npm i flatlint
78
78
 
79
79
  </details>
80
80
 
81
+ <details><summary>remove useless square brace</summary>
82
+
83
+ ```diff
84
+ -const a = [1, 2, 3]];
85
+ +const a = [1, 2, 3];
86
+ ```
87
+
88
+ </details>
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
+
81
101
  <details><summary>add missing quote</summary>
82
102
 
83
103
  ```diff
@@ -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
 
@@ -1,5 +1,5 @@
1
1
  export function report() {
2
- return 'Add missing squire brace';
2
+ return 'Add missing square brace';
3
3
  }
4
4
 
5
5
  export function replace() {
@@ -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,5 @@
1
+ export const report = () => 'Remove useless square brace';
2
+
3
+ export const replace = () => ({
4
+ 'const __a = [__array]];': 'const __a = [__array];',
5
+ });
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.15.0",
3
+ "version": "1.17.0",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",