Skip to content

Commit 23025bc

Browse files
authored
acorn 8, esm, mocha (#205)
* remove husky * adopt esm, mocha * acorn 8 * restore .db.sql test * simpler serialization * re-enable reference tests * add private field test * restore peekId * yarn upgrade * remove unused rollup plugins * remove sublime project * test Markdown interpolation
1 parent a9da65a commit 23025bc

File tree

179 files changed

+8598
-10741
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+8598
-10741
lines changed

.eslintignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
dist/
2-
test/
3-
tap-snapshots/
2+
test/input/

.eslintrc.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
"extends": "eslint:recommended",
33
"parserOptions": {
44
"sourceType": "module",
5-
"ecmaVersion": 2018
5+
"ecmaVersion": 2020
66
},
77
"env": {
88
"es6": true,
99
"node": true
1010
},
1111
"rules": {
12-
"semi": 2,
1312
"no-cond-assign": 0,
14-
"no-process-env": 2,
1513
"no-var": 2,
14+
"comma-dangle": ["error", "never"],
15+
"semi": [2, "always"],
1616
"quotes": ["error", "double", {"allowTemplateLiterals": true}]
1717
}
1818
}

.github/eslint.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "eslint-compact",
5+
"pattern": [
6+
{
7+
"regexp": "^(.+):\\sline\\s(\\d+),\\scol\\s(\\d+),\\s(Error|Warning|Info)\\s-\\s(.+)\\s\\((.+)\\)$",
8+
"file": 1,
9+
"line": 2,
10+
"column": 3,
11+
"severity": 4,
12+
"message": 5,
13+
"code": 6
14+
}
15+
]
16+
}
17+
]
18+
}

.github/workflows/nodejs.yml

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
name: Node CI
1+
# https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
22

3-
on: [push]
3+
name: Node.js CI
4+
5+
on:
6+
push:
7+
branches: [ main ]
8+
pull_request:
9+
branches: [ main ]
410

511
jobs:
612
build:
713

14+
runs-on: ubuntu-latest
15+
816
strategy:
917
matrix:
10-
os: [ubuntu-latest]
11-
node-version: [10.x, 12.x]
12-
13-
runs-on: ${{ matrix.os }}
18+
node-version: [14.x]
1419

1520
steps:
16-
- uses: actions/checkout@v1
21+
- uses: actions/checkout@v2
1722
- name: Use Node.js ${{ matrix.node-version }}
1823
uses: actions/setup-node@v1
1924
with:
2025
node-version: ${{ matrix.node-version }}
21-
- name: yarn install and test
22-
run: |
23-
yarn install --frozen-lockfile
24-
yarn test
25-
env:
26-
CI: true
26+
- run: yarn --frozen-lockfile
27+
- run: |
28+
echo ::add-matcher::.github/eslint.json
29+
yarn run eslint . --format=compact
30+
- run: yarn test

.gitignore

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
*.sublime-workspace
21
.DS_Store
3-
.nyc_output
42
dist/
5-
node_modules
6-
npm-debug.log
7-
.idea
3+
node_modules/

README.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# @observablehq/parser
22

