-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
98 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/cspell-config/src/deserializers/cspellYaml.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { deserializerCSpellYaml } from './cspellYaml'; | ||
import { stringify } from 'yaml'; | ||
|
||
const oc = expect.objectContaining; | ||
|
||
describe('cspellYaml', () => { | ||
const sampleCSpellYaml = `\ | ||
version: "0.2" | ||
words: | ||
- cache | ||
`; | ||
|
||
test.each` | ||
uri | content | expected | ||
${''} | ${''} | ${undefined} | ||
${'cspell.yaml'} | ${''} | ${oc({ settings: {} })} | ||
${'cspell.yaml'} | ${'---\n{}\n'} | ${oc({ settings: {} })} | ||
${'cspell.js'} | ${''} | ${undefined} | ||
${'cspell.json'} | ${''} | ${undefined} | ||
${'cspell-ext.yml'} | ${'---\nversion: "0.2"\n'} | ${oc({ settings: { version: '0.2' } })} | ||
${'.cspell.yml'} | ${'\nwords: []\n'} | ${oc({ settings: { words: [] } })} | ||
`('success $uri', ({ uri, content, expected }) => { | ||
expect(deserializerCSpellYaml(uri, content)).toEqual(expected); | ||
}); | ||
|
||
test.each` | ||
uri | content | expected | ||
${'cspell.yaml'} | ${'"version'} | ${'Missing closing'} | ||
${'cspell.yaml'} | ${'[]'} | ${'Unable to parse cspell.yaml'} | ||
`('fail $uri', ({ uri, content, expected }) => { | ||
expect(() => deserializerCSpellYaml(uri, content)).toThrowError(expected); | ||
}); | ||
|
||
test.each` | ||
uri | content | expected | ||
${'cspell.yaml'} | ${'{\n\t"name": "name"}'} | ${toYaml({ name: 'name' }, '\t')} | ||
${'cspell.yaml?x=5'} | ${'{\n "words":[]}'} | ${toYaml({ words: [] }, 2)} | ||
${'cspell.yml'} | ${sampleCSpellYaml} | ${sampleCSpellYaml} | ||
`('serialize $uri', ({ uri, content, expected }) => { | ||
const file = deserializerCSpellYaml(uri, content); | ||
expect(file?.serialize()).toEqual(expected); | ||
}); | ||
}); | ||
|
||
function toYaml(obj: unknown, indent: string | number = 2): string { | ||
return stringify(obj, null, indent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Deserializer } from '../Deserializer'; | ||
import { ImplCSpellConfigFile } from '../CSpellConfigFile'; | ||
import { detectIndent } from './util'; | ||
import { CSpellSettings } from '@cspell/cspell-types'; | ||
import { parse, stringify } from 'yaml'; | ||
|
||
const isSupportedFormat = /\.ya?ml(?=$|[?#])/; | ||
|
||
function _deserializerCSpellYaml(uri: string, content: string): ImplCSpellConfigFile | undefined { | ||
if (!isSupportedFormat.test(uri)) return undefined; | ||
|
||
const cspell = parse(content) || {}; | ||
if (!cspell || typeof cspell !== 'object' || Array.isArray(cspell)) { | ||
throw new Error(`Unable to parse ${uri}`); | ||
} | ||
|
||
const indent = detectIndent(content); | ||
|
||
function serialize(settings: CSpellSettings) { | ||
return stringify(settings, null, indent); | ||
} | ||
|
||
return new ImplCSpellConfigFile(uri, cspell, serialize); | ||
} | ||
|
||
export const deserializerCSpellYaml: Deserializer = _deserializerCSpellYaml; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { Deserializer } from '../Deserializer'; | ||
import { deserializerCSpellJson } from './cspellJson'; | ||
import { deserializerCSpellYaml } from './cspellYaml'; | ||
import { deserializerPackageJson } from './packageJson'; | ||
|
||
export const defaultDeserializers: Deserializer[] = [deserializerPackageJson, deserializerCSpellJson]; | ||
export const defaultDeserializers: Deserializer[] = [ | ||
deserializerPackageJson, | ||
deserializerCSpellJson, | ||
deserializerCSpellYaml, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters