Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename tags option as customTags #107

Merged
merged 3 commits into from
Apr 5, 2019
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
2 changes: 1 addition & 1 deletion docs
2 changes: 1 addition & 1 deletion map.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module.exports = require('./dist/schema/Map').default
require('./dist/deprecation').warn(__filename)
require('./dist/deprecation').warnFileDeprecation(__filename)
2 changes: 1 addition & 1 deletion pair.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module.exports = require('./dist/schema/Pair').default
require('./dist/deprecation').warn(__filename)
require('./dist/deprecation').warnFileDeprecation(__filename)
2 changes: 1 addition & 1 deletion scalar.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module.exports = require('./dist/schema/Scalar').default
require('./dist/deprecation').warn(__filename)
require('./dist/deprecation').warnFileDeprecation(__filename)
2 changes: 1 addition & 1 deletion schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module.exports.nullOptions = opt.nullOptions
module.exports.strOptions = opt.strOptions
module.exports.stringify = require('./dist/stringify').stringifyString

require('./dist/deprecation').warn(__filename)
require('./dist/deprecation').warnFileDeprecation(__filename)
2 changes: 1 addition & 1 deletion seq.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module.exports = require('./dist/schema/Seq').default
require('./dist/deprecation').warn(__filename)
require('./dist/deprecation').warnFileDeprecation(__filename)
28 changes: 21 additions & 7 deletions src/deprecation.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
/* global global, console */
export function warn(filename) {
if (global && global._YAML_SILENCE_DEPRECATION_WARNINGS) return
const path = filename
.replace(/.*yaml[/\\]/i, '')
.replace(/\.js$/, '')
.replace(/\\/g, '/')
const msg = `The endpoint 'yaml/${path}' will be removed in a future release.`

function warn(msg) {
if (global && global.process && global.process.emitWarning) {
global.process.emitWarning(msg, 'DeprecationWarning')
} else {
// eslint-disable-next-line no-console
console.warn(`DeprecationWarning: ${msg}`)
}
}

export function warnFileDeprecation(filename) {
if (global && global._YAML_SILENCE_DEPRECATION_WARNINGS) return
const path = filename
.replace(/.*yaml[/\\]/i, '')
.replace(/\.js$/, '')
.replace(/\\/g, '/')
warn(`The endpoint 'yaml/${path}' will be removed in a future release.`)
}

const warned = {}
export function warnOptionDeprecation(name, alternative) {
if (global && global._YAML_SILENCE_DEPRECATION_WARNINGS) return
if (warned[name]) return
warned[name] = true
let msg = `The option '${name}' will be removed in a future release`
msg += alternative ? `, use '${alternative}' instead.` : '.'
warn(msg)
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const defaultOptions = {
keepBlobsInJSON: true,
mapAsMap: false,
maxAliasCount: 100,
tags: null,
customTags: null,
version: '1.2'
}

Expand Down
7 changes: 6 additions & 1 deletion src/schema/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { warnOptionDeprecation } from '../deprecation'
import { Type } from '../constants'
import { YAMLReferenceError, YAMLWarning } from '../errors'
import { stringifyString } from '../stringify'
Expand All @@ -22,7 +23,7 @@ export default class Schema {
STR: 'tag:yaml.org,2002:str'
}

constructor({ merge, schema, tags: customTags }) {
constructor({ customTags, merge, schema, tags: deprecatedCustomTags }) {
this.merge = !!merge
this.name = schema
this.tags = schemas[schema.replace(/\W/g, '')] // 'yaml-1.1' -> 'yaml11'
Expand All @@ -32,6 +33,10 @@ export default class Schema {
.join(', ')
throw new Error(`Unknown schema "${schema}"; use one of ${keys}`)
}
if (!customTags && deprecatedCustomTags) {
customTags = deprecatedCustomTags
warnOptionDeprecation('tags', 'customTags')
}
if (Array.isArray(customTags)) {
for (const tag of customTags) this.tags = this.tags.concat(tag)
} else if (typeof customTags === 'function') {
Expand Down
22 changes: 11 additions & 11 deletions tests/doc/YAML-1.2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ alias: *anchor`,
tag: '!local',
resolve: (doc, node) => 'local:' + node.strValue
}
const res = YAML.parse(src, { tags: [tag] })
const res = YAML.parse(src, { customTags: [tag] })
expect(res).toMatchObject({
anchored: 'local:value',
alias: 'local:value'
Expand Down Expand Up @@ -995,7 +995,7 @@ bar`,
]
],
special: src => {
const tags = [
const customTags = [
{
tag: '!foo',
resolve: () => 'private'
Expand All @@ -1005,7 +1005,7 @@ bar`,
resolve: () => 'global'
}
]
const docs = YAML.parseAllDocuments(src, { tags })
const docs = YAML.parseAllDocuments(src, { customTags })
expect(docs.map(d => d.toJSON())).toMatchObject(['private', 'global'])
}
},
Expand All @@ -1025,7 +1025,7 @@ bar`,
tag: 'tag:example.com,2000:app/int',
resolve: () => 'interval'
}
const res = YAML.parse(src, { tags: [tag] })
const res = YAML.parse(src, { customTags: [tag] })
expect(res).toBe('interval')
}
},
Expand All @@ -1045,7 +1045,7 @@ bar`,
tag: 'tag:example.com,2000:app/foo',
resolve: (doc, node) => 'foo' + node.strValue
}
const res = YAML.parse(src, { tags: [tag] })
const res = YAML.parse(src, { customTags: [tag] })
expect(res).toBe('foobar')
}
},
Expand All @@ -1072,7 +1072,7 @@ bar`,
tag: '!my-light',
resolve: (doc, node) => 'light:' + node.strValue
}
const docs = YAML.parseAllDocuments(src, { tags: [tag] })
const docs = YAML.parseAllDocuments(src, { customTags: [tag] })
expect(docs.map(d => d.toJSON())).toMatchObject([
'light:fluorescent',
'light:green'
Expand All @@ -1095,7 +1095,7 @@ bar`,
tag: 'tag:example.com,2000:app/foo',
resolve: (doc, node) => 'foo' + node.strValue
}
const res = YAML.parse(src, { tags: [tag] })
const res = YAML.parse(src, { customTags: [tag] })
expect(res).toMatchObject(['foobar'])
}
}
Expand All @@ -1120,7 +1120,7 @@ bar`,
tag: '!bar',
resolve: (doc, node) => 'bar' + node.strValue
}
const res = YAML.parse(src, { tags: [tag] })
const res = YAML.parse(src, { customTags: [tag] })
expect(res).toMatchObject({ foo: 'barbaz' })
}
},
Expand Down Expand Up @@ -1149,7 +1149,7 @@ bar`,
]
],
special: src => {
const tags = [
const customTags = [
{
tag: '!local',
resolve: (doc, node) => 'local:' + node.strValue
Expand All @@ -1159,7 +1159,7 @@ bar`,
resolve: (doc, node) => 'tag!' + node.strValue
}
]
const res = YAML.parse(src, { tags })
const res = YAML.parse(src, { customTags })
expect(res).toMatchObject(['local:foo', 'bar', 'tag!baz'])
}
},
Expand Down Expand Up @@ -1712,7 +1712,7 @@ folded:
tag: '!foo',
resolve: (doc, node) => 'foo' + node.strValue
}
const res = YAML.parse(src, { tags: [tag] })
const res = YAML.parse(src, { customTags: [tag] })
expect(res).toMatchObject({ literal: 'value\n', folded: 'foovalue\n' })
}
},
Expand Down
4 changes: 2 additions & 2 deletions tests/doc/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ describe('eemeli/yaml#80: custom tags', () => {
}

beforeAll(() => {
YAML.defaultOptions.tags = [regexp, sharedSymbol]
YAML.defaultOptions.customTags = [regexp, sharedSymbol]
})

afterAll(() => {
YAML.defaultOptions.tags = []
YAML.defaultOptions.customTags = []
})

describe('RegExp', () => {
Expand Down
8 changes: 4 additions & 4 deletions tests/doc/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,22 +615,22 @@ invoice:
AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=`

test('tag object in tags', () => {
const bin = YAML.parse(src, { tags: [binary] })
const bin = YAML.parse(src, { customTags: [binary] })
expect(bin).toBeInstanceOf(Uint8Array)
})

test('tag array in tags', () => {
const bin = YAML.parse(src, { tags: [[binary]] })
const bin = YAML.parse(src, { customTags: [[binary]] })
expect(bin).toBeInstanceOf(Uint8Array)
})

test('tag string in tags', () => {
const bin = YAML.parse(src, { tags: ['binary'] })
const bin = YAML.parse(src, { customTags: ['binary'] })
expect(bin).toBeInstanceOf(Uint8Array)
})

test('tag string in tag array', () => {
const bin = YAML.parse(src, { tags: [['binary']] })
const bin = YAML.parse(src, { customTags: [['binary']] })
expect(bin).toBeInstanceOf(Uint8Array)
})

Expand Down
2 changes: 1 addition & 1 deletion types/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Object.defineProperty(exports, '__esModule', { value: true })
exports.binary = require('../dist/tags/yaml-1.1/binary').default
exports.default = [exports.binary]

require('../dist/deprecation').warn(__filename)
require('../dist/deprecation').warnFileDeprecation(__filename)
2 changes: 1 addition & 1 deletion types/omap.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module.exports = require('../dist/tags/yaml-1.1/omap').default
require('../dist/deprecation').warn(__filename)
require('../dist/deprecation').warnFileDeprecation(__filename)
2 changes: 1 addition & 1 deletion types/pairs.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module.exports = require('../dist/tags/yaml-1.1/pairs').default
require('../dist/deprecation').warn(__filename)
require('../dist/deprecation').warnFileDeprecation(__filename)
2 changes: 1 addition & 1 deletion types/set.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module.exports = require('../dist/tags/yaml-1.1/set').default
require('../dist/deprecation').warn(__filename)
require('../dist/deprecation').warnFileDeprecation(__filename)
2 changes: 1 addition & 1 deletion types/timestamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ exports.floatTime = ts.floatTime
exports.intTime = ts.intTime
exports.timestamp = ts.timestamp

require('../dist/deprecation').warn(__filename)
require('../dist/deprecation').warnFileDeprecation(__filename)