Skip to content

Commit

Permalink
Reformat all code with new prettier 2 configuration 📝
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrieanKhisbe authored and azeemba committed May 4, 2021
1 parent bb3921a commit a720fd2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 80 deletions.
62 changes: 31 additions & 31 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const ErrorCodes = {
TrailingComma: 0x207,
DuplicateKey: 0x208,
CommentNotPermitted: 0x209,
SchemaResolveError: 0x300
SchemaResolveError: 0x300,
};

const AllErrorCodes = _.values(ErrorCodes);
Expand All @@ -31,25 +31,25 @@ const fileLintResults = {};
const fileComments = {};
const fileDocuments = {};

const getSignature = problem =>
const getSignature = (problem) =>
`${problem.range.start.line} ${problem.range.start.character} ${problem.message}`;

function getDiagnostics(jsonDocument) {
return _.pipe(
_.map(problem => [getSignature(problem), problem]),
_.map((problem) => [getSignature(problem), problem]),
_.reverse, // reverse ensure fromPairs keep first signature occurence of problem
_.fromPairs
)(jsonDocument.syntaxErrors);
}
const reportError = filter => (errorName, context) => {
_.filter(filter, fileLintResults[context.getFilename()]).forEach(error => {
const reportError = (filter) => (errorName, context) => {
_.filter(filter, fileLintResults[context.getFilename()]).forEach((error) => {
context.report({
ruleId: `json/${errorName}`,
message: error.message,
loc: {
start: {line: error.range.start.line + 1, column: error.range.start.character},
end: {line: error.range.end.line + 1, column: error.range.end.character}
}
end: {line: error.range.end.line + 1, column: error.range.end.character},
},
// later: see how to add fix
});
});
Expand All @@ -58,14 +58,14 @@ const reportComment = (errorName, context) => {
const ruleOption = _.head(context.options);
if (ruleOption === AllowComments || _.get(AllowComments, ruleOption)) return;

_.forEach(comment => {
_.forEach((comment) => {
context.report({
ruleId: errorName,
message: 'Comment not allowed',
loc: {
start: {line: comment.start.line + 1, column: comment.start.character},
end: {line: comment.end.line + 1, column: comment.end.character}
}
end: {line: comment.end.line + 1, column: comment.end.character},
},
});
}, fileComments[context.getFilename()]);
};
Expand All @@ -74,10 +74,10 @@ const makeRule = (errorName, reporters) => ({
create(context) {
return {
Program() {
_.flatten([reporters]).map(reporter => reporter(errorName, context));
}
_.flatten([reporters]).map((reporter) => reporter(errorName, context));
},
};
}
},
});

const rules = _.pipe(
Expand All @@ -87,41 +87,41 @@ const rules = _.pipe(
errorName,
makeRule(
errorName,
reportError(err => err.code === errorCode)
)
reportError((err) => err.code === errorCode)
),
]),
_.fromPairs,
_.assign({
'*': makeRule('*', [reportError(_.constant(true)), reportComment]),
json: makeRule('json', [reportError(_.constant(true)), reportComment]),
unknown: makeRule('unknown', reportError(_.negate(AllErrorCodes.includes))),
'comment-not-permitted': makeRule('comment-not-permitted', reportComment)
'comment-not-permitted': makeRule('comment-not-permitted', reportComment),
})
)(ErrorCodes);

const errorSignature = err =>
['message', 'line', 'column', 'endLine', 'endColumn'].map(field => err[field]).join('::');
const errorSignature = (err) =>
['message', 'line', 'column', 'endLine', 'endColumn'].map((field) => err[field]).join('::');

const getErrorCode = _.pipe(_.get('ruleId'), _.split('/'), _.last);

