flatlint 1.4.1 → 1.5.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
+ 2024.12.29, v1.5.1
2
+
3
+ feature:
4
+ - d65021c flatlint: parser: preprocess: improve
5
+
6
+ 2024.12.29, v1.5.0
7
+
8
+ feature:
9
+ - 5ff09d3 flatlint: add-missing-quote: call
10
+
1
11
  2024.12.29, v1.4.1
2
12
 
3
13
  feature:
package/README.md CHANGED
@@ -53,6 +53,9 @@ npm i flatlint
53
53
  ```diff
54
54
  -const a = 'hello
55
55
  +const a = 'hello'
56
+
57
+ -fn('hello);
58
+ +fn('hello');
56
59
  ```
57
60
 
58
61
  </details>
@@ -1,4 +1,4 @@
1
- import {prepare} from '../tokenizer/index.js';
1
+ import {prepare} from '../parser/index.js';
2
2
  import {
3
3
  isId,
4
4
  isIdentifier,
package/lib/flatlint.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {loadPlugins} from '@putout/engine-loader';
2
2
  import {run} from './runner/runner.js';
3
- import {parse} from './tokenizer/index.js';
3
+ import {parse} from './parser/index.js';
4
4
 
5
5
  export function lint(source, overrides = {}) {
6
6
  const {fix = true, plugins: pluginNames = []} = overrides;
@@ -2,40 +2,23 @@ import tokenize from 'js-tokens';
2
2
  import {
3
3
  isNewLine,
4
4
  isStringLiteral,
5
- Punctuator,
6
- StringLiteral,
7
5
  } from '#types';
6
+ import {parseStringLiteral} from './string-literal.js';
8
7
 
9
8
  const isString = (a) => typeof a === 'string';
10
9
 
11
- const closeQuotes = (tokens) => {
10
+ const preprocess = (tokens) => {
12
11
  const n = tokens.length;
13
12
 
14
13
  for (let i = 0; i < n; i++) {
15
14
  const token = tokens[i];
16
15
 
17
- if (isStringLiteral(token)) {
18
- const {closed, value} = token;
19
-
20
- const quote = Punctuator(value.at(0));
21
- const newTokens = [];
22
-
23
- if (closed) {
24
- const literal = StringLiteral(value.slice(1, -1));
25
- newTokens.push(quote, literal, quote);
26
- } else if (value.endsWith(';')) {
27
- const literal = StringLiteral(value.slice(1, -1));
28
- const semicolon = Punctuator(';');
29
-
30
- newTokens.push(quote, literal, semicolon);
31
- } else {
32
- const literal = StringLiteral(value.slice(1));
33
- newTokens.push(quote, literal);
34
- }
35
-
36
- tokens.splice(i, 1, ...newTokens);
37
- ++i;
38
- }
16
+ if (isStringLiteral(token))
17
+ i = parseStringLiteral({
18
+ token,
19
+ tokens,
20
+ i,
21
+ });
39
22
  }
40
23
  };
41
24
 
@@ -44,7 +27,7 @@ export const prepare = (a) => {
44
27
  return a;
45
28
 
46
29
  const array = Array.from(tokenize(a));
47
- closeQuotes(array);
30
+ preprocess(array);
48
31
 
49
32
  return array;
50
33
  };
@@ -0,0 +1,47 @@
1
+ import {Punctuator, StringLiteral} from '#types';
2
+
3
+ export function parseStringLiteral({i, token, tokens}) {
4
+ const {closed} = token;
5
+ let {value} = token;
6
+
7
+ const quote = Punctuator(value.at(0));
8
+ const newTokens = [quote];
9
+
10
+ if (closed) {
11
+ const literal = StringLiteral(value.slice(1, -1));
12
+ newTokens.push(literal, quote);
13
+ tokens.splice(i, 1, ...newTokens);
14
+ ++i;
15
+ } else {
16
+ let count = 0;
17
+
18
+ if (value.endsWith(';')) {
19
+ const semicolon = Punctuator(';');
20
+
21
+ value = value.slice(0, -1);
22
+ newTokens.push(semicolon);
23
+ ++count;
24
+ }
25
+
26
+ if (value.endsWith(')')) {
27
+ const brace = Punctuator(')');
28
+
29
+ value = value.slice(0, -1);
30
+ const {length} = newTokens;
31
+ const start = !count ? length : length - 1;
32
+
33
+ newTokens.splice(start, 0, brace);
34
+ ++count;
35
+ }
36
+
37
+ ++count;
38
+
39
+ const literal = StringLiteral(value.slice(1));
40
+ newTokens.splice(1, 0, literal);
41
+
42
+ tokens.splice(i, 1, ...newTokens);
43
+ i += count;
44
+ }
45
+
46
+ return i;
47
+ }
@@ -5,5 +5,6 @@ export function report() {
5
5
  export function replace() {
6
6
  return {
7
7
  'const __a = "__b': 'const __a = "__b"',
8
+ '__a("__b)': '__a("__b")',
8
9
  };
9
10
  }
@@ -1,7 +1,7 @@
1
1
  import {compare} from '../compare/compare.js';
2
2
  import {traverse} from '../traverser/traverser.js';
3
3
  import {is} from '../types/types.js';
4
- import {prepare} from '../tokenizer/index.js';
4
+ import {prepare} from '../parser/index.js';
5
5
 
6
6
  const {entries} = Object;
7
7
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.4.1",
3
+ "version": "1.5.1",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",