Skip to content

Commit b5b32ea

Browse files
committed
v0.1.0
2 parents 4193213 + 8f777a4 commit b5b32ea

File tree

6 files changed

+1076
-1244
lines changed

6 files changed

+1076
-1244
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Changelog
2+
3+
All notable changes to this library will be documented in this file.
4+
5+
## 0.1.0 - 2020-07-02
6+
7+
### 🚨 Breaking Changes
8+
9+
- Throw errors instead of returning `{ parse_error: ... }` objects
10+
- Migrate from `@babel/parser` to `meriyah` (all error messages changed slightly)
11+
- Return `null` when input is empty script
12+
- Ignore `'use strict'` and other directives
13+
14+
## 0.0.1 to 0.0.5 - 2002-01-12
15+
16+
- Early Releases

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,17 @@ Notes:
8686

8787
- If a specific syntactic JS feature is not specified in the table above, it's likely that it isn't supported. If you have an idea on how to support said feature, feel free to file a GitHub Issue.
8888

89+
### In Plans to Support
90+
91+
- Optional Chaining
92+
- Nullish Coalescing
93+
8994
### Unsupported Syntax
9095

9196
The following syntactic features are not supported by this module.
9297

9398
- Class Declarations
99+
- Private name expressions `#myPrivateProperty`
94100
- Update Expressions (`i++`, `i--`, etc.)
95101
- Assignment Expressions
96102
- Tagged Template Expressions

lib/index.js

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { parse } = require('@babel/parser')
1+
const { parseScript: parse } = require('meriyah')
22

33
const processOp = (operator) => {
44
switch (operator) {
@@ -27,31 +27,22 @@ const replaceVariable = (name) => (rule) => {
2727
}
2828
}
2929

30-
function processError ({ loc }, message) {
31-
return {
32-
parsing_error: {
33-
message,
34-
at: loc
35-
}
36-
}
30+
function processError ({ loc, at }, message) {
31+
const error = new Error(message)
32+
error.at = at || loc
33+
throw error
3734
}
3835

3936
function processNode (node, valueOnly = false) {
37+
if (!node) return null
38+
4039
switch (node.type) {
41-
case 'File': {
42-
return processNode(node.program)
43-
}
4440
case 'Program': {
45-
if (node.directives.length > 0) {
46-
return node.directives.length > 1
47-
? processError(node, 'Only one expression statement allowed.')
48-
: processNode(node.directives[0])
49-
}
50-
5141
return node.body.length > 1
52-
? processError(node, 'Block statements can only have one expression statement.')
42+
? processError(node, 'Only one expression statement allowed.')
5343
: processNode(node.body[0])
5444
}
45+
5546
case 'TemplateLiteral': {
5647
const nodes = []
5748
const expressions = node.expressions
@@ -74,29 +65,19 @@ function processNode (node, valueOnly = false) {
7465
cat: nodes
7566
}
7667
}
77-
case 'Directive': {
78-
// directives will be parsed as strings to allow rules to be just a plain string
79-
return processNode(node.value)
80-
}
81-
case 'DirectiveLiteral':
82-
case 'BooleanLiteral':
83-
case 'StringLiteral':
84-
case 'NumericLiteral': {
85-
return node.value
86-
}
8768

88-
case 'NullLiteral': {
89-
return null
69+
case 'Literal': {
70+
if (node.value instanceof RegExp) {
71+
return [node.value.source, node.value.flags]
72+
}
73+
74+
return node.value
9075
}
9176

9277
case 'SpreadElement': {
9378
return processNode(node.argument)
9479
}
9580

96-
case 'RegExpLiteral': {
97-
return [node.pattern, node.flags]
98-
}
99-
10081
case 'ArrayExpression': {
10182
// handle spread operators [1, 2, ...myArray]
10283
if (node.elements.some((node) => node.type === 'SpreadElement')) {
@@ -157,7 +138,7 @@ function processNode (node, valueOnly = false) {
157138
}
158139
}
159140

160-
if (node.operator === '-' && node.argument.type === 'NumericLiteral') {
141+
if (node.operator === '-' && typeof node.argument.value === 'number') {
161142
return node.argument.value * -1
162143
}
163144

@@ -246,6 +227,9 @@ function processNode (node, valueOnly = false) {
246227
case 'AssignmentExpression': {
247228
return processError(node, 'Assignments not supported.')
248229
}
230+
case 'PrivateName': {
231+
return processError(node, 'Private names are not supported. Unexpected character \'#\'')
232+
}
249233
/** catch other unsupported features */
250234
default: {
251235
return processError(node, `Invalid node '${node.type}'. Not supported.`)
@@ -255,13 +239,29 @@ function processNode (node, valueOnly = false) {
255239

256240
const transformJS = (code) => {
257241
try {
258-
return processNode(parse(code, { strictMode: true, errorRecovery: true }))
242+
return processNode(
243+
parse(code, {
244+
loc: true,
245+
next: true,
246+
module: false,
247+
impliedStrict: true,
248+
directives: false
249+
})
250+
)
259251
} catch (e) {
260252
return processError({
261-
loc: {
262-
start: e.loc,
263-
end: e.loc
264-
}
253+
at: e.loc
254+
? ({
255+
start: {
256+
line: e.loc.line,
257+
column: e.loc.column
258+
},
259+
end: {
260+
line: e.loc.line,
261+
column: e.loc.column
262+
}
263+
})
264+
: e.at
265265
}, `Could not parse code. ${e.message}`)
266266
}
267267
}

0 commit comments

Comments
 (0)