flatlint 1.23.0 → 1.24.1

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.05, v1.24.1
2
+
3
+ feature:
4
+ - 4c6c3c9 flatlint: with-plugins: add-missing-semicolon
5
+
6
+ 2025.01.05, v1.24.0
7
+
8
+ feature:
9
+ - b6252d0 flatlint: path: getPrev -> getAllPrev
10
+
1
11
  2025.01.05, v1.23.0
2
12
 
3
13
  feature:
package/lib/flatlint.js CHANGED
@@ -2,6 +2,9 @@ import {loadPlugins} from '@putout/engine-loader';
2
2
  import {parse} from '#parser';
3
3
  import {run} from '#runner';
4
4
 
5
+ const getValue = (token) => token.value;
6
+ const print = (tokens) => tokens.map(getValue).join('');
7
+
5
8
  export function lint(source, overrides = {}) {
6
9
  const {fix = true, plugins: pluginNames = []} = overrides;
7
10
  const plugins = loadPlugins({
@@ -23,9 +26,3 @@ export function lint(source, overrides = {}) {
23
26
  places,
24
27
  ];
25
28
  }
26
-
27
- function print(tokens) {
28
- return tokens
29
- .map((token) => token.value)
30
- .join('');
31
- }
@@ -1,4 +1,8 @@
1
- import {isPunctuator} from '#types';
1
+ import {
2
+ comma,
3
+ isPunctuator,
4
+ semicolon,
5
+ } from '#types';
2
6
 
3
7
  export const report = () => 'Add missing semicolon';
4
8
 
@@ -13,11 +17,13 @@ export const replace = () => ({
13
17
  });
14
18
 
15
19
  function check(vars, path) {
16
- let last;
20
+ if (path.isEndsWithPunctuator(comma))
21
+ return false;
17
22
 
18
- for (const token of path.getNext()) {
19
- last = token;
23
+ for (const token of path.getAllNext()) {
24
+ if (isPunctuator(token, semicolon))
25
+ return false;
20
26
  }
21
27
 
22
- return !isPunctuator(last, ';');
28
+ return true;
23
29
  }
@@ -4,7 +4,7 @@ export const report = () => 'Remove useless coma';
4
4
 
5
5
  export const match = () => ({
6
6
  '__a(__args) {},': (vars, path) => {
7
- for (const token of path.getPrev()) {
7
+ for (const token of path.getAllPrev()) {
8
8
  if (isIdentifier(token, 'class'))
9
9
  return true;
10
10
  }
@@ -4,7 +4,7 @@ export const report = () => 'Remove useless semicolon';
4
4
 
5
5
  export const match = () => ({
6
6
  '__a: __expr;': (vars, path) => {
7
- for (const token of path.getPrev()) {
7
+ for (const token of path.getAllPrev()) {
8
8
  if (isIdentifier(token, 'interface'))
9
9
  return false;
10
10
  }
package/lib/plugins.js CHANGED
@@ -6,12 +6,14 @@ import * as convertCommaToSemicolon from './plugins/convert-comma-to-semicolon/i
6
6
  import * as convertFromToRequire from './plugins/convert-from-to-require/index.js';
7
7
  import * as removeUselessRoundBrace from './plugins/remove-useless-round-brace/index.js';
8
8
  import * as addConstToExport from './plugins/add-const-to-export/index.js';
9
+ import * as addMissingSemicolon from './plugins/add-missing-semicolon/index.js';
9
10
 
10
11
  export const plugins = [
11
12
  ['wrap-assignment-in-parens', wrapAssignmentInParens],
12
13
  ['add-missing-round-braces', addMissingRoundBraces],
13
14
  ['add-missing-squire-brace', addMissingSquireBrace],
14
15
  ['add-missing-quote', addMissingQuote],
16
+ ['add-missing-semicolon', addMissingSemicolon],
15
17
  ['add-const-to-export', addConstToExport],
16
18
  ['convert-comma-to-semicolon', convertCommaToSemicolon],
17
19
  ['convert-from-to-require', convertFromToRequire],
@@ -1,33 +1,59 @@
1
- import {isNewLine} from '#types';
1
+ import {isNewLine, isPunctuator} from '#types';
2
2
 
3
- export const createPath = ({tokens, start}) => ({
4
- getPrev: createGetPrev({
3
+ export const createPath = ({tokens, start, end}) => ({
4
+ getAllPrev: createGetAllPrev({
5
5
  tokens,
6
6
  start,
7
7
  }),
8
- getNext: createGetNext({
8
+ getAllNext: createGetAllNext({
9
9
  tokens,
10
- start,
10
+ end,
11
+ }),
12
+ isNextPunctuator: createIsNextPunctuator({
13
+ tokens,
14
+ end,
15
+ }),
16
+ isEndsWithPunctuator: createIsEndsWithPunctuator({
17
+ tokens,
18
+ end,
19
+ }),
20
+ isNext: createIsNext({
21
+ tokens,
22
+ end,
11
23
  }),
12
24
  });
13
25
 
14
- function createGetPrev({tokens, start}) {
15
- return function*() {
16
- for (let i = start; i >= 0; --i) {
17
- yield tokens[i];
18
- }
19
- };
20
- }
26
+ const createIsNextPunctuator = ({tokens, end}) => (punctuator) => {
27
+ const current = tokens[end];
28
+
29
+ if (!current)
30
+ return false;
31
+
32
+ return isPunctuator(current, punctuator);
33
+ };
34
+
35
+ const createIsEndsWithPunctuator = ({tokens, end}) => (punctuator) => {
36
+ const current = tokens[end - 1];
37
+ return isPunctuator(current, punctuator);
38
+ };
39
+
40
+ const createIsNext = ({tokens, end}) => () => {
41
+ return Boolean(tokens[end]);
42
+ };
43
+
44
+ const createGetAllPrev = ({tokens, start}) => function*() {
45
+ for (let i = start; i >= 0; --i) {
46
+ yield tokens[i];
47
+ }
48
+ };
21
49
 
22
- function createGetNext({tokens, start}) {
23
- return function*() {
24
- for (let i = start; i < tokens.length; ++i) {
25
- const current = tokens[i];
26
-
27
- if (isNewLine(current))
28
- continue;
29
-
30
- yield current;
31
- }
32
- };
33
- }
50
+ const createGetAllNext = ({tokens, end}) => function*() {
51
+ for (let i = end; i < tokens.length; ++i) {
52
+ const current = tokens[i];
53
+
54
+ if (isNewLine(current))
55
+ continue;
56
+
57
+ yield current;
58
+ }
59
+ };
@@ -136,6 +136,6 @@ function getCurrentValues({from, start, end, tokens}) {
136
136
  const current = tokens.slice(start, end);
137
137
 
138
138
  const waysFrom = findVarsWays(prepare(from));
139
+
139
140
  return getValues(current, waysFrom);
140
141
  }
141
-
@@ -15,11 +15,16 @@ export const isStringLiteral = ({type}) => type === 'StringLiteral';
15
15
  export const isNumericLiteral = ({type}) => type === 'NumericLiteral';
16
16
  export const isNewLine = ({type}) => type === 'LineTerminatorSequence';
17
17
  export const notWhiteSpace = (a) => !isWhiteSpace(a);
18
- export const isPunctuator = (token, value) => {
18
+ export const isPunctuator = (token, punctuator) => {
19
19
  if (token.type !== 'Punctuator')
20
20
  return false;
21
21
 
22
- return !value || token.value === value;
22
+ if (!punctuator)
23
+ return true;
24
+
25
+ const punctuatorValue = punctuator.value || punctuator;
26
+
27
+ return token.value === punctuatorValue;
23
28
  };
24
29
 
25
30
  export const StringLiteral = (value) => ({
@@ -76,6 +81,7 @@ export const openRoundBrace = Punctuator('(');
76
81
  export const closeRoundBrace = Punctuator(')');
77
82
  export const closeSquareBrace = Punctuator(']');
78
83
  export const semicolon = Punctuator(';');
84
+ export const comma = Punctuator(',');
79
85
 
80
86
  export const OK = true;
81
87
  export const NOT_OK = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.23.0",
3
+ "version": "1.24.1",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",