Skip to content

Commit 34c25bd

Browse files
committed
chore: Upgrade last package (ecmascript) to ES2020 and ES modules.
1 parent 5af0468 commit 34c25bd

15 files changed

Lines changed: 42 additions & 77 deletions

File tree

.eslintrc.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
parserOptions: {
3-
ecmaVersion: 6,
4-
sourceType: 'script'
3+
ecmaVersion: 11, // es2020
4+
sourceType: 'module'
55
},
66

77
// To minimize dependencies on Node- or browser-specific features, leave the

examples/csv/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* eslint-env node */
22

3-
'use strict';
4-
53
const assert = require('assert');
64
const fs = require('fs');
75
const {join} = require('path');

examples/ecmascript/compile.js

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
/* eslint-env node */
22

3-
'use strict';
3+
import assert from 'node:assert/strict';
4+
import fs from 'node:fs';
45

5-
// --------------------------------------------------------------------
6-
// Imports
7-
// --------------------------------------------------------------------
8-
9-
const fs = require('fs');
10-
const path = require('path');
11-
12-
const es5 = require('./src/es5');
13-
const ohm = require('ohm-js');
6+
import * as es5 from './src/es5.js';
7+
import initES6 from './src/es6.js';
8+
import * as ohm from 'ohm-js';
149

1510
// --------------------------------------------------------------------
1611
// Helpers
@@ -20,19 +15,6 @@ function removeShebang(source) {
2015
return source.slice(0, 2) === '#!' ? source.replace('#!', '//') : source;
2116
}
2217

23-
function loadModule(name) {
24-
const ns = {ES5: es5.grammar};
25-
if (fs.existsSync(name)) {
26-
return require(name)(ohm, ns, es5.semantics);
27-
}
28-
const relPath = path.join(__dirname, 'src', name);
29-
const modulePath = require.resolve(relPath);
30-
if (modulePath) {
31-
return require(modulePath)(ohm, ns, es5.semantics);
32-
}
33-
throw new Error("Error: Cannot find grammar module '" + name + "'");
34-
}
35-
3618
// --------------------------------------------------------------------
3719
// Main
3820
// --------------------------------------------------------------------
@@ -60,7 +42,8 @@ function compile(args) {
6042
}
6143
}
6244

63-
const lang = opts.grammar ? loadModule(opts.grammar) : es5;
45+
assert(!opts.grammar || ['es5', 'es6'].includes(opts.grammar));
46+
const lang = opts.grammar === 'es6' ? initES6(ohm, {ES5: es5.grammar}, es5.semantics) : es5;
6447

