From dd9605ed5579a5dc14d2ff70db962c919b8ad7e7 Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Fri, 29 Jan 2021 13:13:30 +0200 Subject: [PATCH] Drop yaml/parse-cst endpoint BREAKING CHANGE: Users will need to update their code: -import parseCST from 'yaml/parse-cst' +import YAML from 'yaml' +const parseCST = YAML.parseCST --- CONTRIBUTING.md | 2 +- README.md | 5 ----- browser/parse-cst.js | 1 - parse-cst.d.ts => cst.d.ts | 6 ------ docs/01_intro.md | 5 ----- docs/07_cst_parser.md | 14 +++++++------- index.d.ts | 9 +++++++-- jest.config.js | 1 - package.json | 2 -- parse-cst.js | 1 - rollup.browser-config.js | 1 - rollup.node-config.js | 1 - src/cst/parse.js | 2 -- tests/cst/corner-cases.js | 3 ++- tests/cst/parse.js | 3 ++- types.d.ts | 2 +- util.d.ts | 2 +- 17 files changed, 21 insertions(+), 39 deletions(-) delete mode 100644 browser/parse-cst.js rename parse-cst.d.ts => cst.d.ts (96%) delete mode 100644 parse-cst.js diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 10c68c24..9f899fa2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -38,7 +38,7 @@ npm test # just to be sure - **`tests/cst/`** - Tests for the CST parser - **`tests/doc/`** - Tests for the AST level of the library - **`tests/yaml-test-suite/`** - Git submodule of a custom fork of the [YAML Test Suite](https://github.com/yaml/yaml-test-suite) -- **`{index,parse-cst,types,util}.js`** - The library's published API; see the documentation site for more details. Not transpiled, so written as backwards-compatible CommonJS. +- **`{index,types,util}.js`** - The library's published API; see the documentation site for more details. Not transpiled, so written as backwards-compatible CommonJS. ## Contributing Code diff --git a/README.md b/README.md index 8f7b3dce..b3a2dedd 100644 --- a/README.md +++ b/README.md @@ -54,11 +54,6 @@ import { Pair, YAMLMap, YAMLSeq } from 'yaml/types' ### CST Parser -```js -import parseCST from 'yaml/parse-cst' -``` - -- [`parseCST(str): CSTDocument[]`](https://eemeli.org/yaml/#parsecst) - [`YAML.parseCST(str): CSTDocument[]`](https://eemeli.org/yaml/#parsecst) ## YAML.parse diff --git a/browser/parse-cst.js b/browser/parse-cst.js deleted file mode 100644 index 80657565..00000000 --- a/browser/parse-cst.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./dist/parse-cst').parse diff --git a/parse-cst.d.ts b/cst.d.ts similarity index 96% rename from parse-cst.d.ts rename to cst.d.ts index bd8a5b4d..0f252f59 100644 --- a/parse-cst.d.ts +++ b/cst.d.ts @@ -1,11 +1,5 @@ import { Type, YAMLSyntaxError } from './util' -export default function parseCST(str: string): ParsedCST - -export interface ParsedCST extends Array { - setOrigRanges(): boolean -} - export namespace CST { interface Range { start: number diff --git a/docs/01_intro.md b/docs/01_intro.md index d6f1cf1e..98879c34 100644 --- a/docs/01_intro.md +++ b/docs/01_intro.md @@ -59,9 +59,4 @@ import { Pair, YAMLMap, YAMLSeq } from 'yaml/types'

CST Parser

