flatlint 1.3.0 → 1.4.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,8 @@
1
+ 2024.12.29, v1.4.0
2
+
3
+ feature:
4
+ - bb670ef flatlint: add-missing-quote: add
5
+
1
6
  2024.12.29, v1.3.0
2
7
 
3
8
  feature:
package/README.md CHANGED
@@ -48,6 +48,15 @@ npm i flatlint
48
48
 
49
49
  </details>
50
50
 
51
+ <details><summary>add missing quote</summary>
52
+
53
+ ```diff
54
+ -const a = 'hello
55
+ +const a = 'hello'
56
+ ```
57
+
58
+ </details>
59
+
51
60
  ## API
52
61
 
53
62
  ```js
@@ -3,11 +3,14 @@ import {
3
3
  isId,
4
4
  isIdentifier,
5
5
  isPunctuator,
6
+ isQuote,
7
+ isStringLiteral,
6
8
  } from './types.js';
7
9
 
8
10
  export const compare = (source, template) => {
9
11
  const templateTokens = prepare(template);
10
12
  const tokens = prepare(source);
13
+
11
14
  const n = tokens.length - 1;
12
15
  const templateTokensLength = templateTokens.length;
13
16
  let isEqual = false;
@@ -17,6 +20,12 @@ export const compare = (source, template) => {
17
20
  for (let index = 0; index < n; index++) {
18
21
  for (let templateIndex = 0; templateIndex < templateTokensLength; templateIndex++) {
19
22
  const currentTokenIndex = index + templateIndex;
23
+
24
+ if (currentTokenIndex > n) {
25
+ isEqual = false;
26
+ break;
27
+ }
28
+
20
29
  const templateToken = templateTokens[templateIndex];
21
30
  const currentToken = tokens[currentTokenIndex];
22
31
 
@@ -40,7 +49,9 @@ export const compare = (source, template) => {
40
49
  const comparators = [
41
50
  equal,
42
51
  equalId,
52
+ equalStr,
43
53
  equalAny,
54
+ equalQuote,
44
55
  ];
45
56
 
46
57
  function compareAll(a, b) {
@@ -66,6 +77,20 @@ function equalAny(a, b) {
66
77
  return b.value === '__';
67
78
  }
68
79
 
80
+ function equalQuote(a, b) {
81
+ if (!isPunctuator(a))
82
+ return false;
83
+
84
+ return isQuote(b.value);
85
+ }
86
+
87
+ function equalStr(a, b) {
88
+ if (!isStringLiteral(a))
89
+ return false;
90
+
91
+ return isId(b.value);
92
+ }
93
+
69
94
  function equalId(a, b) {
70
95
  if (!isIdentifier(b))
71
96
  return false;
@@ -2,6 +2,7 @@ const isString = (a) => typeof a === 'string';
2
2
 
3
3
  export const isWhiteSpace = ({type}) => type === 'WhiteSpace';
4
4
  export const isIdentifier = ({type}) => type === 'IdentifierName';
5
+ export const isStringLiteral = ({type}) => type === 'StringLiteral';
5
6
  export const isNumericLiteral = ({type}) => type === 'NumericLiteral';
6
7
  export const isNewLine = ({type}) => type === 'LineTerminatorSequence';
7
8
  export const notWhiteSpace = (a) => !isWhiteSpace(a);
@@ -18,6 +19,7 @@ export const is = (str, array = ALL) => {
18
19
 
19
20
  const LINKED_NODE = /^__[a-z]$/;
20
21
  const ANY = '__';
22
+ const QUOTE = /^['"]$/;
21
23
 
22
24
  const ALL = [ANY, LINKED_NODE];
23
25
 
@@ -29,3 +31,4 @@ function check(str, item) {
29
31
  }
30
32
 
31
33
  export const isId = (a) => LINKED_NODE.test(a);
34
+ export const isQuote = (a) => QUOTE.test(a);
@@ -0,0 +1,9 @@
1
+ export function report() {
2
+ return 'Add missing quote';
3
+ }
4
+
5
+ export function replace() {
6
+ return {
7
+ 'const __a = "__b': 'const __a = "__b"',
8
+ };
9
+ }
@@ -5,5 +5,6 @@ export function report() {
5
5
  export function replace() {
6
6
  return {
7
7
  'const __a = __b)': 'const __a = __b',
8
+ 'const __a = "__b")': 'const __a = "__b"',
8
9
  };
9
10
  }
package/lib/plugins.js CHANGED
@@ -1,11 +1,13 @@
1
1
  import * as wrapAssignmentInParens from './plugins/wrap-assignment-in-parens/index.js';
2
2
  import * as addMissingRoundBraces from './plugins/add-missing-round-braces/index.js';
3
+ import * as addMissingQuote from './plugins/add-missing-quote/index.js';
3
4
  import * as convertCommaToSemicolon from './plugins/convert-comma-to-semicolon/index.js';
4
5
  import * as removeUselessRoundBrace from './plugins/remove-useless-round-brace/index.js';
5
6
 
6
7
  export const plugins = [
7
8
  ['wrap-assignment-in-parens', wrapAssignmentInParens],
8
9
  ['add-missing-round-braces', addMissingRoundBraces],
10
+ ['add-missing-quote', addMissingQuote],
9
11
  ['convert-comma-to-semicolon', convertCommaToSemicolon],
10
12
  ['remove-useless-round-brace', removeUselessRoundBrace],
11
13
  ];
@@ -66,6 +66,10 @@ function findVarsWays(tokens) {
66
66
  if (is(value))
67
67
  ways[value] = index;
68
68
  },
69
+ StringLiteral({value, index}) {
70
+ if (is(value))
71
+ ways[value] = index;
72
+ },
69
73
  });
70
74
 
71
75
  return ways;
@@ -1,13 +1,57 @@
1
1
  import tokenize from 'js-tokens';
2
- import {isNewLine} from '../compare/types.js';
2
+ import {
3
+ isNewLine,
4
+ isStringLiteral,
5
+ } from '../compare/types.js';
3
6
 
4
7
  const isString = (a) => typeof a === 'string';
5
8
 
9
+ const closeQuotes = (tokens) => {
10
+ const n = tokens.length;
11
+
12
+ for (let i = 0; i < n; i++) {
13
+ const token = tokens[i];
14
+
15
+ if (isStringLiteral(token)) {
16
+ const {closed, value} = token;
17
+
18
+ const quote = {
19
+ type: 'Punctuator',
20
+ value: value.at(0),
21
+ };
22
+
23
+ const newTokens = [];
24
+
25
+ if (closed) {
26
+ const literal = {
27
+ value: value.slice(1, -1),
28
+ type: 'StringLiteral',
29
+ };
30
+
31
+ newTokens.push(quote, literal, quote);
32
+ } else {
33
+ const literal = {
34
+ value: value.slice(1),
35
+ type: 'StringLiteral',
36
+ };
37
+
38
+ newTokens.push(quote, literal);
39
+ }
40
+
41
+ tokens.splice(i, 1, ...newTokens);
42
+ ++i;
43
+ }
44
+ }
45
+ };
46
+
6
47
  export const prepare = (a) => {
7
- if (isString(a))
8
- return Array.from(tokenize(a));
48
+ if (!isString(a))
49
+ return a;
50
+
51
+ const array = Array.from(tokenize(a));
52
+ closeQuotes(array);
9
53
 
10
- return a;
54
+ return array;
11
55
  };
12
56
 
13
57
  export const parse = (source) => {
@@ -1,13 +1,26 @@
1
- import {isIdentifier} from '../compare/types.js';
1
+ import {
2
+ isIdentifier,
3
+ isStringLiteral,
4
+ } from '../compare/types.js';
2
5
 
3
6
  const maybeCall = (fn, a) => fn?.(a);
4
7
 
5
8
  export const traverse = (tokens, visitors = {}) => {
6
9
  for (const [index, token] of tokens.entries()) {
7
- if (isIdentifier(token))
10
+ if (isIdentifier(token)) {
8
11
  maybeCall(visitors.Identifier, {
9
12
  ...token,
10
13
  index,
11
14
  });
15
+ continue;
16
+ }
17
+
18
+ if (isStringLiteral(token)) {
19
+ maybeCall(visitors.StringLiteral, {
20
+ ...token,
21
+ index,
22
+ });
23
+ continue;
24
+ }
12
25
  }
13
26
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",