jslike 1.3.1 → 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.
@@ -0,0 +1,103 @@
1
+ import { parse } from '../index.cjs';
2
+
3
+ /**
4
+ * WangValidator - Syntax validation for Wang/JSLike code
5
+ */
6
+
7
+
8
+ class WangValidator {
9
+ /**
10
+ * Validate code and return validation result
11
+ * @param {string} code - The code to validate
12
+ * @param {object} options - Validation options
13
+ * @param {boolean} options.includeAST - Whether to include AST in result
14
+ * @returns {object} Validation result
15
+ */
16
+ validate(code, options = {}) {
17
+ try {
18
+ // Parse the code
19
+ const ast = parse(code);
20
+
21
+ return {
22
+ valid: true,
23
+ ast: options.includeAST ? ast : undefined
24
+ };
25
+ } catch (error) {
26
+ // Extract line and column from error
27
+ const line = error.loc?.line || 1;
28
+ const column = error.loc?.column || 0;
29
+
30
+ // Generate suggestion for common errors
31
+ const suggestion = this.getSuggestion(error, code);
32
+
33
+ return {
34
+ valid: false,
35
+ error: {
36
+ message: error.message,
37
+ line,
38
+ column,
39
+ suggestion
40
+ }
41
+ };
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Generate suggestions for common syntax errors
47
+ * @param {Error} error - The parse error
48
+ * @param {string} code - The original code
49
+ * @returns {string|undefined} Suggestion text
50
+ */
51
+ getSuggestion(error, code) {
52
+ const message = error.message.toLowerCase();
53
+
54
+ // Common error patterns and suggestions
55
+ if (message.includes('unexpected token')) {
56
+ if (message.includes('}')) {
57
+ return 'Check for missing opening brace or extra closing brace';
58
+ }
59
+ if (message.includes('{')) {
60
+ return 'Check for missing closing brace or misplaced opening brace';
61
+ }
62
+ if (message.includes(')')) {
63
+ return 'Check for missing opening parenthesis or extra closing parenthesis';
64
+ }
65
+ if (message.includes('(')) {
66
+ return 'Check for missing closing parenthesis';
67
+ }
68
+ return 'Check syntax near the error location';
69
+ }
70
+
71
+ if (message.includes('unterminated string')) {
72
+ return 'Add closing quote to string literal';
73
+ }
74
+
75
+ if (message.includes('unterminated template')) {
76
+ return 'Add closing backtick to template literal';
77
+ }
78
+
79
+ if (message.includes('identifier expected')) {
80
+ return 'Provide a valid identifier name';
81
+ }
82
+
83
+ if (message.includes('unexpected end of input')) {
84
+ return 'Check for unclosed brackets, braces, or parentheses';
85
+ }
86
+
87
+ if (message.includes('invalid left-hand side')) {
88
+ return 'Check that assignment target is a valid variable or property';
89
+ }
90
+
91
+ if (message.includes('duplicate parameter')) {
92
+ return 'Remove duplicate parameter name in function definition';
93
+ }
94
+
95
+ if (message.includes('strict mode')) {
96
+ return 'This syntax is not allowed in strict mode';
97
+ }
98
+
99
+ return undefined;
100
+ }
101
+ }
102
+
103
+ export { WangValidator };
@@ -1,10 +1,11 @@
1
+ import { parse } from '../index.js';
2
+
1
3
  /**
2
4
  * WangValidator - Syntax validation for Wang/JSLike code
3
5
  */
4
6
 
5
- import { parse } from '../index.js';
6
7
 
7
- export class WangValidator {
8
+ class WangValidator {
8
9
  /**
9
10
  * Validate code and return validation result
10
11
  * @param {string} code - The code to validate
@@ -98,3 +99,5 @@ export class WangValidator {
98
99
  return undefined;
99
100
  }
100
101
  }
102
+
103
+ export { WangValidator };