diff --git a/package.json b/package.json index 8314290..2d8a2ff 100644 --- a/package.json +++ b/package.json @@ -2,13 +2,14 @@ "name": "kdljs", "version": "0.2.0", "description": "KDL parser and serializer.", + "type": "module", "main": "src/index.js", "types": "index.d.ts", "scripts": { "lint": "standard", "test": "mocha --throw-deprecation test/spec.js", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md", - "docs": "jsdoc -c .jsdoc.js", + "docs": "jsdoc -c .jsdoc.cjs", "preversion": "npm run lint && npm test", "version": "npm run changelog" }, diff --git a/src/formatter.js b/src/formatter.js index f5b4f61..5a029d1 100644 --- a/src/formatter.js +++ b/src/formatter.js @@ -3,8 +3,9 @@ * @memberof module:kdljs */ -const { validateDocument } = require('./validator.js') -const { Identifier } = require('./parser/tokens.js') +import { validateDocument } from './validator.js' +import { Identifier } from './parser/tokens.js' +import { bannedIdentifiers } from './parser/base.js' /* eslint-disable no-control-regex */ const linespace = /^[\r\n\u0085\u000C\u2028\u2029]$/ @@ -22,14 +23,6 @@ const commonEscapes = { } const identifierPattern = new RegExp('^(' + Identifier.PATTERN.source + ')$') -const bannedIdentifiers = new Set([ - 'true', - 'false', - 'null', - 'inf', - '-inf', - 'nan' -]) /** * @access private @@ -293,7 +286,7 @@ function processOptions (options) { * @param {module:kdljs.formatter.Options} [options={}] - Formatting options * @return {string} formatted KDL file */ -module.exports.format = function format (doc, options) { +export function format (doc, options) { if (!validateDocument(doc)) { throw new TypeError('Invalid KDL document') } diff --git a/src/index.js b/src/index.js index 7afda1c..b176876 100644 --- a/src/index.js +++ b/src/index.js @@ -6,10 +6,10 @@ * @borrows module:kdljs.validator.validateDocument as validateDocument */ -const { parse } = require('./parser/index.js') -const { format } = require('./formatter.js') -const { query } = require('./queryEngine.js') -const { validateDocument } = require('./validator.js') +import { parse } from './parser/index.js' +import { format } from './formatter.js' +import { query } from './queryEngine.js' +import { validateDocument } from './validator.js' /** * A {@link https://github.com/kdl-org/kdl/blob/main/SPEC.md#document|Document}. @@ -53,7 +53,9 @@ const { validateDocument } = require('./validator.js') * @type {string} */ -module.exports.parse = parse -module.exports.format = format -module.exports.query = query -module.exports.validateDocument = validateDocument +export { + parse, + format, + query, + validateDocument +} diff --git a/src/parser/base.js b/src/parser/base.js index ead22b1..c56aff7 100644 --- a/src/parser/base.js +++ b/src/parser/base.js @@ -3,8 +3,8 @@ * @memberof module:kdljs.parser */ -const { EmbeddedActionsParser, createTokenInstance, MismatchedTokenException } = require('chevrotain') -const Tokens = require('./tokens.js') +import { EmbeddedActionsParser, createTokenInstance, MismatchedTokenException } from 'chevrotain' +import * as Tokens from './tokens.js' /** * @type Object @@ -471,6 +471,9 @@ class BaseParser extends EmbeddedActionsParser { } } -module.exports.escapes = escapes -module.exports.radix = radix -module.exports.BaseParser = BaseParser +export { + escapes, + radix, + bannedIdentifiers, + BaseParser +} diff --git a/src/parser/index.js b/src/parser/index.js index c475e28..4ac0af4 100644 --- a/src/parser/index.js +++ b/src/parser/index.js @@ -4,7 +4,7 @@ * @borrows module:kdljs.parser.kdl.parse as parse */ -const kdl = require('./kdl') +import * as kdl from './kdl.js' -module.exports.parse = kdl.parse -module.exports.kdl = kdl +export const parse = kdl.parse +export { kdl } diff --git a/src/parser/kdl.js b/src/parser/kdl.js index 26db889..7258a35 100644 --- a/src/parser/kdl.js +++ b/src/parser/kdl.js @@ -3,9 +3,9 @@ * @memberof module:kdljs.parser */ -const { Lexer, MismatchedTokenException, createTokenInstance } = require('chevrotain') -const { BaseParser } = require('./base.js') -const Tokens = require('./tokens.js') +import { Lexer, MismatchedTokenException, createTokenInstance } from 'chevrotain' +import { BaseParser } from './base.js' +import * as Tokens from './tokens.js' const tokens = { defaultMode: 'main', @@ -341,7 +341,7 @@ const parser = new KdlParser() * @param {string} text - Input KDL file (or fragment) * @return {module:kdljs.parser.kdl.ParseResult} Output */ -module.exports.parse = function parse (text) { +export function parse (text) { const { tokens, errors } = lexer.tokenize(text) if (errors.length) { @@ -360,6 +360,8 @@ module.exports.parse = function parse (text) { } } -module.exports.lexer = lexer -module.exports.parser = parser -module.exports.KdlParser = KdlParser +export { + lexer, + parser, + KdlParser +} diff --git a/src/parser/kql.js b/src/parser/kql.js index 936d588..7c35681 100644 --- a/src/parser/kql.js +++ b/src/parser/kql.js @@ -3,9 +3,9 @@ * @memberof module:kdljs.parser */ -const { Lexer } = require('chevrotain') -const { BaseParser } = require('./base.js') -const Tokens = require('./tokens.js') +import { Lexer } from 'chevrotain' +import { BaseParser } from './base.js' +import * as Tokens from './tokens.js' const tokens = { defaultMode: 'main', @@ -361,7 +361,7 @@ const parser = new KqlParser() * @param {string} text - Input KQL file (or fragment) * @return {module:kdljs.parser.kql.ParseResult} Output */ -module.exports.parse = function parse (text) { +export function parse (text) { parser.input = lexer.tokenize(text).tokens const output = parser.query() @@ -371,6 +371,8 @@ module.exports.parse = function parse (text) { } } -module.exports.lexer = lexer -module.exports.parser = parser -module.exports.KqlParser = KqlParser +export { + lexer, + parser, + KqlParser +} diff --git a/src/parser/tokens.js b/src/parser/tokens.js index 201563e..ac75974 100644 --- a/src/parser/tokens.js +++ b/src/parser/tokens.js @@ -1,4 +1,4 @@ -const { createToken, EOF } = require('chevrotain') +import { createToken, EOF } from 'chevrotain' // Whitespace and comments const WhiteSpace = createToken({ @@ -121,7 +121,7 @@ const PropAccessor = createToken({ name: 'PropAccessor', pattern: /prop\(/ }) const Accessor = createToken({ name: 'Accessor', pattern: /(name|tag|values|props)\(/ }) const Comma = createToken({ name: 'Comma', pattern: /,/ }) -module.exports = { +export { WhiteSpace, BOM, NewLine, diff --git a/src/queryEngine.js b/src/queryEngine.js index 9d90543..d71b262 100644 --- a/src/queryEngine.js +++ b/src/queryEngine.js @@ -3,8 +3,8 @@ * @memberof module:kdljs */ -const { parse } = require('./parser/kql.js') -const { validateDocument } = require('./validator.js') +import { parse } from './parser/kql.js' +import { validateDocument } from './validator.js' /** * @typedef Query @@ -285,7 +285,7 @@ function applyQuery (query, doc) { * @param {module:kdljs~QueryString} queryString - Query for selecting and/or transforming results * @return {any} */ -function query (doc, queryString) { +export function query (doc, queryString) { if (!validateDocument(doc)) { throw new TypeError('Invalid KDL document') } @@ -297,5 +297,3 @@ function query (doc, queryString) { return applyQuery(output, doc) } - -module.exports.query = query diff --git a/src/validator.js b/src/validator.js index 80d50ad..4624e13 100644 --- a/src/validator.js +++ b/src/validator.js @@ -8,7 +8,7 @@ * @param {module:kdljs~Document} doc - KDL document * @return {boolean} */ -function validateDocument (doc) { +export function validateDocument (doc) { return Array.isArray(doc) && doc.every(node => validateNode(node)) } @@ -85,5 +85,3 @@ function validateValue (value) { const type = typeof value return type === 'string' || type === 'number' || type === 'boolean' || value === null } - -module.exports.validateDocument = validateDocument diff --git a/test/spec.js b/test/spec.js index d3d82df..17176bf 100644 --- a/test/spec.js +++ b/test/spec.js @@ -1,10 +1,10 @@ /* eslint-env mocha */ -const assert = require('assert') -const path = require('path') -const fs = require('fs') -const suite = require('./suite.json') -const { parse, format } = require('../') +import assert from 'node:assert' +import path from 'node:path' +import url from 'node:url' +import fs from 'node:fs' +import { parse, format } from '../src/index.js' function prepareExpectations (nodes) { return nodes.map(node => ({ @@ -20,6 +20,8 @@ function prepareExpectations (nodes) { })) } +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)) +const suite = JSON.parse(fs.readFileSync(path.join(__dirname, 'suite.json'), 'utf8')) const customTests = path.join(__dirname, './kdl') const customTestFile = fs.readdirSync(customTests)