-```js -import parseCST from 'yaml/parse-cst' -``` - -- [`parseCST(str): CSTDocument[]`](#parsecst) - [`YAML.parseCST(str): CSTDocument[]`](#parsecst) diff --git a/docs/07_cst_parser.md b/docs/07_cst_parser.md index 173e8e70..5e1ab81c 100644 --- a/docs/07_cst_parser.md +++ b/docs/07_cst_parser.md @@ -6,9 +6,9 @@ For ease of implementation and to provide better error handling and reporting, t ```js -import parseCST from 'yaml/parse-cst' +import YAML from 'yaml' -const cst = parseCST(` +const cst = YAML.parseCST(` sequence: [ one, two, ] mapping: { sky: blue, sea: green } --- @@ -32,13 +32,13 @@ cst[1] // second document, containing a sequence .strValue // 'Block scalar\n' ``` -#### `parseCST(string): CSTDocument[]` - #### `YAML.parseCST(string): CSTDocument[]` The CST parser will not produce a CST that is necessarily valid YAML, and in particular its representation of collections of items is expected to undergo further processing and validation. The parser should never throw errors, but may include them as a value of the relevant node. On the other hand, if you feed it garbage, you'll likely get a garbage CST as well. -The public API of the CST layer is a single function which returns an array of parsed CST documents. The array and its contained nodes override the default `toString` method, each returning a YAML string representation of its contents. The same function is exported as a part of the default `YAML` object, as well as seprately at `yaml/parse-cst`. It has no dependency on the rest of the library, so importing only `parseCST` should add about 9kB to your gzipped bundle size, when the whole library will add about 27kB. +The public API of the CST layer is a single function which returns an array of parsed CST documents. +The array and its contained nodes override the default `toString` method, each returning a YAML string representation of its contents. +The `parseCST` function has no dependency on the rest of the library, so importing only it should add about 9kB to your gzipped bundle size, when the whole library will add about 27kB. Care should be taken when modifying the CST, as no error checks are included to verify that the resulting YAML is valid, or that e.g. indentation levels aren't broken. In other words, this is an engineering tool and you may hurt yourself. If you're looking to generate a brand new YAML document, see the section on [Creating Documents](#creating-documents). @@ -84,10 +84,10 @@ While the YAML spec considers e.g. block collections within a flow collection to

Dealing with CRLF line terminators

```js -import parseCST from 'yaml/parse-cst' +import YAML from 'yaml' const src = '- foo\r\n- bar\r\n' -const cst = parseCST(src) +const cst = YAML.parseCST(src) cst.setOrigRanges() // true const { range, valueRange } = cst[0].contents[0].items[1].node diff --git a/index.d.ts b/index.d.ts index 6b17369c..984ded72 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -import { CST } from './parse-cst' +import { CST } from './cst' import { AST, Alias, @@ -14,7 +14,12 @@ import { import { Type, YAMLError, YAMLWarning } from './util' export { AST, CST } -export { default as parseCST } from './parse-cst' + +export function parseCST(str: string): ParsedCST + +export interface ParsedCST extends Array { + setOrigRanges(): boolean +} /** * `yaml` defines document-specific options in three places: as an argument of diff --git a/jest.config.js b/jest.config.js index fde23bdc..11b1dc2e 100644 --- a/jest.config.js +++ b/jest.config.js @@ -22,7 +22,6 @@ switch (process.env.npm_lifecycle_event) { process.env.TRACE_LEVEL = 'log' moduleNameMapper = { '^\\./dist$': '/src/index.js', - '^\\./dist/parse-cst(\\.js)?$': '/src/cst/parse.js', '^\\./dist/types(\\.js)?$': '/src/types.js', '^\\./dist/(.+)$': '/src/$1', '^\\.\\./dist/test-events.js$': '/src/test-events.js' diff --git a/package.json b/package.json index 98950256..827439d7 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ "main": "./index.js", "browser": { "./index.js": "./browser/index.js", - "./parse-cst.js": "./browser/parse-cst.js", "./types.js": "./browser/types.js", "./types.mjs": "./browser/types.js", "./util.js": "./browser/util.js", @@ -33,7 +32,6 @@ "exports": { ".": "./index.js", "./package.json": "./package.json", - "./parse-cst": "./parse-cst.js", "./types": [ { "import": "./types.mjs" diff --git a/parse-cst.js b/parse-cst.js deleted file mode 100644 index 80657565..00000000 --- a/parse-cst.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./dist/parse-cst').parse diff --git a/rollup.browser-config.js b/rollup.browser-config.js index 3ca2cb62..49ee5f85 100644 --- a/rollup.browser-config.js +++ b/rollup.browser-config.js @@ -3,7 +3,6 @@ import babel from '@rollup/plugin-babel' export default { input: { index: 'src/index.js', - 'parse-cst': 'src/cst/parse.js', types: 'src/types.js', util: 'src/util.js' }, diff --git a/rollup.node-config.js b/rollup.node-config.js index 1ab73d96..ca710f8e 100644 --- a/rollup.node-config.js +++ b/rollup.node-config.js @@ -3,7 +3,6 @@ import babel from '@rollup/plugin-babel' export default { input: { index: 'src/index.js', - 'parse-cst': 'src/cst/parse.js', 'test-events': 'src/test-events.js', types: 'src/types.js', util: 'src/util.js' diff --git a/src/cst/parse.js b/src/cst/parse.js index f9c5f9e6..613cc2a5 100644 --- a/src/cst/parse.js +++ b/src/cst/parse.js @@ -1,5 +1,3 @@ -// Published as 'yaml/parse-cst' - import { Document } from './Document.js' import { ParseContext } from './ParseContext.js' diff --git a/tests/cst/corner-cases.js b/tests/cst/corner-cases.js index 9ef16916..6ff6acc6 100644 --- a/tests/cst/corner-cases.js +++ b/tests/cst/corner-cases.js @@ -1,5 +1,6 @@ import { source } from 'common-tags' -import parse from '../../parse-cst.js' +import * as YAML from '../../index.js' +const parse = YAML.parseCST describe('folded block with chomp: keep', () => { test('nl + nl', () => { diff --git a/tests/cst/parse.js b/tests/cst/parse.js index 83c1f36d..0158898f 100644 --- a/tests/cst/parse.js +++ b/tests/cst/parse.js @@ -1,4 +1,5 @@ -import parse from '../../parse-cst.js' +import * as YAML from '../../index.js' +const parse = YAML.parseCST test('return value', () => { const src = '---\n- foo\n- bar\n' diff --git a/types.d.ts b/types.d.ts index 0d7737f3..900439fa 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1,5 +1,5 @@ import { Document, scalarOptions } from './index' -import { CST } from './parse-cst' +import { CST } from './cst' import { Type } from './util' export const binaryOptions: scalarOptions.Binary diff --git a/util.d.ts b/util.d.ts index dace24ec..50d03b15 100644 --- a/util.d.ts +++ b/util.d.ts @@ -1,4 +1,4 @@ -import { CST } from './parse-cst' +import { CST } from './cst' import { Pair, Scalar, Schema } from './types' export function findPair(items: any[], key: Scalar | any): Pair | undefined