const processors = {
'.json': {
preprocess: function(text, fileName) {
preprocess: function (text, fileName) {
const textDocument = jsonService.TextDocument.create(fileName, 'json', 1, text);
fileDocuments[fileName] = textDocument;
const parsed = jsonServiceHandle.parseJSONDocument(textDocument);
fileLintResults[fileName] = getDiagnostics(parsed);
fileComments[fileName] = parsed.comments;
return ['']; // sorry nothing ;)
},
postprocess: function(messages, fileName) {
postprocess: function (messages, fileName) {
const textDocument = fileDocuments[fileName];
delete fileLintResults[fileName];
delete fileComments[fileName];
return _.pipe(
_.first,
_.groupBy(errorSignature),
_.mapValues(errors => {
_.mapValues((errors) => {
if (errors.length === 1) return _.first(errors);
// Otherwise there is two errors: the generic and specific one
// json/* or json/json and json/some-code
Expand All @@ -133,36 +133,36 @@ const processors = {
? genericError
: specificError;
}),
_.mapValues(error => {
_.mapValues((error) => {
const source = textDocument.getText({
start: {line: error.line - 1, character: error.column},
end: {line: error.endLine - 1, character: error.endColumn}
end: {line: error.endLine - 1, character: error.endColumn},
});
return _.assign(error, {
source,
column: error.column + 1,
endColumn: error.endColumn + 1
endColumn: error.endColumn + 1,
});
}),
_.values
)(messages);
}
}
},
},
};

const configs = {
recommended: {
plugins: ['json'],
rules: {
'json/*': 'error'
}
'json/*': 'error',
},
},
'recommended-with-comments': {
plugins: ['json'],
rules: {
'json/*': ['error', {allowComments: true}]
}
}
'json/*': ['error', {allowComments: true}],
},
},
};

module.exports = {rules, configs, processors};
42 changes: 21 additions & 21 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {expect} = require('chai');
const _ = require('lodash/fp');

const SCOPE = 'self'; // (for test purpose only, relying the the eslint-plugin-self for tests)
const scoped = rule => `${SCOPE}/${rule}`;
const scoped = (rule) => `${SCOPE}/${rule}`;

function getLintResults(filename, eslintConfig) {
try {
Expand All @@ -13,7 +13,7 @@ function getLintResults(filename, eslintConfig) {
{
encoding: 'utf8',
stdio: 'pipe',
cwd: __dirname
cwd: __dirname,
}
);
return JSON.parse(results)[0];
Expand Down Expand Up @@ -66,58 +66,58 @@ function validateFile(filename, expectations = {}) {
);
}

describe('Integrations tests', function() {
it('validate correct json', function() {
describe('Integrations tests', function () {
it('validate correct json', function () {
validateFile('good-json', {errorCount: 0, warningCount: 0});
});
it('detect duplicate keys', function() {
it('detect duplicate keys', function () {
validateFile('duplicate-keys', {
errors: ['duplicate-key:2']
errors: ['duplicate-key:2'],
}); // FIXME: give error count!
});
it('handle comments in json', function() {
it('handle comments in json', function () {
validateFile('json-with-comments', {errorCount: 0, warningCount: 0});
});
it('detect wrong syntax', function() {
it('detect wrong syntax', function () {
validateFile('wrong-syntax', {errorCount: 1, warningCount: 0});
});
it('detect many infringements in messy json', function() {
it('detect many infringements in messy json', function () {
validateFile('whole-mess', {
errors: ['duplicate-key:2', 'trailing-comma'],
warnings: ['*']
warnings: ['*'],
});
});
});

describe('Integrations tests with config', function() {
describe('recommended', function() {
it('detect many infringements in messy json', function() {
describe('Integrations tests with config', function () {
describe('recommended', function () {
it('detect many infringements in messy json', function () {
validateFile('whole-mess', {
eslintrc: '.eslintrc.with-recommended-config.json',
errors: ['*:4']
errors: ['*:4'],
});
});

it('handle comments in json', function() {
it('handle comments in json', function () {
validateFile('json-with-comments', {
eslintrc: '.eslintrc.with-recommended-config.json',
errorCount: 1 // comment-not-permitted under the '*' glob
errorCount: 1, // comment-not-permitted under the '*' glob
});
});
});
describe('recommended-with-comments', function() {
it('detect many infringements in messy json', function() {
describe('recommended-with-comments', function () {
it('detect many infringements in messy json', function () {
validateFile('whole-mess', {
eslintrc: '.eslintrc.with-recommended-comments-config.json',
errors: ['*:3']
errors: ['*:3'],
});
});

it('handle comments in json', function() {
it('handle comments in json', function () {
validateFile('json-with-comments', {
eslintrc: '.eslintrc.with-recommended-comments-config.json',
errorCount: 0,
warningCount: 0
warningCount: 0,
});
});
});
Expand Down
Loading

0 comments on commit a720fd2

Please sign in to comment.