This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strict order of rules in README.md (#817)
* Validate order of rules in README.md (fixes #816) * Fix rules order in README.md * create-rule task also updates README (fixed #816) * Update CONTRIBUTING.md with detais about README.md actions (fixes #648)
- Loading branch information
1 parent
592e864
commit f70d669
Showing
7 changed files
with
133 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const { readFile, writeFile } = require('./files'); | ||
|
||
// Matches <tr>, <td>, and <code> openning tags with only whitespace surrounding them | ||
// Capturing group for rule name inside <code> and </code> tags with possible whitespace around it | ||
const praseRe = /\s+<tr>\s*?<td>\s*?<code>\s*?([0-9a-z-]*?)\s*?<\/code>/g; | ||
const README_NAME = 'README.md'; | ||
const readmeContent = readFile(README_NAME); | ||
|
||
function getAllRules() { | ||
const rules = []; | ||
let match; | ||
|
||
while ((match = praseRe.exec(readmeContent))) { | ||
rules.push(match[1]); | ||
} | ||
|
||
return rules; | ||
} | ||
|
||
function addNewRule(name, ruleMarkup) { | ||
let match; | ||
let insertPosition; | ||
|
||
while ((match = praseRe.exec(readmeContent))) { | ||
const existingName = match[1]; | ||
// Looks for first rule that should follow new rule. | ||
// Match position will be used to insert new rule row | ||
if (name < existingName) { | ||
insertPosition = match.index; | ||
break; | ||
} | ||
} | ||
|
||
// In case when new rule should be added to the end of the table - look for insret position before </tbody> | ||
if (insertPosition === undefined) { | ||
insertPosition = readmeContent.match(/\s+<\/tbody>\s+<\/table>/).index; | ||
} | ||
const contentBeforeNewRule = readmeContent.slice(0, insertPosition); | ||
const contentAfterNewRule = readmeContent.slice(insertPosition); | ||
const newContent = contentBeforeNewRule + ruleMarkup + contentAfterNewRule; | ||
writeFile(README_NAME, newContent); | ||
|
||
// To position cursor on the same line with new rule name should add 3 lines to match lines added with template | ||
return `./${README_NAME}:${contentBeforeNewRule.split('\n').length + 3}`; | ||
} | ||
|
||
module.exports = { | ||
getAllRules, | ||
addNewRule | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = ({ name, description }) => ` | ||
<tr> | ||
<td> | ||
<code>${name}</code> | ||
</td> | ||
<td>${description}</td> | ||
<td>@next</td> | ||
</tr>`; |
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,18 @@ | ||
const { yellowBright } = require('chalk'); | ||
const { getAllRules } = require('./common/readme-rules'); | ||
|
||
const rules = getAllRules(); | ||
const sortedRules = [].concat(rules).sort(); | ||
const mismatchIndex = rules.findIndex((name, i) => name !== sortedRules[i]); | ||
|
||
if (mismatchIndex === -1) { | ||
process.exit(0); | ||
} | ||
|
||
const message = | ||
mismatchIndex === 0 | ||
? `First rule expecteded to be ${sortedRules[0]} but found ${rules[0]}.` | ||
: `Rule ${rules[mismatchIndex - 1]} should be followed by ${sortedRules[mismatchIndex]} but found ${rules[mismatchIndex]}.`; | ||
console.log(yellowBright('Incorrect order of rules in README.md.')); | ||
console.log(message); | ||
process.exit(1); |
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