Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ The `TomlDocument` class provides a stateful interface for working with TOML doc
new TomlDocument(tomlSource: string | Uint8Array, options?: ParseOptions)
```

Initializes the TomlDocument with TOML source, parsing it into an internal representation (AST). When bytes are provided they are decoded as UTF-8 in fatal mode, rejecting invalid sequences before parsing.
Initializes the TomlDocument with TOML source, parsing it into an internal representation (CST). When bytes are provided they are decoded as UTF-8 in fatal mode, rejecting invalid sequences before parsing.

**Parameters:**
- `tomlSource: string | Uint8Array` - The TOML source to parse
Expand Down Expand Up @@ -289,22 +289,22 @@ overwrite(tomlString: string): void
```

**patch(updatedObject, format?)**
- Applies a patch to the current AST using a modified JS object
- Updates the internal AST while preserving formatting and comments
- Applies a patch to the current CST using a modified JS object
- Updates the internal CST while preserving formatting and comments
- Use `toTomlString` getter to retrieve the updated TOML string
- **Parameters:**
- `updatedObject: any` - The modified JS object to patch with
- `format?: Format` - Optional formatting options

**update(tomlString)**
- Updates the internal AST by supplying a modified TOML string
- Updates the internal CST by supplying a modified TOML string
- Uses incremental parsing for efficiency (only re-parses changed portions)
- Use `toJsObject` getter to retrieve the updated JS object representation
- **Parameters:**
- `tomlString: string` - The modified TOML string to update with