6548
const files = filenames.map(name => {
6649
// If benchmarking, read in all the files at once, so that we can just time the matching.
@@ -104,8 +87,9 @@ function compile(args) {
10487
return null;
10588
}
10689

107-
module.exports = compile;
90+
export {compile};
10891

109-
if (require.main === module) {
92+
// `importa.meta.main` was added in Node v24.2.0.
93+
if (import.meta.main) {
11094
console.log(compile(process.argv.slice(2)));
11195
}

examples/ecmascript/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
'use strict';
2-
3-
module.exports = require('./src/es5');
1+
export * from './src/es5.js';

examples/ecmascript/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "ohm-grammar-ecmascript",
33
"version": "2.0.0",
44
"description": "Ohm grammars for various editions of ECMAScript (aka JavaScript).",
5+
"type": "module",
56
"scripts": {
67
"test": "ava"
78
},

examples/ecmascript/src/es5.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
/* eslint-env node */
22

3-
'use strict';
3+
import {readFileSync} from 'node:fs';
44

5-
// --------------------------------------------------------------------
6-
// Imports
7-
// --------------------------------------------------------------------
8-
9-
const fs = require('fs');
10-
const path = require('path');
11-
12-
const ohm = require('ohm-js');
5+
import * as ohm from 'ohm-js';
136

147
// --------------------------------------------------------------------
158
// Helpers
@@ -56,9 +49,9 @@ function nodeToES5(node, children) {
5649
}
5750

5851
// Instantiate the ES5 grammar.
59-
const contents = fs.readFileSync(path.join(__dirname, 'es5.ohm'));
60-
const g = ohm.grammars(contents).ES5;
61-
const semantics = g.createSemantics();
52+
const contents = readFileSync(new URL('es5.ohm', import.meta.url));
53+
const grammar = ohm.grammars(contents).ES5;
54+
const semantics = grammar.createSemantics();
6255

6356
semantics.addOperation('toES5()', {
6457
Program(_, sourceElements) {
@@ -116,7 +109,4 @@ function mergeBindings(...nodes) {
116109
return bindings;
117110
}
118111

119-
module.exports = {
120-
grammar: g,
121-
semantics,
122-
};
112+
export {grammar, semantics};
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import compile from '../compile.js';
2-
import es5 from './es5.js';
1+
/* global URL */
2+
3+
import {compile} from '../compile.js';
4+
import * as es5 from './es5.js';
35

46
import test from 'ava';
5-
import path from 'path';
7+
import {join} from 'node:path';
68

7-
const __dirname = path.dirname(new URL(import.meta.url).pathname);
8-
const testdataPath = filename => path.join(__dirname, 'testdata', filename);
9+
const testdataPath = filename => new URL(join('testdata', filename), import.meta.url);
910

1011
test('es5 - basic tests', t => {
1112
let results = es5.grammar.match('var x = 3; console.log(x)');

examples/ecmascript/src/es6.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/* eslint-env node */
22

3-
'use strict';
4-
5-
const fs = require('fs');
6-
const path = require('path');
3+
import {readFileSync} from 'node:fs';
74

85
const anyNodesMentionThis = (...nodes) => nodes.some(n => n.mentionsThis);
96

@@ -34,8 +31,8 @@ const toES5Actions = {
3431
},
3532
};
3633

37-
module.exports = function(ohm, ns, s) {
38-
const g = ohm.grammar(fs.readFileSync(path.join(__dirname, 'es6.ohm')).toString(), ns);
34+
export default function(ohm, ns, s) {
35+
const g = ohm.grammar(readFileSync(new URL('es6.ohm', import.meta.url)).toString(), ns);
3936
const semantics = g.extendSemantics(s);
4037
semantics.addAttribute('mentionsThis', mentionsThisActions);
4138
semantics.extendOperation('toES5', toES5Actions);
@@ -44,4 +41,4 @@ module.exports = function(ohm, ns, s) {
4441
grammar: g,
4542
semantics,
4643
};
47-
};
44+
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import fs from 'fs';
21
import * as ohm from 'ohm-js';
32
import test from 'ava';
43

5-
import es5 from './es5.js';
4+
import * as es5 from './es5.js';
65
import initES6 from './es6.js';
76

87
const es6 = initES6(ohm, {ES5: es5.grammar}, es5.semantics);
@@ -12,7 +11,7 @@ test('template literals', t => {
1211
const succeeds = str => {
1312
const result = es6.grammar.match(str);
1413
if (!result.succeeded()) {
15-
console.error(result.message);
14+
console.error(result.message); // eslint-disable-line no-console
1615
}
1716
return result.succeeded();
1817
};

examples/indentation-sensitive/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const outline = grammar(
1616
spaces := (~newline space)*
1717
}
1818
`,
19-
{IndentationSensitive}
19+
{IndentationSensitive},
2020
);
2121
const semantics = outline.createSemantics().addOperation('toJS', {
2222
Items(items) {
@@ -65,15 +65,15 @@ test('errors', () => {
6565
number = digit+
6666
}
6767
`,
68-
{IndentationSensitive}
68+
{IndentationSensitive},
6969
);
7070

7171
assert.is(g.match('if True:\n 3').succeeded(), true);
7272
assert.is(g.match('if True:\n if False:\n 3').succeeded(), true);
7373
assert.is(g.match('if True:\n3').shortMessage, 'Line 2, col 1: expected an indented block');
7474
assert.is(
7575
g.match('if True:\n if False:\n 3').shortMessage,
76-
'Line 3, col 2: expected an indented block'
76+
'Line 3, col 2: expected an indented block',
7777
);
7878
});
7979

0 commit comments

Comments
 (0)