toml-eslint-parser

A TOML parser that produces output compatible with ESLint

Usage no npm install needed!

<script type="module">
  import tomlEslintParser from 'https://cdn.skypack.dev/toml-eslint-parser';
</script>

README

toml-eslint-parser

A TOML parser that produces output compatible with ESLint.

NPM license NPM version NPM downloads NPM downloads NPM downloads NPM downloads NPM downloads Build Status

Features

Installation

npm install --save-dev toml-eslint-parser

Usage

Configuration

Use .eslintrc.* file to configure parser. See also: https://eslint.org/docs/user-guide/configuring.

Example .eslintrc.js:

module.exports = {
    "overrides": [
        {
            "files": ["*.toml"],
            "parser": "toml-eslint-parser"
        }
    ]
}

Usage for Custom Rules / Plugins

Usage for Directly

Example:

import type { AST } from "toml-eslint-parser"
import { parseTOML, getStaticTOMLValue } from "toml-eslint-parser"

const code = `
# This is a TOML document

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00

[database]
enabled = true
ports = [ 8001, 8001, 8002 ]
data = [ ["delta", "phi"], [3.14] ]
temp_targets = { cpu = 79.5, case = 72.0 }

[servers]

[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"
`

const ast: AST.TOMLProgram = parseTOML(code)
console.log(ast)
// {
//   type: 'Program',
//   body: [
//     {
//       type: 'TOMLTopLevelTable',
//       body: [
//         {
//           type: 'TOMLKeyValue',
//           key: { /* ... */ },
//           value: { /* ... */ },
//           /* ... */
//         },
//         {
//           type: 'TOMLTable',
//           kind: 'standard',
//           key: { /* ... */ },
//           body: [ /* ... */ ],
//           /* ... */
//         },
//         /* ... */
//       ]
//       /* ... */
//     }
//   ],
//   tokens: [ /* ... */ ],
//   comments: [
//     {
//       type: 'Block',
//       value: ' This is a TOML document',
//       // ...
//     }
//   ],
// }

const value = getStaticTOMLValue(ast)
console.log(value)
// {
//   title: 'TOML Example',
//   owner: { name: 'Tom Preston-Werner', dob: 1979-05-27T15:32:00.000Z },
//   database: {
//     enabled: true,
//     ports: [ 8001, 8001, 8002 ],
//     data: [ [ /* ... */ ], [ /* ... */ ] ],
//     temp_targets: { cpu: 79.5, case: 72 }
//   },
//   servers: {
//     alpha: { ip: '10.0.0.1', role: 'frontend' },
//     beta: { ip: '10.0.0.2', role: 'backend' }
//   }
// }

Testing

This project uses files from BurntSushi/toml-test and iarna/toml-spec-tests repositories for testing.

Related Packages