flatlint 1.5.0 → 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,8 @@
1
+ 2024.12.29, v1.5.1
2
+
3
+ feature:
4
+ - d65021c flatlint: parser: preprocess: improve
5
+
1
6
  2024.12.29, v1.5.0
2
7
 
3
8
  feature:
@@ -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;
@@ -0,0 +1,61 @@
1
+ import tokenize from 'js-tokens';
2
+ import {
3
+ isNewLine,
4
+ isStringLiteral,
5
+ } from '#types';
6
+ import {parseStringLiteral} from './string-literal.js';
7
+
8
+ const isString = (a) => typeof a === 'string';
9
+
10
+ const preprocess = (tokens) => {
11
+ const n = tokens.length;
12
+
13
+ for (let i = 0; i < n; i++) {
14
+ const token = tokens[i];
15
+
16
+ if (isStringLiteral(token))
17
+ i = parseStringLiteral({
18
+ token,
19
+ tokens,
20
+ i,
21
+ });
22
+ }
23
+ };
24
+
25
+ export const prepare = (a) => {
26
+ if (!isString(a))
27
+ return a;
28
+
29
+ const array = Array.from(tokenize(a));
30
+ preprocess(array);
31
+
32
+ return array;
33
+ };
34
+
35
+ export const parse = (source) => {
36
+ return getTokensWithLocation(prepare(source));
37
+ };
38
+
39
+ function getTokensWithLocation(tokens) {
40
+ let line = 1;
41
+ let column = 1;
42
+ const result = [];
43
+
44
+ for (const token of tokens) {
45
+ if (isNewLine(token))
46
+ ++line;
47
+
48
+ result.push({
49
+ ...token,
50
+ line,
51
+ column,
52
+ });
53
+
54
+ column += token.value.length;
55
+
56
+ if (isNewLine(token))
57
+ column = 1;
58
+ }
59
+
60
+ return result;
61
+ }
@@ -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
+ }
@@ -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.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",
@@ -1,89 +0,0 @@
1
- import tokenize from 'js-tokens';
2
- import {
3
- isNewLine,
4
- isStringLiteral,
5
- Punctuator,
6
- StringLiteral,
7
- } from '#types';
8
-
9
- const isString = (a) => typeof a === 'string';
10
-
11
- const closeQuotes = (tokens) => {
12
- const n = tokens.length;
13
-
14
- for (let i = 0; i < n; i++) {
15
- const token = tokens[i];
16
-
17
- if (isStringLiteral(token)) {
18
- const {closed, value} = token;
19
-
20
- const quote = Punctuator(value.at(0));
21
- const newTokens = [quote];
22
-
23
- if (closed) {
24
- const literal = StringLiteral(value.slice(1, -1));
25
- newTokens.push(literal, quote);
26
- } else if (value.endsWith(');')) {
27
- const literal = StringLiteral(value.slice(1, -2));
28
- const brace = Punctuator(')');
29
- const semicolon = Punctuator(';');
30
-
31
- newTokens.push(literal, brace, semicolon);
32
- } else if (value.endsWith(')')) {
33
- const literal = StringLiteral(value.slice(1, -1));
34
- const brace = Punctuator(')');
35
-
36
- newTokens.push(literal, brace);
37
- } else if (value.endsWith(';')) {
38
- const literal = StringLiteral(value.slice(1, -1));
39
- const semicolon = Punctuator(';');
40
-
41
- newTokens.push(literal, semicolon);
42
- } else {
43
- const literal = StringLiteral(value.slice(1));
44
- newTokens.push(literal);
45
- }
46
-
47
- tokens.splice(i, 1, ...newTokens);
48
- ++i;
49
- }
50
- }
51
- };
52
-
53
- export const prepare = (a) => {
54
- if (!isString(a))
55
- return a;
56
-
57
- const array = Array.from(tokenize(a));
58
- closeQuotes(array);
59
-
60
- return array;
61
- };
62
-
63
- export const parse = (source) => {
64
- return getTokensWithLocation(prepare(source));
65
- };
66
-
67
- function getTokensWithLocation(tokens) {
68
- let line = 1;
69
- let column = 1;
70
- const result = [];
71
-
72
- for (const token of tokens) {
73
- if (isNewLine(token))
74
- ++line;
75
-
76
- result.push({
77
- ...token,
78
- line,
79
- column,
80
- });
81
-
82
- column += token.value.length;
83
-
84
- if (isNewLine(token))
85
- column = 1;
86
- }
87
-
88
- return result;
89
- }