Skip to content

Commit

Permalink
Merge pull request #234 from eemeli/fewer-ends
Browse files Browse the repository at this point in the history
Merge 'yaml/types' into 'yaml' & fiddle with 'yaml/util'
  • Loading branch information
eemeli committed Mar 6, 2021
2 parents d34e48c + 107f06b commit 03838a9
Show file tree
Hide file tree
Showing 44 changed files with 159 additions and 231 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -47,7 +47,7 @@ const YAML = require('yaml')
- [`YAML.visit(node, visitor)`](https://eemeli.org/yaml/#modifying-nodes)

```js
import { Pair, YAMLMap, YAMLSeq } from 'yaml/types'
import { Pair, YAMLMap, YAMLSeq } from 'yaml'
```

- [`new Pair(key, value)`](https://eemeli.org/yaml/#creating-nodes)
Expand Down
File renamed without changes.
39 changes: 39 additions & 0 deletions config/jest.config.js
@@ -0,0 +1,39 @@
let moduleNameMapper
const testPathIgnorePatterns = ['tests/_import', 'cst/common']

// The npm script name is significant.
switch (process.env.npm_lifecycle_event) {
case 'test:dist':
moduleNameMapper = {
'^yaml$': '<rootDir>/dist/index.js',
'^yaml/util$': '<rootDir>/dist/util.js',
'^yaml/test-events$': '<rootDir>/dist/test-events.js'
}
testPathIgnorePatterns.push('doc/createNode', 'doc/types')
break

case 'test':
default:
process.env.TRACE_LEVEL = 'log'
moduleNameMapper = {
'^yaml$': '<rootDir>/src/index.ts',
'^yaml/util$': '<rootDir>/src/util.ts',
'^yaml/test-events$': '<rootDir>/src/test-events.ts'
}
}

module.exports = {
collectCoverageFrom: ['src/**/*.{js,ts}', '!src/**/*.d.ts'],
moduleNameMapper,
resolver: 'jest-ts-webcompat-resolver',
rootDir: '..',
testEnvironment: 'node',
testMatch: ['**/tests/**/*.{js,ts}'],
testPathIgnorePatterns,
transform: {
'/(src|tests)/.*\\.(js|ts)$': [
'babel-jest',
{ configFile: './config/babel.config.js' }
]
}
}
Expand Up @@ -5,7 +5,6 @@ import typescript from '@rollup/plugin-typescript'
export default {
input: {
index: 'src/index.ts',
types: 'src/types.ts',
util: 'src/util.ts'
},
output: { dir: 'browser/dist', format: 'esm', preserveModules: true },
Expand All @@ -16,6 +15,7 @@ export default {
}),
babel({
babelHelpers: 'bundled',
configFile: './config/babel.config.js',
presets: [['@babel/env', { modules: false }]]
}),
typescript({ declaration: false, outDir: 'browser/dist' })
Expand Down
2 changes: 1 addition & 1 deletion rollup.node-config.js → config/rollup.node-config.js
Expand Up @@ -6,7 +6,6 @@ export default {
input: {
index: 'src/index.ts',
'test-events': 'src/test-events.ts',
types: 'src/types.ts',
util: 'src/util.ts'
},
output: {
Expand All @@ -18,6 +17,7 @@ export default {
plugins: [
babel({
babelHelpers: 'bundled',
configFile: './config/babel.config.js',
presets: [['@babel/env', { modules: false, targets: { node: '10.0' } }]]
}),
typescript(),
Expand Down
2 changes: 1 addition & 1 deletion docs/01_intro.md
Expand Up @@ -52,7 +52,7 @@ const YAML = require('yaml')
- [`YAML.visit(node, visitor)`](#modifying-nodes)

```js
import { Pair, YAMLMap, YAMLSeq } from 'yaml/types'
import { Pair, YAMLMap, YAMLSeq } from 'yaml'
```

- [`new Pair(key, value)`](#creating-nodes)
Expand Down
2 changes: 0 additions & 2 deletions docs/03_options.md
Expand Up @@ -98,8 +98,6 @@ YAML.stringify({ this: null, that: 'value' })

Some customization options are availabe to control the parsing and stringification of scalars. Note that these values are used by all documents.

These options objects are also exported individually from `'yaml/types'`.

| Option | Type | Default value | Description |
| ------------------ | --------- | --------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| binary.defaultType | `Type` | `'BLOCK_LITERAL'` | The type of string literal used to stringify `!!binary` values |
Expand Down
9 changes: 4 additions & 5 deletions docs/05_content_nodes.md
Expand Up @@ -175,10 +175,9 @@ The primary purpose of this method is to enable attaching comments or other meta
<h4 style="clear:both"><code>new YAMLMap(), new YAMLSeq(), doc.createPair(key, value)</code></h4>

```js
import YAML from 'yaml'
import { YAMLSeq } from 'yaml/types'
import { Document, YAMLSeq } from 'yaml'

const doc = new YAML.Document(new YAMLSeq())
const doc = new Document(new YAMLSeq())
doc.contents.items = [
'some values',
42,
Expand All @@ -194,9 +193,9 @@ doc.toString()
// - 1: a number
```

To construct a `YAMLSeq` or `YAMLMap`, use `doc.createNode()` with array, object or iterable input, or create the collections directly by importing the classes from `yaml/types`.
To construct a `YAMLSeq` or `YAMLMap`, use `new Document()` or `doc.createNode()` with array, object or iterable input, or create the collections directly by importing the classes from `yaml`.

Once created, normal array operations may be used to modify the `items` array. New `Pair` objects may created either by importing the class from `yaml/types` and using its `new Pair(key, value)` constructor, or by using the `doc.createPair(key, value, options?)` method. The latter will recursively wrap the `key` and `value` as nodes, and accepts the same options as `doc.createNode()`
Once created, normal array operations may be used to modify the `items` array. New `Pair` objects may created either by importing the class from `yaml` and using its `new Pair(key, value)` constructor, or by using the `doc.createPair(key, value, options?)` method. The latter will recursively wrap the `key` and `value` as nodes, and accepts the same options as `doc.createNode()`

## Modifying Nodes

Expand Down
5 changes: 3 additions & 2 deletions docs/06_custom_tags.md
Expand Up @@ -127,12 +127,13 @@ Finally, `stringify(item, ctx, ...): string` defines how your data should be rep
<!-- prettier-ignore -->
```js
import {
debug, // (logLevel, ...messages) => void -- Log debug messages to console
findPair, // (items, key) => Pair? -- Given a key, find a matching Pair
foldFlowLines, // (text, indent, mode, options) => string -- Fold long lines
stringifyNumber, // (node) => string
stringifyString, // (node, ctx, ...) => string
toJS, // (value, arg, ctx) => any -- Recursively convert to plain JS
Type, // { [string]: string } -- Used as enum for node types
YAMLError, YAMLParseError, YAMLWarning
warn // (logLevel, warning) => void -- Emit a warning
} from 'yaml/util'
```

Expand Down
1 change: 0 additions & 1 deletion index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion index.js

This file was deleted.

39 changes: 0 additions & 39 deletions jest.config.js

This file was deleted.

33 changes: 8 additions & 25 deletions package.json
Expand Up @@ -13,55 +13,38 @@
"homepage": "https://eemeli.org/yaml/",
"files": [
"browser/",
"dist/",
"types/",
"*.d.ts",
"*.js",
"*.mjs",
"!*config.js"
"dist/"
],
"type": "commonjs",
"main": "./index.js",
"main": "./dist/index.js",
"browser": {
"./index.js": "./browser/index.js",
"./types.js": "./browser/dist/types.js",
"./types.mjs": "./browser/dist/types.js",
"./util.js": "./browser/dist/util.js",
"./util.mjs": "./browser/dist/util.js"
},
"exports": {
".": {
"node": "./index.js",
"node": "./dist/index.js",
"default": "./browser/index.js"
},
"./package.json": "./package.json",
"./types": {
"node": {
"import": "./types.mjs",
"require": "./types.js"
},
"default": "./browser/dist/types.js"
},
"./util": {
"node": {
"import": "./util.mjs",
"require": "./util.js"
},
"node": "./dist/util.js",
"default": "./browser/dist/util.js"
}
},
"scripts": {
"build": "npm run build:node && npm run build:browser",
"build:browser": "rollup -c rollup.browser-config.js",
"build:node": "rollup -c rollup.node-config.js",
"build:browser": "rollup -c config/rollup.browser-config.js",
"build:node": "rollup -c config/rollup.node-config.js",
"clean": "git clean -fdxe node_modules",
"lint": "eslint src/",
"prettier": "prettier --write .",
"prestart": "npm run build:node",
"start": "node -i -e 'YAML=require(\"./dist/index.js\")'",
"test": "jest",
"test": "jest --config config/jest.config.js",
"test:browsers": "cd playground && npm test",
"test:dist": "npm run build:node && jest",
"test:dist": "npm run build:node && jest --config config/jest.config.js",
"test:types": "tsc --noEmit",
"docs:install": "cd docs-slate && bundle install",
"docs:deploy": "cd docs-slate && ./deploy.sh",
Expand Down
14 changes: 13 additions & 1 deletion src/index.ts
@@ -1,7 +1,13 @@
export { Composer } from './compose/composer.js'
export { Type } from './constants.js'

export { CreateNodeOptions, Document } from './doc/Document.js'
export { Options, defaultOptions, scalarOptions } from './options.js'
export { Schema } from './doc/Schema.js'

export { YAMLError, YAMLParseError, YAMLWarning } from './errors.js'

export { Alias } from './nodes/Alias.js'
export { Merge } from './nodes/Merge.js'
export {
isAlias,
isCollection,
Expand All @@ -14,6 +20,12 @@ export {
Node,
ParsedNode
} from './nodes/Node.js'
export { Pair, PairType } from './nodes/Pair.js'
export { Scalar } from './nodes/Scalar.js'
export { YAMLMap } from './nodes/YAMLMap.js'
export { YAMLSeq } from './nodes/YAMLSeq.js'

export { Options, defaultOptions, scalarOptions } from './options.js'

export { Lexer } from './parse/lexer.js'
export { LineCounter } from './parse/line-counter.js'
Expand Down
2 changes: 0 additions & 2 deletions src/log.ts
@@ -1,5 +1,3 @@
/* global console, process */

import { LogLevel, LogLevelId } from './constants.js'

export function debug(logLevel: LogLevelId, ...messages: any[]) {
Expand Down
5 changes: 3 additions & 2 deletions src/public-api.ts
Expand Up @@ -47,7 +47,7 @@ export function parseAllDocuments<T extends ParsedNode = ParsedNode>(
export function parseDocument<T extends ParsedNode = ParsedNode>(
source: string,
options?: Options
): Document.Parsed<T> | null {
) {
let doc: Document.Parsed<T> | null = null
const composer = new Composer(_doc => {
if (!doc) doc = _doc as Document.Parsed<T>
Expand All @@ -60,7 +60,8 @@ export function parseDocument<T extends ParsedNode = ParsedNode>(
const parser = new Parser(composer.next, options?.lineCounter?.addNewLine)
parser.parse(source)
composer.end(true, source.length)
return doc
// `doc` is always set by compose.end(true) at the very latest
return (doc as unknown) as Document.Parsed<T>
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/stringify/foldFlowLines.ts
Expand Up @@ -37,10 +37,6 @@ export interface FoldOptions {
* Tries to keep input at up to `lineWidth` characters, splitting only on spaces
* not followed by newlines or spaces unless `mode` is `'quoted'`. Lines are
* terminated with `\n` and started with `indent`.
*
* @param {string} text
* @param {string} indent
* @param {Object} options
*/
export function foldFlowLines(
text: string,
Expand Down
8 changes: 0 additions & 8 deletions src/types.ts

This file was deleted.

7 changes: 4 additions & 3 deletions src/util.ts
@@ -1,6 +1,7 @@
export type { LogLevelId } from './constants.js'
export { debug, warn } from './log.js'
export { findPair } from './nodes/YAMLMap.js'
export { toJS, ToJSContext } from './nodes/toJS.js'
export { toJS, ToJSAnchorValue, ToJSContext } from './nodes/toJS.js'
export { foldFlowLines } from './stringify/foldFlowLines'
export { stringifyNumber } from './stringify/stringifyNumber.js'
export { stringifyString } from './stringify/stringifyString.js'
export { Type } from './constants.js'
export * from './errors.js'
2 changes: 1 addition & 1 deletion src/visit.ts
Expand Up @@ -64,7 +64,7 @@ export type visitor =
* `Alias` and `Scalar` node.
*/
export function visit(
node: Node | Document,
node: Node | Document | null,
visitor:
| visitorFn<unknown>
| {
Expand Down
2 changes: 1 addition & 1 deletion tests/doc/YAML-1.1.spec.js
@@ -1,5 +1,5 @@
import { source } from 'common-tags'
import * as YAML from '../../index.js'
import * as YAML from 'yaml'

const orig = {}
beforeAll(() => {
Expand Down
4 changes: 2 additions & 2 deletions tests/doc/YAML-1.2.spec.js
@@ -1,5 +1,5 @@
import * as YAML from '../../index.js'
import { YAMLError } from '../../util.js'
import * as YAML from 'yaml'
import { YAMLError } from 'yaml'

const collectionKeyWarning = /^Keys with collection values will be stringified due to JS Object restrictions/

Expand Down
7 changes: 3 additions & 4 deletions tests/doc/anchors.js
@@ -1,5 +1,4 @@
import * as YAML from '../../index.js'
import { Merge, YAMLMap } from '../../types.js'
import * as YAML from 'yaml'

test('basic', () => {
const src = `- &a 1\n- *a\n`
Expand Down Expand Up @@ -162,9 +161,9 @@ describe('merge <<', () => {
])
doc.contents.items.slice(5).forEach(({ items }) => {
const merge = items[0]
expect(merge).toBeInstanceOf(Merge)
expect(merge).toBeInstanceOf(YAML.Merge)
merge.value.items.forEach(({ source }) => {
expect(source).toBeInstanceOf(YAMLMap)
expect(source).toBeInstanceOf(YAML.YAMLMap)
})
})
})
Expand Down

0 comments on commit 03838a9

Please sign in to comment.