**overwrite(tomlString)**
- Overwrites the internal AST by fully re-parsing the supplied TOML string
- Overwrites the internal CST by fully re-parsing the supplied TOML string
- Simpler but slower than `update()` which uses incremental parsing
- **Parameters:**
- `tomlString: string` - The TOML string to overwrite with
Expand Down Expand Up @@ -425,7 +425,7 @@ const toml = stringify({
**newLine**
- **Type:** `string`
- **Default:** `'\n'`
- **Description:** The line ending character(s) to use in the output TOML. This option affects only the stringification process, not the internal representation (AST).
- **Description:** The line ending character(s) to use in the output TOML. This option affects only the stringification process, not the internal representation (CST).

```js
const format = TomlFormat.default();
Expand All @@ -436,7 +436,7 @@ format.newLine = '\r\n'; // Windows line endings
**trailingNewline**
- **Type:** `number`
- **Default:** `1`
- **Description:** The number of trailing newlines to add at the end of the TOML document. This option affects only the stringification process, not the internal representation (AST).
- **Description:** The number of trailing newlines to add at the end of the TOML document. This option affects only the stringification process, not the internal representation (CST).

```js
const format = TomlFormat.default();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`AST parsing comparison JS object to AST should parse nested objects with inline table formatting by default: js-object-ast 1`] = `
exports[`CST parsing comparison JS object to CST should parse nested objects with inline table formatting by default: js-object-cst 1`] = `
Object {
"items": Array [
Object {
Expand Down Expand Up @@ -215,14 +215,14 @@ Object {
}
`;

exports[`AST parsing comparison JS object to AST should parse nested objects with inline table formatting by default: js-object-to-toml 1`] = `
exports[`CST parsing comparison JS object to CST should parse nested objects with inline table formatting by default: js-object-to-toml 1`] = `
"[top]
name = \\"example\\"
nested = { key = \\"value\\" }
"
`;

exports[`AST parsing comparison TOML file to AST should parse nested tables preserving separate section formatting: toml-file-ast 1`] = `
exports[`CST parsing comparison TOML file to CST should parse nested tables preserving separate section formatting: toml-file-cst 1`] = `
Object {
"items": Array [
Object {
Expand Down Expand Up @@ -423,7 +423,7 @@ Object {
}
`;

exports[`AST parsing comparison TOML file to AST should parse nested tables preserving separate section formatting: toml-file-to-toml 1`] = `
exports[`CST parsing comparison TOML file to CST should parse nested tables preserving separate section formatting: toml-file-to-toml 1`] = `
"[top]
name = \\"example\\"

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/continue-parsing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import toJS from '../to-js';
import dedent from 'dedent';

describe('continueParsingTOML', () => {
it('appends parsed items from remaining string to existing AST', () => {
it('appends parsed items from remaining string to existing CST', () => {
const initialToml = dedent`
[section1]
key1 = "value1"
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('continueParsingTOML', () => {
});
});

it('handles empty initial AST', () => {
it('handles empty initial CST', () => {
const initialToml = '';
const remainingToml = dedent`
[section]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Tests for comparing AST structures when parsing from JS objects vs TOML files.
* This test explores the different ways the AST is built in both scenarios.
* Tests for comparing CST structures when parsing from JS objects vs TOML files.
* This test explores the different ways the CST is built in both scenarios.
*/

import parseJS from '../parse-js';
Expand All @@ -10,10 +10,10 @@ import { TomlFormat } from '../toml-format';
import { readFileSync } from 'fs';
import path from 'path';

describe('AST parsing comparison', () => {
describe('CST parsing comparison', () => {
const fmt = new TomlFormat();

describe('JS object to AST', () => {
describe('JS object to CST', () => {
it('should parse nested objects with inline table formatting by default', () => {
const jsObject = {
top: {
Expand All @@ -24,12 +24,12 @@ describe('AST parsing comparison', () => {
}
};

const ast = parseJS(jsObject);
const str = toTOML(ast.items, fmt);
const cst = parseJS(jsObject);
const str = toTOML(cst.items, fmt);

// The default formatting for nested tables should be inline
expect(str).toMatchSnapshot('js-object-to-toml');
expect(ast).toMatchSnapshot('js-object-ast');
expect(cst).toMatchSnapshot('js-object-cst');

// Verify that nested tables are formatted as inline by default
expect(str).toContain('nested = { key = "value" }');
Expand All @@ -38,16 +38,16 @@ describe('AST parsing comparison', () => {
});
});

describe('TOML file to AST', () => {
describe('TOML file to CST', () => {
it('should parse nested tables preserving separate section formatting', () => {
const tomlInput = readFileSync(
path.join(__dirname, '../__fixtures__/nested-tables.toml'),
'utf-8'
);

const astGenerator = parseTOML(tomlInput);
const blocks = Array.from(astGenerator);
const astFromToml = {
const cstGenerator = parseTOML(tomlInput);
const blocks = Array.from(cstGenerator);
const cstFromToml = {
type: 'Document',
items: blocks,
loc: blocks.length > 0 ? {
Expand All @@ -60,7 +60,7 @@ describe('AST parsing comparison', () => {

// Should preserve the separate section formatting when parsed from TOML
expect(strFromToml).toMatchSnapshot('toml-file-to-toml');
expect(astFromToml).toMatchSnapshot('toml-file-ast');
expect(cstFromToml).toMatchSnapshot('toml-file-cst');

// Verify that nested tables maintain separate sections
expect(strFromToml).toContain('[top]');
Expand All @@ -85,8 +85,8 @@ describe('AST parsing comparison', () => {
nested: { key: "value" }
}
};
const jsAst = parseJS(jsObject);
const jsToml = toTOML(jsAst.items, fmt);
const jsCst = parseJS(jsObject);
const jsToml = toTOML(jsCst.items, fmt);

// TOML file parsing preserves separate sections
const tomlInput = readFileSync(
Expand Down
54 changes: 27 additions & 27 deletions src/__tests__/detect-trailing-comma.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ import { TomlFormat } from '../toml-format';
import parseTOML from '../parse-toml';

function autoDetectFormat(toml: string) {
return TomlFormat.autoDetectFormatWithAst(toml, parseTOML(toml));
return TomlFormat.autoDetectFormatWithCst(toml, parseTOML(toml));
}

describe('detectTrailingComma', () => {
test('should detect trailing comma in inline array', () => {
const toml = `array = ["a", "b", "c",]`;
const ast = parseTOML(toml);
expect(detectTrailingComma(ast)).toBe(true);
const cst = parseTOML(toml);
expect(detectTrailingComma(cst)).toBe(true);
});

test('should detect no trailing comma in inline array', () => {
const toml = `array = ["a", "b", "c"]`;
const ast = parseTOML(toml);
expect(detectTrailingComma(ast)).toBe(false);
const cst = parseTOML(toml);
expect(detectTrailingComma(cst)).toBe(false);
});

test('should detect trailing comma in inline table', () => {
const toml = `table = { a = "1", b = "2", }`;
const ast = parseTOML(toml);
expect(detectTrailingComma(ast)).toBe(true);
const cst = parseTOML(toml);
expect(detectTrailingComma(cst)).toBe(true);
});

test('should detect no trailing comma in inline table', () => {
const toml = `table = { a = "1", b = "2" }`;
const ast = parseTOML(toml);
expect(detectTrailingComma(ast)).toBe(false);
const cst = parseTOML(toml);
expect(detectTrailingComma(cst)).toBe(false);
});

test('should return false for TOML without comma-separated structures', () => {
Expand All @@ -37,8 +37,8 @@ describe('detectTrailingComma', () => {
[section]
key = "value"
`;
const ast = parseTOML(toml);
expect(detectTrailingComma(ast)).toBe(false);
const cst = parseTOML(toml);
expect(detectTrailingComma(cst)).toBe(false);
});

test('should find trailing comma in nested structures', () => {
Expand All @@ -47,8 +47,8 @@ describe('detectTrailingComma', () => {
[section]
array = ["a", "b",]
`;
const ast = parseTOML(toml);
expect(detectTrailingComma(ast)).toBe(true);
const cst = parseTOML(toml);
expect(detectTrailingComma(cst)).toBe(true);
});
});

Expand Down Expand Up @@ -119,26 +119,26 @@ describe('TomlFormat.autoDetectFormat', () => {
describe('detectBracketSpacing', () => {
test('should detect bracket spacing in inline array', () => {
const toml = `array = [ "a", "b", "c" ]`;
const ast = parseTOML(toml);
expect(detectBracketSpacing(toml, ast)).toBe(true);
const cst = parseTOML(toml);
expect(detectBracketSpacing(toml, cst)).toBe(true);
});

test('should detect no bracket spacing in inline array', () => {
const toml = `array = ["a", "b", "c"]`;
const ast = parseTOML(toml);
expect(detectBracketSpacing(toml, ast)).toBe(false);
const cst = parseTOML(toml);
expect(detectBracketSpacing(toml, cst)).toBe(false);
});

test('should detect bracket spacing in inline table', () => {
const toml = `table = { a = "1", b = "2" }`;
const ast = parseTOML(toml);
expect(detectBracketSpacing(toml, ast)).toBe(true);
const cst = parseTOML(toml);
expect(detectBracketSpacing(toml, cst)).toBe(true);
});

test('should detect no bracket spacing in inline table', () => {
const toml = `table = {a = "1", b = "2"}`;
const ast = parseTOML(toml);
expect(detectBracketSpacing(toml, ast)).toBe(false);
const cst = parseTOML(toml);
expect(detectBracketSpacing(toml, cst)).toBe(false);
});

test('should return true for TOML without bracket structures', () => {
Expand All @@ -147,8 +147,8 @@ describe('detectBracketSpacing', () => {
[section]
key = "value"
`;
const ast = parseTOML(toml);
expect(detectBracketSpacing(toml, ast)).toBe(true); // Default to true
const cst = parseTOML(toml);
expect(detectBracketSpacing(toml, cst)).toBe(true); // Default to true
});

test('should find bracket spacing in nested structures', () => {
Expand All @@ -157,16 +157,16 @@ describe('detectBracketSpacing', () => {
[section]
array = [ "a", "b" ]
`;
const ast = parseTOML(toml);
expect(detectBracketSpacing(toml, ast)).toBe(true);
const cst = parseTOML(toml);
expect(detectBracketSpacing(toml, cst)).toBe(true);
});

test('should handle mixed bracket spacing correctly', () => {
const toml = `array1 = [ "a", "b" ]
array2 = ["c", "d"]`;
const ast = parseTOML(toml);
const cst = parseTOML(toml);
// Should return true based on the first occurrence found
expect(detectBracketSpacing(toml, ast)).toBe(true);
expect(detectBracketSpacing(toml, cst)).toBe(true);
});
});

Expand Down
14 changes: 7 additions & 7 deletions src/__tests__/find-by-path.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import findByPath from '../find-by-path';
import parseTOML from '../parse-toml';
import { example } from '../__fixtures__';
import { Document, NodeType } from '../ast';
import { Document, NodeType } from '../cst';
import dedent from 'dedent';

it('should find node by path', () => {
const ast = parseTOML(example);
const cst = parseTOML(example);
const document: Document = {
type: NodeType.Document,
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
items: [...ast]
items: [...cst]
};

expect(findByPath(document, []).type).toEqual('Document');
Expand All @@ -29,11 +29,11 @@ it('should find nodes within nested inline tables', () => {
}
`;

const ast = parseTOML(toml);
const cst = parseTOML(toml);
const document: Document = {
type: NodeType.Document,
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
items: [...ast]
items: [...cst]
};

// Should find simple inline table properties
Expand Down Expand Up @@ -84,11 +84,11 @@ it('should keep scanning AOT-scoped siblings after a shorter prefix match fails'
name = "banana"
`;

const ast = parseTOML(toml);
const cst = parseTOML(toml);
const document: Document = {
type: NodeType.Document,
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
items: [...ast]
items: [...cst]
};

const width = findByPath(document, ['fruit', 0, 'physical', 'dimensions', 'width']) as any;
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/generate.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generateString } from '../generate';
import { NodeType } from '../ast';
import { NodeType } from '../cst';

describe('generateString', () => {
describe('with multiline basic string format preservation', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/parse-js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import parseJS from '../parse-js';
import parseTOML from '../parse-toml';
import toTOML from '../to-toml';
import dedent from 'dedent';
import { NodeType, AST, Document } from '../ast';
import { NodeType, CST, Document } from '../cst';
import { TomlFormat } from '../toml-format';

function toDocument(ast: AST): Document {
const items = [...ast];
function toDocument(cst: CST): Document {
const items = [...cst];
return {
type: NodeType.Document,
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/parse-toml.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import parseTOML from '../parse-toml';
import { Table, KeyValue, InlineArray, DateTime } from '../ast';
import { Table, KeyValue, InlineArray, DateTime } from '../cst';
import { example, fruit, hard_example, hard_example_unicode, kitchen_sink } from '../__fixtures__';
import dedent from 'dedent';

Expand Down
Loading
Loading