Merge pull request #7940 from deepseek-harness/refactor/cordis-include-yaml
refactor(cordis): route Include-dialect YAML through Include exports
This commit is contained in:
commit
94d9995755
55 files changed
+186
-237
No files matched your search
@@ -0,0 +1,39 @@
|
||||
/** Include's YAML dialect exports used by DSH configuration tools. */
|
||||
import { entryYamlErrorPosition, parseEntryYaml, stringifyEntryYaml } from '@deepseek-ai/cordis-plugin-include'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
describe('Include YAML dialect', () => {
|
||||
it('reads and writes !!js scalars as expression nodes', () => {
|
||||
const source = '- id: a\n name: example\n disabled: !!js process.env.OFF\n'
|
||||
const parsed = parseEntryYaml(source)
|
||||
expect(parsed).toEqual([{ id: 'a', name: 'example', disabled: { __jsExpr: 'process.env.OFF' } }])
|
||||
expect(stringifyEntryYaml(parsed)).toBe(source)
|
||||
})
|
||||
|
||||
it('reads binary integers as numbers and dates as strings', () => {
|
||||
expect(parseEntryYaml('- 0b101\n- 2001-01-01\n- ~\n')).toEqual([5, '2001-01-01', null])
|
||||
expect(parseEntryYaml('')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('writes repeated references in full', () => {
|
||||
const shared = { value: 1 }
|
||||
expect(stringifyEntryYaml([{ config: shared }, { config: shared }])).toBe('- config:\n value: 1\n- config:\n value: 1\n')
|
||||
})
|
||||
|
||||
it('folds long scalars at the requested line width', () => {
|
||||
const value = [{ text: Array.from({ length: 20 }, (_, index) => `word${String(index)}`).join(' ') }]
|
||||
expect(stringifyEntryYaml(value).split('\n').length).toBeGreaterThan(2)
|
||||
expect(stringifyEntryYaml(value, { lineWidth: -1 }).split('\n')).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('locates parse errors', () => {
|
||||
let error: unknown
|
||||
try {
|
||||
parseEntryYaml('- id: a\n name: [\n')
|
||||
} catch (caught) {
|
||||
error = caught
|
||||
}
|
||||
expect(entryYamlErrorPosition(error)).toEqual({ line: 3, column: 1 })
|
||||
expect(entryYamlErrorPosition(new Error('other'))).toBeUndefined()
|
||||
})
|
||||
})
|
||||
+2
-12
@@ -3,30 +3,20 @@
|
||||
* @module scripts/cordis-yaml
|
||||
*/
|
||||
|
||||
import * as yaml from 'js-yaml'
|
||||
import { parseEntryYaml } from '@deepseek-ai/cordis-plugin-include'
|
||||
|
||||
/** A Loader `!!js` expression preserved as data instead of executed. */
|
||||
export interface JsExpr {
|
||||
__jsExpr: string
|
||||
}
|
||||
|
||||
const jsExprType = new yaml.Type('tag:yaml.org,2002:js', {
|
||||
kind: 'scalar',
|
||||
resolve: data => typeof data === 'string',
|
||||
construct: (data: unknown): JsExpr => {
|
||||
if (typeof data !== 'string') throw new TypeError('!!js requires a scalar string')
|
||||
return { __jsExpr: data }
|
||||
},
|
||||
})
|
||||
const schema = yaml.JSON_SCHEMA.extend(jsExprType)
|
||||
|
||||
/**
|
||||
* Parse a Cordis config while preserving Loader `!!js` expressions as data.
|
||||
* @param source - Cordis YAML source text.
|
||||
* @returns the parsed YAML value.
|
||||
*/
|
||||
export function loadCordisYaml(source: string): unknown {
|
||||
return yaml.load(source, { schema })
|
||||
return parseEntryYaml(source)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user