Skip to content

Commit

Permalink
refactor: change to ESM
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This library is now ESM-only.
  • Loading branch information
larsgw committed Jan 5, 2025
1 parent b8095f3 commit 0b1d787
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 57 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
15 changes: 4 additions & 11 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]$/
Expand All @@ -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
Expand Down Expand Up @@ -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')
}
Expand Down
18 changes: 10 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -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
}
13 changes: 8 additions & 5 deletions src/parser/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions src/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
16 changes: 9 additions & 7 deletions src/parser/kdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
16 changes: 9 additions & 7 deletions src/parser/kql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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()

Expand All @@ -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
}
4 changes: 2 additions & 2 deletions src/parser/tokens.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createToken, EOF } = require('chevrotain')
import { createToken, EOF } from 'chevrotain'

// Whitespace and comments
const WhiteSpace = createToken({
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 3 additions & 5 deletions src/queryEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
}
Expand All @@ -297,5 +297,3 @@ function query (doc, queryString) {

return applyQuery(output, doc)
}

module.exports.query = query
4 changes: 1 addition & 3 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -85,5 +85,3 @@ function validateValue (value) {
const type = typeof value
return type === 'string' || type === 'number' || type === 'boolean' || value === null
}

module.exports.validateDocument = validateDocument
12 changes: 7 additions & 5 deletions test/spec.js
Original file line number Diff line number Diff line change
@@ -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 => ({
Expand All @@ -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)

Expand Down

0 comments on commit 0b1d787

Please sign in to comment.