3-
[![Node CI](https://github.com/observablehq/parser/workflows/Node%20CI/badge.svg)](https://github.com/observablehq/parser/actions?workflow=Node+CI)
4-
53
To parse a cell:
64

75
```js
@@ -423,15 +421,11 @@ Injecting a view injects both the view symbol (`viewof foo`) and the value symbo
423421

424422
## API Reference
425423

426-
<a href="#parseCell" name="parseCell">#</a> <b>parseCell</b>(<i>input</i>[, <i>options</i>]) [<>](https://github.com/observablehq/parser/blob/master/src/parse.js "Source")
424+
<a href="#parseCell" name="parseCell">#</a> <b>parseCell</b>(<i>input</i>[, <i>options</i>]) [<>](https://github.com/observablehq/parser/blob/main/src/parse.js "Source")
427425

428426
Returns a [cell](#cell).
429427

430-
<a href="#parseModule" name="parseModule">#</a> <b>parseModule</b>(<i>input</i>[, <i>options</i>]) [<>](https://github.com/observablehq/parser/blob/master/src/parse.js "Source")
431-
432-
Returns a [program](#program).
433-
434-
<a href="#peekId" name="peekId">#</a> <b>peekId</b>(<i>input</i>) [<>](https://github.com/observablehq/parser/blob/master/src/parse.js "Source")
428+
<a href="#peekId" name="peekId">#</a> <b>peekId</b>(<i>input</i>) [<>](https://github.com/observablehq/parser/blob/main/src/peek.js "Source")
435429

436430
Tries to find the ID of a cell given a snippet of its contents, and returns it as a string if found.
437431

package.json

+31-23
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
11
{
22
"name": "@observablehq/parser",
3+
"description": "An Acorn parser for Observable JavaScript",
34
"version": "5.0.0",
4-
"license": "ISC",
5-
"main": "dist/parser.min.js",
6-
"module": "src/index.js",
75
"author": {
86
"name": "Observable, Inc.",
97
"url": "https://observablehq.com"
108
},
9+
"license": "ISC",
10+
"type": "module",
11+
"main": "src/index.js",
12+
"module": "src/index.js",
13+
"jsdelivr": "dist/parser.min.js",
14+
"unpkg": "dist/parser.min.js",
15+
"exports": {
16+
"umd": "./dist/parser.min.js",
17+
"default": "./src/index.js"
18+
},
1119
"repository": {
1220
"type": "git",
1321
"url": "https://github.com/observablehq/parser.git"
1422
},
23+
"files": [
24+
"dist/**/*.js",
25+
"src/**/*.js"
26+
],
27+
"engines": {
28+
"node": ">=14.5.0"
29+
},
1530
"scripts": {
16-
"test": "eslint . && tap 'test/**/*-test.js'",
17-
"snapshot": "TAP_SNAPSHOT=1 tap 'test/**/*-test.js'",
31+
"test": "mkdir -p test/output && mocha -r module-alias/register 'test/**/*-test.js' && eslint src test",
1832
"prepublishOnly": "rm -rf dist && rollup -c",
1933
"postpublish": "git push && git push --tags"
2034
},
21-
"husky": {
22-
"hooks": {
23-
"pre-commit": "yarn test"
24-
}
35+
"_moduleAliases": {
36+
"@observablehq/parser": "./src/index.js"
2537
},
2638
"dependencies": {
27-
"acorn": "^7.1.1",
28-
"acorn-walk": "^7.0.0"
39+
"acorn": "8",
40+
"acorn-walk": "8"
2941
},
3042
"devDependencies": {
31-
"eslint": "^6.7.2",
32-
"esm": "^3.0.84",
33-
"rollup": "^2.26.11",
34-
"rollup-plugin-commonjs": "^10.1.0",
35-
"rollup-plugin-node-resolve": "^5.0.0",
36-
"rollup-plugin-terser": "^7.0.2",
37-
"tap": "^14.10.2",
38-
"husky": "^3.1.0"
43+
"eslint": "8",
44+
"mocha": "9",
45+
"module-alias": "2",
46+
"rollup": "2",
47+
"rollup-plugin-terser": "7"
3948
},
40-
"files": [
41-
"dist/**/*.js",
42-
"src/**/*.js"
43-
]
49+
"publishConfig": {
50+
"access": "public"
51+
}
4452
}

parser.sublime-project

-24
This file was deleted.

rollup.config.js

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import commonjs from "rollup-plugin-commonjs";
2-
import node from "rollup-plugin-node-resolve";
31
import {terser} from "rollup-plugin-terser";
42
import * as meta from "./package.json";
53

@@ -9,8 +7,6 @@ export default [
97
{
108
input: "src/index.js",
119
plugins: [
12-
node(),
13-
commonjs(),
1410
terser({
1511
output: {preamble: copyright},
1612
mangle: {reserved: ["RequireError"]}

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export {parseCell, peekId, CellParser, TemplateCellParser, parseModule, ModuleParser} from "./parse.js";
1+
export {parseCell, CellParser, TemplateCellParser} from "./parse.js";
2+
export {peekId} from "./peek.js";
23
export {default as walk} from "./walk.js";

src/parse.js

+4-101
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ const SCOPE_FUNCTION = 2;
77
const SCOPE_ASYNC = 4;
88
const SCOPE_GENERATOR = 8;
99

10-
const STATE_START = Symbol("start");
11-
const STATE_MODIFIER = Symbol("modifier");
12-
const STATE_FUNCTION = Symbol("function");
13-
const STATE_NAME = Symbol("name");
14-
1510
export function parseCell(input, {tag, raw, globals, ...options} = {}) {
1611
let cell;
1712
// Parse empty input as JavaScript to keep ensure resulting ast
@@ -31,79 +26,9 @@ export function parseCell(input, {tag, raw, globals, ...options} = {}) {
3126
return cell;
3227
}
3328

34-
/*
35-
┌─────┐
36-
┌───────────│START│─function|class
37-
│ └─────┘ │
38-
viewof|mutable|async │ ▼
39-
│ │ ┌────────┐ ┌─┐
40-
▼ │ │FUNCTION│◀───▶│*│
41-
┌────────┐ │ └────────┘ └─┘
42-
│MODIFIER│ │ │
43-
└────────┘ name name
44-
│ │ │
45-
└──name─┐ │ ▼
46-
▼ │ ┌─────────────┐
47-
┌────────┐ │ │FUNCTION_NAME│
48-
│ NAME │◀─┘ └─────────────┘
49-
└────────┘
50-
51-
=
52-
53-
┌────────┐
54-
│ EQ │
55-
└────────┘
56-
*/
57-
58-
export function peekId(input) {
59-
let state = STATE_START;
60-
let name;
61-
try {
62-
for (const token of Parser.tokenizer(input, {ecmaVersion: 11})) {
63-
switch (state) {
64-
case STATE_START:
65-
case STATE_MODIFIER: {
66-
if (token.type === tt.name) {
67-
if (
68-
state === STATE_START &&
69-
(token.value === "viewof" ||
70-
token.value === "mutable" ||
71-
token.value === "async")
72-
) {
73-
state = STATE_MODIFIER;
74-
continue;
75-
}
76-
state = STATE_NAME;
77-
name = token;
78-
continue;
79-
}
80-
if (token.type === tt._function || token.type === tt._class) {
81-
state = STATE_FUNCTION;
82-
continue;
83-
}
84-
break;
85-
}
86-
case STATE_NAME: {
87-
if (token.type === tt.eq) return name.value;
88-
break;
89-
}
90-
case STATE_FUNCTION: {
91-
if (token.type === tt.star) continue;
92-
if (token.type === tt.name && token.end < input.length)
93-
return token.value;
94-
break;
95-
}
96-
}
97-
return;
98-
}
99-
} catch (ignore) {
100-
return;
101-
}
102-
}
103-
10429
export class CellParser extends Parser {
10530
constructor(options, ...args) {
106-
super(Object.assign({ecmaVersion: 12}, options), ...args);
31+
super(Object.assign({ecmaVersion: 13}, options), ...args);
10732
}
10833
enterScope(flags) {
10934
if (flags & SCOPE_FUNCTION) ++this.O_function;
@@ -161,7 +86,7 @@ export class CellParser extends Parser {
16186
} else {
16287
node.local = node.imported;
16388
}
164-
this.checkLVal(node.local, "let");
89+
this.checkLValSimple(node.local, "let");
16590
if (identifiers.has(node.local.name)) {
16691
this.raise(node.local.start, `Identifier '${node.local.name}' has already been declared`);
16792
}
@@ -271,8 +196,8 @@ export class CellParser extends Parser {
271196
}
272197
return super.checkUnreserved(node);
273198
}
274-
checkLVal(expr, bindingType, checkClashes) {
275-
return super.checkLVal(
199+
checkLValSimple(expr, bindingType, checkClashes) {
200+
return super.checkLValSimple(
276201
expr.type === "MutableExpression" ? expr.id : expr,
277202
bindingType,
278203
checkClashes
@@ -367,28 +292,6 @@ function readTemplateToken() {
367292
return this.finishToken(tt.invalidTemplate, this.input.slice(this.start, this.pos));
368293
}
369294

370-
export function parseModule(input, {globals} = {}) {
371-
const program = ModuleParser.parse(input);
372-
for (const cell of program.cells) {
373-
parseReferences(cell, input, globals);
374-
parseFeatures(cell, input, globals);
375-
}
376-
return program;
377-
}
378-
379-
export class ModuleParser extends CellParser {
380-
parseTopLevel(node) {
381-
if (!node.cells) node.cells = [];
382-
while (this.type !== tt.eof) {
383-
const cell = this.parseCell(this.startNode());
384-
cell.input = this.input;
385-
node.cells.push(cell);
386-
}
387-
this.next();
388-
return this.finishNode(node, "Program");
389-
}
390-
}
391-
392295
export class CellTagParser extends Parser {
393296
constructor(options, ...args) {
394297
super(Object.assign({ecmaVersion: 12}, options), ...args);

0 commit comments

Comments
 (0)