Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Handle settings property in readmeAssertions.js script (#277)
* docs: Fix eslint complaints in readmeAssertions.js script * docs: Handle settings property in readmeAssertions.js script
- Loading branch information
Showing
2 changed files
with
54 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,88 @@ | ||
/** | ||
* This script is used to inline assertions into the README.md documents. | ||
*/ | ||
import _ from 'lodash'; | ||
import glob from 'glob'; | ||
|
||
import path from 'path'; | ||
import fs from 'fs'; | ||
import glob from 'glob'; | ||
import _ from 'lodash'; | ||
|
||
const formatCodeSnippet = (setup) => { | ||
const paragraphs = []; | ||
const paragraphs = []; | ||
|
||
if (setup.options) { | ||
paragraphs.push('// Options: ' + JSON.stringify(setup.options)); | ||
} | ||
if (setup.options) { | ||
paragraphs.push('// Options: ' + JSON.stringify(setup.options)); | ||
} | ||
|
||
paragraphs.push(setup.code); | ||
if (setup.settings) { | ||
paragraphs.push('// Settings: ' + JSON.stringify(setup.settings)); | ||
} | ||
|
||
if (setup.errors) { | ||
setup.errors.forEach((message) => { | ||
paragraphs.push('// Message: ' + message.message); | ||
}); | ||
} | ||
paragraphs.push(setup.code); | ||
|
||
if (setup.rules) { | ||
paragraphs.push('// Additional rules: ' + JSON.stringify(setup.rules)); | ||
} | ||
if (setup.errors) { | ||
setup.errors.forEach((message) => { | ||
paragraphs.push('// Message: ' + message.message); | ||
}); | ||
} | ||
|
||
return paragraphs.join('\n'); | ||
if (setup.rules) { | ||
paragraphs.push('// Additional rules: ' + JSON.stringify(setup.rules)); | ||
} | ||
|
||
return paragraphs.join('\n'); | ||
}; | ||
|
||
const getAssertions = () => { | ||
const assertionFiles = glob.sync(path.resolve(__dirname, './../tests/rules/assertions/*.js')); | ||
const assertionFiles = glob.sync(path.resolve(__dirname, './../tests/rules/assertions/*.js')); | ||
|
||
const assertionNames = _.map(assertionFiles, (filePath) => { | ||
return path.basename(filePath, '.js'); | ||
}); | ||
const assertionNames = _.map(assertionFiles, (filePath) => { | ||
return path.basename(filePath, '.js'); | ||
}); | ||
|
||
const assertionCodes = _.map(assertionFiles, (filePath) => { | ||
const codes = require(filePath); | ||
const assertionCodes = _.map(assertionFiles, (filePath) => { | ||
const codes = require(filePath); // eslint-disable-line global-require | ||
|
||
return { | ||
valid: _.map(codes.valid, formatCodeSnippet), | ||
invalid: _.map(codes.invalid, formatCodeSnippet) | ||
}; | ||
}); | ||
return { | ||
invalid: _.map(codes.invalid, formatCodeSnippet), | ||
valid: _.map(codes.valid, formatCodeSnippet) | ||
}; | ||
}); | ||
|
||
return _.zipObject(assertionNames, assertionCodes); | ||
return _.zipObject(assertionNames, assertionCodes); | ||
}; | ||
|
||
const updateDocuments = (assertions) => { | ||
const readmeDocumentPath = path.join(__dirname, './../README.md'); | ||
|
||
let documentBody = fs.readFileSync(readmeDocumentPath, 'utf8'); | ||
const readmeDocumentPath = path.join(__dirname, './../README.md'); | ||
let documentBody; | ||
|
||
documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/ig, (assertionsBlock) => { | ||
let exampleBody; | ||
documentBody = fs.readFileSync(readmeDocumentPath, 'utf8'); | ||
|
||
const ruleName = assertionsBlock.match(/assertions ([a-z]+)/i)[1]; | ||
documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/ig, (assertionsBlock) => { | ||
let exampleBody; | ||
|
||
const ruleAssertions = assertions[ruleName]; | ||
const ruleName = assertionsBlock.match(/assertions ([a-z]+)/i)[1]; | ||
|
||
if (!ruleAssertions) { | ||
throw new Error('No assertions available for rule "' + ruleName + '".'); | ||
const ruleAssertions = assertions[ruleName]; | ||
|
||
return assertionsBlock; | ||
} | ||
if (!ruleAssertions) { | ||
throw new Error('No assertions available for rule "' + ruleName + '".'); | ||
} | ||
|
||
exampleBody = ''; | ||
exampleBody = ''; | ||
|
||
if (ruleAssertions.invalid.length) { | ||
exampleBody += 'The following patterns are considered problems:\n\n```js\n' + ruleAssertions.invalid.join('\n\n') + '\n```\n\n'; | ||
} | ||
if (ruleAssertions.invalid.length) { | ||
exampleBody += 'The following patterns are considered problems:\n\n```js\n' + ruleAssertions.invalid.join('\n\n') + '\n```\n\n'; | ||
} | ||
|
||
if (ruleAssertions.valid.length) { | ||
exampleBody += 'The following patterns are not considered problems:\n\n```js\n' + ruleAssertions.valid.join('\n\n') + '\n```\n\n'; | ||
} | ||
if (ruleAssertions.valid.length) { | ||
exampleBody += 'The following patterns are not considered problems:\n\n```js\n' + ruleAssertions.valid.join('\n\n') + '\n```\n\n'; | ||
} | ||
|
||
return exampleBody; | ||
}); | ||
return exampleBody; | ||
}); | ||
|
||
fs.writeFileSync(readmeDocumentPath, documentBody); | ||
fs.writeFileSync(readmeDocumentPath, documentBody); | ||
}; | ||
|
||
updateDocuments(getAssertions()); |
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