Skip to content

Commit

Permalink
feature(@putout/processor-yaml) js-yaml -> yaml: useless quotes (node…
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Oct 28, 2021
1 parent 0e0a84b commit e207f0e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 31 deletions.
8 changes: 4 additions & 4 deletions packages/engine-processor/test/processor.js
Expand Up @@ -469,11 +469,11 @@ test('putout: engine-processor: yaml: duplicate', async (t) => {

const expected = [{
position: {
column: 1,
line: 4,
column: 3,
line: 2,
},
message: 'Duplicated mapping key (4:3)',
rule: 'duplicated-mapping-key (yaml)',
message: `Map keys must be unique; "only" is repeated`,
rule: 'yaml-semantic-error (yaml)',
}];

t.deepEqual(places, expected);
Expand Down
58 changes: 38 additions & 20 deletions packages/processor-yaml/lib/yaml.js
@@ -1,8 +1,9 @@
'use strict';

const tryCatch = require('try-catch');
const justKebabCase = require('just-kebab-case');

const bigFirst = (a) => a[0].toUpperCase() + a.slice(1);
const parseRule = (a) => justKebabCase(a.replace('YAML', 'Yaml'));
const {stringify, parse} = JSON;

module.exports.files = [
Expand All @@ -11,11 +12,11 @@ module.exports.files = [
];

module.exports.branch = (rawSource) => {
const yaml = require('js-yaml');
const yaml = require('yaml');
const jsonProcessor = require('@putout/processor-json');

const list = [];
const [error, value] = tryCatch(yaml.load, rawSource);
const [error, value] = tryCatch(yaml.parse, rawSource);

if (error)
return [];
Expand All @@ -32,40 +33,57 @@ module.exports.branch = (rawSource) => {
};

module.exports.find = (rawSource) => {
const yaml = require('js-yaml');
const yaml = require('yaml');

const [error] = tryCatch(yaml.load, rawSource);
const places = parsePlaces(error);
const [error] = tryCatch(yaml.parse, rawSource);
const places = parsePlaces({
error,
rawSource,
});

return places;
};

module.exports.merge = (rawSource, list) => {
const yaml = require('js-yaml');
const yaml = require('yaml');
const jsonProcessor = require('@putout/processor-json');
const source = jsonProcessor.merge(rawSource, list);

return yaml.dump(parse(source));
return yaml.stringify(parse(source));
};

function parsePlaces(error) {
function parsePlaces({error, rawSource}) {
if (!error)
return [];

const {mark, reason} = error;
const {line} = mark;
const position = {
line: line + 1,
column: 1,
};

const [message] = error.message.split('\n');
const {message, source} = error;
const {start} = source.range;
const [rule] = String(error).split(':');
const place = {
message: bigFirst(message),
position,
rule: `${reason.replace(/\s/g, '-')} (yaml)`,
message,
rule: `${parseRule(rule)} (yaml)`,
position: parsePosition({
start,
rawSource,
}),
};

return [place];
}

function parsePosition({start, rawSource}) {
let line = 1;
let lastLineStart = 0;

for (let i = 0; i <= start; i++) {
if (rawSource[i] === '\n') {
++line;
lastLineStart = i;
}
}

return {
line,
column: start - lastLineStart,
};
}
6 changes: 3 additions & 3 deletions packages/processor-yaml/package.json
Expand Up @@ -24,9 +24,9 @@
},
"dependencies": {
"@putout/processor-json": "^3.0.0",
"fullstore": "^3.0.0",
"js-yaml": "^4.0.0",
"try-catch": "^3.0.0"
"just-kebab-case": "^3.0.0",
"try-catch": "^3.0.0",
"yaml": "^1.10.2"
},
"keywords": [
"putout",
Expand Down
11 changes: 11 additions & 0 deletions packages/processor-yaml/test/fixture/actions.yml
@@ -0,0 +1,11 @@
name: Node CI
on:
- push
- pull_request
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 14.x
12 changes: 8 additions & 4 deletions packages/processor-yaml/test/yaml.js
Expand Up @@ -11,14 +11,18 @@ test('putout: processor: yaml', async ({process}) => {
await process('travis.yml', ['travis/disable-cache']);
});

test('putout: processor: yaml: actions', async ({noProcess}) => {
await noProcess('actions.yml');
});

test('putout: processor: yaml: duplicate', async ({comparePlaces}) => {
await comparePlaces('duplicate.yml', [{
position: {
column: 1,
line: 4,
column: 3,
line: 2,
},
message: 'Duplicated mapping key (4:3)',
rule: 'duplicated-mapping-key (yaml)',
message: `Map keys must be unique; "only" is repeated`,
rule: 'yaml-semantic-error (yaml)',
}]);
});

Expand Down

0 comments on commit e207f0e

Please sign in to comment.