diff --git a/browser/map.js b/browser/map.js deleted file mode 100644 index 78f2ebc7..00000000 --- a/browser/map.js +++ /dev/null @@ -1,2 +0,0 @@ -module.exports = require('./dist/types').YAMLMap -require('./dist/legacy-exports').warnFileDeprecation(__filename) diff --git a/browser/pair.js b/browser/pair.js deleted file mode 100644 index b2880a25..00000000 --- a/browser/pair.js +++ /dev/null @@ -1,2 +0,0 @@ -module.exports = require('./dist/types').Pair -require('./dist/legacy-exports').warnFileDeprecation(__filename) diff --git a/browser/scalar.js b/browser/scalar.js deleted file mode 100644 index deee1b03..00000000 --- a/browser/scalar.js +++ /dev/null @@ -1,2 +0,0 @@ -module.exports = require('./dist/types').Scalar -require('./dist/legacy-exports').warnFileDeprecation(__filename) diff --git a/browser/schema.js b/browser/schema.js deleted file mode 100644 index 4139c4bf..00000000 --- a/browser/schema.js +++ /dev/null @@ -1,9 +0,0 @@ -const types = require('./dist/types') -const util = require('./dist/util') - -module.exports = types.Schema -module.exports.nullOptions = types.nullOptions -module.exports.strOptions = types.strOptions -module.exports.stringify = util.stringifyString - -require('./dist/legacy-exports').warnFileDeprecation(__filename) diff --git a/browser/seq.js b/browser/seq.js deleted file mode 100644 index 66147df3..00000000 --- a/browser/seq.js +++ /dev/null @@ -1,2 +0,0 @@ -module.exports = require('./dist/types').YAMLSeq -require('./dist/legacy-exports').warnFileDeprecation(__filename) diff --git a/browser/types/binary.js b/browser/types/binary.js deleted file mode 100644 index 271b9de3..00000000 --- a/browser/types/binary.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict' -Object.defineProperty(exports, '__esModule', { value: true }) - -const legacy = require('../dist/legacy-exports') -exports.binary = legacy.binary -exports.default = [exports.binary] - -legacy.warnFileDeprecation(__filename) diff --git a/browser/types/omap.js b/browser/types/omap.js deleted file mode 100644 index 37b638b7..00000000 --- a/browser/types/omap.js +++ /dev/null @@ -1,3 +0,0 @@ -const legacy = require('../dist/legacy-exports') -module.exports = legacy.omap -legacy.warnFileDeprecation(__filename) diff --git a/browser/types/pairs.js b/browser/types/pairs.js deleted file mode 100644 index f1df201c..00000000 --- a/browser/types/pairs.js +++ /dev/null @@ -1,3 +0,0 @@ -const legacy = require('../dist/legacy-exports') -module.exports = legacy.pairs -legacy.warnFileDeprecation(__filename) diff --git a/browser/types/set.js b/browser/types/set.js deleted file mode 100644 index e7dc9d42..00000000 --- a/browser/types/set.js +++ /dev/null @@ -1,3 +0,0 @@ -const legacy = require('../dist/legacy-exports') -module.exports = legacy.set -legacy.warnFileDeprecation(__filename) diff --git a/browser/types/timestamp.js b/browser/types/timestamp.js deleted file mode 100644 index 39c5b6dd..00000000 --- a/browser/types/timestamp.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' -Object.defineProperty(exports, '__esModule', { value: true }) - -const legacy = require('../dist/legacy-exports') -exports.default = [legacy.intTime, legacy.floatTime, legacy.timestamp] -exports.floatTime = legacy.floatTime -exports.intTime = legacy.intTime -exports.timestamp = legacy.timestamp - -legacy.warnFileDeprecation(__filename) diff --git a/docs/03_options.md b/docs/03_options.md index ce76e359..2316aa36 100644 --- a/docs/03_options.md +++ b/docs/03_options.md @@ -110,4 +110,4 @@ These options objects are also exported individually from `'yaml/types'`. ## Silencing Warnings -By default, the library will emit warnings for uses of deprecated APIs and as required by the YAML spec during parsing. If you'd like to silence these, define a global variable `YAML_SILENCE_WARNINGS` with a true-ish value. To silence only deprecation warnings, use `YAML_SILENCE_DEPRECATION_WARNINGS`. These values may also be set in `process.env`. +By default, the library will emit warnings as required by the YAML spec during parsing. If you'd like to silence these, define a global or `process.env` variable `YAML_SILENCE_WARNINGS` with a true-ish value. diff --git a/map.js b/map.js deleted file mode 100644 index 78f2ebc7..00000000 --- a/map.js +++ /dev/null @@ -1,2 +0,0 @@ -module.exports = require('./dist/types').YAMLMap -require('./dist/legacy-exports').warnFileDeprecation(__filename) diff --git a/package.json b/package.json index a0b4bd33..e341c568 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "yaml", - "version": "1.10.0", + "version": "2.0.0-alpha", "license": "ISC", "author": "Eemeli Aro ", "repository": "github:eemeli/yaml", @@ -42,6 +42,7 @@ }, "exports": { ".": "./index.js", + "./package.json": "./package.json", "./parse-cst": "./parse-cst.js", "./types": [ { @@ -54,8 +55,7 @@ "import": "./util.mjs" }, "./util.js" - ], - "./": "./" + ] }, "scripts": { "build": "npm run build:node && npm run build:browser", diff --git a/pair.js b/pair.js deleted file mode 100644 index b2880a25..00000000 --- a/pair.js +++ /dev/null @@ -1,2 +0,0 @@ -module.exports = require('./dist/types').Pair -require('./dist/legacy-exports').warnFileDeprecation(__filename) diff --git a/playground b/playground index 2cc02a08..e6e37865 160000 --- a/playground +++ b/playground @@ -1 +1 @@ -Subproject commit 2cc02a08046e89375aac6bc0c8db270f2dc87cbd +Subproject commit e6e378652a365ba961d05971c1e25cbf85d9d207 diff --git a/rollup.browser-config.js b/rollup.browser-config.js index d74f4cb2..3ca2cb62 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', - 'legacy-exports': 'src/legacy-exports.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 725e969f..1ab73d96 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', - 'legacy-exports': 'src/legacy-exports.js', 'parse-cst': 'src/cst/parse.js', 'test-events': 'src/test-events.js', types: 'src/types.js', diff --git a/scalar.js b/scalar.js deleted file mode 100644 index deee1b03..00000000 --- a/scalar.js +++ /dev/null @@ -1,2 +0,0 @@ -module.exports = require('./dist/types').Scalar -require('./dist/legacy-exports').warnFileDeprecation(__filename) diff --git a/schema.js b/schema.js deleted file mode 100644 index 4139c4bf..00000000 --- a/schema.js +++ /dev/null @@ -1,9 +0,0 @@ -const types = require('./dist/types') -const util = require('./dist/util') - -module.exports = types.Schema -module.exports.nullOptions = types.nullOptions -module.exports.strOptions = types.strOptions -module.exports.stringify = util.stringifyString - -require('./dist/legacy-exports').warnFileDeprecation(__filename) diff --git a/seq.js b/seq.js deleted file mode 100644 index 66147df3..00000000 --- a/seq.js +++ /dev/null @@ -1,2 +0,0 @@ -module.exports = require('./dist/types').YAMLSeq -require('./dist/legacy-exports').warnFileDeprecation(__filename) diff --git a/src/doc/Document.js b/src/doc/Document.js index bd00be79..92fc65cf 100644 --- a/src/doc/Document.js +++ b/src/doc/Document.js @@ -1,4 +1,5 @@ import { Collection, Node, Scalar, isEmptyPath, toJSON } from '../ast/index.js' +import { defaultTagPrefix } from '../constants.js' import { YAMLError } from '../errors.js' import { documentOptions } from '../options.js' import { addComment } from '../stringify/addComment.js' @@ -152,7 +153,7 @@ export class Document { listNonDefaultTags() { return listTagNames(this.contents).filter( - t => t.indexOf(Schema.defaultPrefix) !== 0 + t => t.indexOf(defaultTagPrefix) !== 0 ) } diff --git a/src/doc/Schema.js b/src/doc/Schema.js index cda85e7a..c2a7ecd4 100644 --- a/src/doc/Schema.js +++ b/src/doc/Schema.js @@ -1,7 +1,5 @@ import { Pair } from '../ast/Pair.js' -import { defaultTagPrefix, defaultTags } from '../constants.js' import { schemas, tags } from '../tags/index.js' -import { warnOptionDeprecation } from '../warnings.js' import { createNode } from './createNode.js' import { getSchemaTags } from './getSchemaTags.js' @@ -9,36 +7,16 @@ const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0 export class Schema { - static defaultPrefix = defaultTagPrefix // TODO: remove in v2 - static defaultTags = defaultTags // TODO: remove in v2 - - constructor({ - customTags, - merge, - schema, - sortMapEntries, - tags: deprecatedCustomTags - }) { + constructor({ customTags, merge, schema, sortMapEntries }) { this.merge = !!merge this.name = schema this.sortMapEntries = sortMapEntries === true ? sortMapEntriesByKey : sortMapEntries || null - if (!customTags && deprecatedCustomTags) - warnOptionDeprecation('tags', 'customTags') - this.tags = getSchemaTags( - schemas, - tags, - customTags || deprecatedCustomTags, - schema - ) + this.tags = getSchemaTags(schemas, tags, customTags, schema) } createNode(value, wrapScalars, tagName, ctx) { - const baseCtx = { - defaultPrefix: Schema.defaultPrefix, - schema: this, - wrapScalars - } + const baseCtx = { schema: this, wrapScalars } const createCtx = ctx ? Object.assign(ctx, baseCtx) : baseCtx return createNode(value, tagName, createCtx) } diff --git a/src/doc/createNode.js b/src/doc/createNode.js index e4f5c3b1..fac627b9 100644 --- a/src/doc/createNode.js +++ b/src/doc/createNode.js @@ -1,6 +1,7 @@ import { Alias } from '../ast/Alias.js' import { Node } from '../ast/Node.js' import { Scalar } from '../ast/Scalar.js' +import { defaultTagPrefix } from '../constants.js' import { map } from '../tags/failsafe/map.js' import { seq } from '../tags/failsafe/seq.js' @@ -11,21 +12,14 @@ function findTagObject(value, tagName, tags) { if (!tagObj) throw new Error(`Tag ${tagName} not found`) return tagObj } - - // TODO: deprecate/remove class check - return tags.find( - t => - ((t.identify && t.identify(value)) || - (t.class && value instanceof t.class)) && - !t.format - ) + return tags.find(t => t.identify && t.identify(value) && !t.format) } export function createNode(value, tagName, ctx) { if (value instanceof Node) return value - const { defaultPrefix, onTagObj, prevObjects, schema, wrapScalars } = ctx + const { onTagObj, prevObjects, schema, wrapScalars } = ctx if (tagName && tagName.startsWith('!!')) - tagName = defaultPrefix + tagName.slice(2) + tagName = defaultTagPrefix + tagName.slice(2) let tagObj = findTagObject(value, tagName, schema.tags) if (!tagObj) { diff --git a/src/legacy-exports.js b/src/legacy-exports.js deleted file mode 100644 index d62d0133..00000000 --- a/src/legacy-exports.js +++ /dev/null @@ -1,7 +0,0 @@ -export { binary } from './tags/yaml-1.1/binary.js' -export { omap } from './tags/yaml-1.1/omap.js' -export { pairs } from './tags/yaml-1.1/pairs.js' -export { set } from './tags/yaml-1.1/set.js' -export { floatTime, intTime, timestamp } from './tags/yaml-1.1/timestamp.js' - -export { warnFileDeprecation } from './warnings' diff --git a/src/options.js b/src/options.js index c6780504..c0a662b4 100644 --- a/src/options.js +++ b/src/options.js @@ -17,7 +17,7 @@ export const defaultOptions = { keepBlobsInJSON: true, mapAsMap: false, maxAliasCount: 100, - prettyErrors: false, // TODO Set true in v2 + prettyErrors: true, simpleKeys: false, version: '1.2' } diff --git a/src/stringify/stringify.js b/src/stringify/stringify.js index de0a2305..5ac1a7e8 100644 --- a/src/stringify/stringify.js +++ b/src/stringify/stringify.js @@ -16,11 +16,7 @@ function getTagObject(tags, item) { let tagObj, obj if (item instanceof Scalar) { obj = item.value - // TODO: deprecate/remove class check - const match = tags.filter( - t => - (t.identify && t.identify(obj)) || (t.class && obj instanceof t.class) - ) + const match = tags.filter(t => t.identify && t.identify(obj)) tagObj = match.find(t => t.format === item.format) || match.find(t => !t.format) } else { diff --git a/src/warnings.js b/src/warnings.js index 8afa5dcd..95262592 100644 --- a/src/warnings.js +++ b/src/warnings.js @@ -1,51 +1,20 @@ -/* global console, process, YAML_SILENCE_DEPRECATION_WARNINGS, YAML_SILENCE_WARNINGS */ +/* global console, process, YAML_SILENCE_WARNINGS */ -function shouldWarn(deprecation) { - const env = (typeof process !== 'undefined' && process.env) || {} +export function warn(warning, type) { + if (typeof YAML_SILENCE_WARNINGS !== 'undefined' && YAML_SILENCE_WARNINGS) + return - if (deprecation) { - if (typeof YAML_SILENCE_DEPRECATION_WARNINGS !== 'undefined') - return !YAML_SILENCE_DEPRECATION_WARNINGS - return !env.YAML_SILENCE_DEPRECATION_WARNINGS - } + if (typeof process !== 'undefined') { + if (process.env.YAML_SILENCE_WARNINGS) return - if (typeof YAML_SILENCE_WARNINGS !== 'undefined') - return !YAML_SILENCE_WARNINGS - return !env.YAML_SILENCE_WARNINGS -} - -export function warn(warning, type) { - if (shouldWarn(false)) { - const emit = typeof process !== 'undefined' && process.emitWarning // This will throw in Jest if `warning` is an Error instance due to // https://github.com/facebook/jest/issues/2549 - if (emit) emit(warning, type) - else { - // eslint-disable-next-line no-console - console.warn(type ? `${type}: ${warning}` : warning) + if (process.emitWarning) { + process.emitWarning(warning, type) + return } } -} -export function warnFileDeprecation(filename) { - if (shouldWarn(true)) { - const path = filename - .replace(/.*yaml[/\\]/i, '') - .replace(/\.js$/, '') - .replace(/\\/g, '/') - warn( - `The endpoint 'yaml/${path}' will be removed in a future release.`, - 'DeprecationWarning' - ) - } -} - -const warned = {} -export function warnOptionDeprecation(name, alternative) { - if (!warned[name] && shouldWarn(true)) { - warned[name] = true - let msg = `The option '${name}' will be removed in a future release` - msg += alternative ? `, use '${alternative}' instead.` : '.' - warn(msg, 'DeprecationWarning') - } + // eslint-disable-next-line no-console + console.warn(type ? `${type}: ${warning}` : warning) } diff --git a/tests/doc/YAML-1.2.spec.js b/tests/doc/YAML-1.2.spec.js index 1bc83901..94377c6c 100644 --- a/tests/doc/YAML-1.2.spec.js +++ b/tests/doc/YAML-1.2.spec.js @@ -1833,7 +1833,7 @@ matches %: 20`, } } -let origFoldOptions +let origFoldOptions, origPrettyErrors beforeAll(() => { origFoldOptions = YAML.scalarOptions.str.fold @@ -1841,10 +1841,13 @@ beforeAll(() => { lineWidth: 20, minContentWidth: 0 } + origPrettyErrors = YAML.defaultOptions.prettyErrors + YAML.defaultOptions.prettyErrors = false }) afterAll(() => { YAML.scalarOptions.str.fold = origFoldOptions + YAML.defaultOptions.prettyErrors = origPrettyErrors }) for (const section in spec) { diff --git a/tests/doc/errors.js b/tests/doc/errors.js index 02f3311a..d8ec4d51 100644 --- a/tests/doc/errors.js +++ b/tests/doc/errors.js @@ -1,13 +1,16 @@ -import fs from 'fs' -import path from 'path' import { Node } from '../../src/cst/Node.js' import { YAMLError } from '../../src/errors.js' -import { - warnFileDeprecation, - warnOptionDeprecation -} from '../../src/warnings.js' import { YAML } from '../../src/index.js' +let origPrettyErrors +beforeAll(() => { + origPrettyErrors = YAML.defaultOptions.prettyErrors + YAML.defaultOptions.prettyErrors = false +}) +afterAll(() => { + YAML.defaultOptions.prettyErrors = origPrettyErrors +}) + test('require a message and source for all errors', () => { const exp = /Invalid arguments/ expect(() => new YAMLError()).toThrow(exp) @@ -278,83 +281,28 @@ test('multiple tags on one node', () => { expect(doc.warnings).toMatchObject([{}]) }) -describe('deprecations', () => { +describe('warnings', () => { let mock beforeEach(() => { mock = jest.spyOn(global.process, 'emitWarning').mockImplementation() }) afterEach(() => mock.mockRestore()) - describe('env vars', () => { - let prevAll, prevDeprecations - beforeEach(() => { - prevAll = global.YAML_SILENCE_WARNINGS - prevDeprecations = global.YAML_SILENCE_DEPRECATION_WARNINGS - }) - afterEach(() => { - global.YAML_SILENCE_WARNINGS = prevAll - global.YAML_SILENCE_DEPRECATION_WARNINGS = prevDeprecations - }) - - test('YAML_SILENCE_WARNINGS', () => { - global.YAML_SILENCE_WARNINGS = true - warnFileDeprecation('foo') - warnOptionDeprecation('bar1', 'baz') - expect(mock).toHaveBeenCalledTimes(0) - }) - - test('YAML_SILENCE_DEPRECATION_WARNINGS', () => { - global.YAML_SILENCE_DEPRECATION_WARNINGS = true - warnFileDeprecation('foo') - warnOptionDeprecation('bar2', 'baz') - expect(mock).toHaveBeenCalledTimes(0) - }) + test('warn for tag fallback', () => { + YAML.parse('!foo bar') + const message = + 'The tag !foo is unavailable, falling back to tag:yaml.org,2002:str' + expect(mock.mock.calls).toMatchObject([[{ message }, undefined]]) }) - test('only warn once', () => { - warnOptionDeprecation('bar3') - warnOptionDeprecation('bar3') - expect(mock).toHaveBeenCalledTimes(1) - }) - - test('without process.emitWarning', () => { - global.process.emitWarning = null - const cMock = jest.spyOn(console, 'warn').mockImplementation() + test('silence with env var', () => { + const prev = global.YAML_SILENCE_WARNINGS try { - warnFileDeprecation('foo') - warnOptionDeprecation('bar4', 'baz') - expect(cMock).toHaveBeenCalledTimes(2) + global.YAML_SILENCE_WARNINGS = true + YAML.parse('!foo bar') + expect(mock).toHaveBeenCalledTimes(0) } finally { - cMock.mockRestore() + global.YAML_SILENCE_WARNINGS = prev } }) - - test('tags option', () => { - const doc = new YAML.Document({ tags: [] }) - doc.setSchema() - expect(mock).toHaveBeenCalledTimes(1) - }) - - const files = [ - 'map', - 'pair', - 'scalar', - 'schema', - 'seq', - 'types/binary', - 'types/omap', - 'types/pairs', - 'types/set', - 'types/timestamp' - ] - const root = path.resolve(__dirname, '../..') - const fileTest = fs.existsSync(path.resolve(root, 'dist')) ? test : test.skip - for (const file of files) - fileTest(`file: ${file}`, () => { - const fp = path.resolve(root, file) - // await import() may fail with ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING due - // to https://github.com/facebook/jest/issues/9430 - require(fp) - expect(mock).toHaveBeenCalledTimes(1) - }) }) diff --git a/tests/doc/parse.js b/tests/doc/parse.js index b81676b7..3e10b9ff 100644 --- a/tests/doc/parse.js +++ b/tests/doc/parse.js @@ -576,7 +576,7 @@ describe('handling complex keys', () => { }) test('add warning to doc when casting key in collection to string', () => { - const doc = YAML.parseDocument('[foo]: bar') + const doc = YAML.parseDocument('[foo]: bar', { prettyErrors: false }) const message = 'Keys with collection values will be stringified as YAML due to JS Object restrictions. Use mapAsMap: true to avoid this.' expect(doc.warnings).toMatchObject([{ message }]) diff --git a/tests/doc/types.js b/tests/doc/types.js index 20ff67c5..cd360647 100644 --- a/tests/doc/types.js +++ b/tests/doc/types.js @@ -4,7 +4,7 @@ import { binary } from '../../src/tags/yaml-1.1/binary.js' import { YAMLOMap } from '../../src/tags/yaml-1.1/omap.js' import { YAMLSet } from '../../src/tags/yaml-1.1/set.js' -let origFoldOptions +let origFoldOptions, origPrettyErrors beforeAll(() => { origFoldOptions = YAML.scalarOptions.str.fold @@ -12,10 +12,13 @@ beforeAll(() => { lineWidth: 20, minContentWidth: 0 } + origPrettyErrors = YAML.defaultOptions.prettyErrors + YAML.defaultOptions.prettyErrors = false }) afterAll(() => { YAML.scalarOptions.str.fold = origFoldOptions + YAML.defaultOptions.prettyErrors = origPrettyErrors }) describe('json schema', () => { diff --git a/types.d.ts b/types.d.ts index 411e74e8..07c83327 100644 --- a/types.d.ts +++ b/types.d.ts @@ -9,16 +9,6 @@ export const nullOptions: scalarOptions.Null export const strOptions: scalarOptions.Str export class Schema { - /** Default: `'tag:yaml.org,2002:'` */ - static defaultPrefix: string - static defaultTags: { - /** Default: `'tag:yaml.org,2002:map'` */ - MAP: string - /** Default: `'tag:yaml.org,2002:seq'` */ - SEQ: string - /** Default: `'tag:yaml.org,2002:str'` */ - STR: string - } constructor(options: Schema.Options) /** * Convert any value into a `Node` using this schema, recursively turning diff --git a/types/binary.js b/types/binary.js deleted file mode 100644 index 271b9de3..00000000 --- a/types/binary.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict' -Object.defineProperty(exports, '__esModule', { value: true }) - -const legacy = require('../dist/legacy-exports') -exports.binary = legacy.binary -exports.default = [exports.binary] - -legacy.warnFileDeprecation(__filename) diff --git a/types/omap.js b/types/omap.js deleted file mode 100644 index 37b638b7..00000000 --- a/types/omap.js +++ /dev/null @@ -1,3 +0,0 @@ -const legacy = require('../dist/legacy-exports') -module.exports = legacy.omap -legacy.warnFileDeprecation(__filename) diff --git a/types/pairs.js b/types/pairs.js deleted file mode 100644 index f1df201c..00000000 --- a/types/pairs.js +++ /dev/null @@ -1,3 +0,0 @@ -const legacy = require('../dist/legacy-exports') -module.exports = legacy.pairs -legacy.warnFileDeprecation(__filename) diff --git a/types/set.js b/types/set.js deleted file mode 100644 index e7dc9d42..00000000 --- a/types/set.js +++ /dev/null @@ -1,3 +0,0 @@ -const legacy = require('../dist/legacy-exports') -module.exports = legacy.set -legacy.warnFileDeprecation(__filename) diff --git a/types/timestamp.js b/types/timestamp.js deleted file mode 100644 index 39c5b6dd..00000000 --- a/types/timestamp.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' -Object.defineProperty(exports, '__esModule', { value: true }) - -const legacy = require('../dist/legacy-exports') -exports.default = [legacy.intTime, legacy.floatTime, legacy.timestamp] -exports.floatTime = legacy.floatTime -exports.intTime = legacy.intTime -exports.timestamp = legacy.timestamp - -legacy.warnFileDeprecation(__filename)