Skip to content

Commit

Permalink
Add support for .yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed May 20, 2022
1 parent 39968e4 commit 890604f
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 7 deletions.
3 changes: 3 additions & 0 deletions cspell.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
{
"path": "packages/cspell-bundled-dicts"
},
{
"path": "packages/cspell-config"
},
{
"path": "packages/cspell-dynamic-loader"
},
Expand Down
11 changes: 11 additions & 0 deletions packages/cspell-config/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,14 @@ exports[`cspell-config edit config 3`] = `
}
"
`;

exports[`cspell-config edit config 4`] = `
"version: \\"0.2\\"
words:
- angle
- apple
- before
- cache
- zebra
"
`;
4 changes: 2 additions & 2 deletions packages/cspell-config/src/deserializers/cspellJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { detectIndent } from './util';
import { CSpellSettings } from '@cspell/cspell-types';
import { parse, stringify } from 'comment-json';

const isPackageJsonFile = /\.jsonc?(?=$|[?#])/;
const isSupportedFormat = /\.jsonc?(?=$|[?#])/;

function _deserializerCSpellJson(uri: string, content: string): ImplCSpellConfigFile | undefined {
if (!isPackageJsonFile.test(uri)) return undefined;
if (!isSupportedFormat.test(uri)) return undefined;

const cspell = parse(content);
if (!cspell || typeof cspell !== 'object' || Array.isArray(cspell)) {
Expand Down
47 changes: 47 additions & 0 deletions packages/cspell-config/src/deserializers/cspellYaml.test.ts
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);
}
26 changes: 26 additions & 0 deletions packages/cspell-config/src/deserializers/cspellYaml.ts
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;
7 changes: 6 additions & 1 deletion packages/cspell-config/src/deserializers/index.ts
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,
];
4 changes: 2 additions & 2 deletions packages/cspell-config/src/deserializers/packageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { ImplCSpellConfigFile } from '../CSpellConfigFile';
import { detectIndent } from './util';
import { CSpellSettings } from '@cspell/cspell-types';

const isPackageJsonFile = /package\.json(?=$|[?#])/;
const isSupportedFormat = /package\.json(?=$|[?#])/;

function _deserializerPackageJson(uri: string, content: string): ImplCSpellConfigFile | undefined {
if (!isPackageJsonFile.test(uri)) return undefined;
if (!isSupportedFormat.test(uri)) return undefined;

const packageJson = JSON.parse(content);
if (!packageJson || typeof packageJson !== 'object' || Array.isArray(packageJson)) {
Expand Down
3 changes: 1 addition & 2 deletions packages/cspell-config/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ describe('cspell-config', () => {
${'package/with-value/package.json'} | ${['apple']}
${'package/without-value/package.json'} | ${['apple']}
${'cspell.jsonc'} | ${['apple', 'cache']}
${'cspell.yaml'} | ${['apple', 'cache']}
`('edit config', async ({ fixture, addWords }) => {
const fixtureFile = fixtures(fixture);
console.log('Fixture file: %s', fixtureFile);
const tempFile = tempPath(fixture);
console.log('temp file: %s', tempFile);
await copyFile(fixtureFile, tempFile);
const rw = createReaderWriter();
const uri = URI.file(tempFile).toString();
Expand Down

0 comments on commit 890604f

Please sign in to comment.