Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Strict order of rules in README.md (#817)
Browse files Browse the repository at this point in the history
* 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
IllusionMH authored and Josh Goldberg committed Feb 10, 2019
1 parent 592e864 commit f70d669
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 45 deletions.
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ You can create a new rule from a template with the `create-rule` script:
npm run create-rule
```

This will prompt you to enter the details of the new rule. Once you're done, it will create a file for the rule implementation (inside `src`) as well as folder with rule tests (inside `test`).
This will prompt you to enter the details of the new rule. Once you're done, it will create a file for the rule implementation (inside `src`), folder with rule tests (inside `test`), and will add rule info to `README.md`.

You should update rule entry in `README.md` with information about available options, more detailed description, and examples if needed. New rules should have `@next` placeholder in version column, which will be replaced with proper version during release process.

More information about writing rule tests can be found in [TSLint documentation](https://palantir.github.io/tslint/develop/testing-rules/).

Expand Down
84 changes: 42 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,6 @@ We recommend you specify exact versions of lint libraries, including `tslint-mic
</td>
<td>2.0.5</td>
</tr>
<tr>
<td>
<code>react-a11y-accessible-headings</code>
</td>
<td>
For accessibility of your website, there should be no more than 2 H1 heading elements, HTML heading elements must be concise, used for structuring information on the page and non-empty.
</td>
<td>6.0.0</td>
</tr>
<tr>
<td>
<code>react-a11y-iframes</code>
</td>
<td>
Enforce that iframe elements are not empty, have title, and are unique.
</td>
<td>6.1.0</td>
</tr>
<tr>
<td>
<code>informative-docs</code>
Expand Down Expand Up @@ -731,15 +713,6 @@ We recommend you specify exact versions of lint libraries, including `tslint-mic
</td>
<td>4.0.2</td>
</tr>
<tr>
<td>
<code>void-zero</code>
</td>
<td>
<code>void 0</code>, which resolves to <code>undefined</code>, can be confusing to newcomers. Exclusively use <code>undefined</code> to reduce ambiguity.
</td>
<td>6.1.0</td>
</tr>
<tr>
<td>
<code>no-var-self</code>
Expand Down Expand Up @@ -839,6 +812,15 @@ We recommend you specify exact versions of lint libraries, including `tslint-mic
</td>
<td>1.0</td>
</tr>
<tr>
<td>
<code>react-a11y-accessible-headings</code>
</td>
<td>
For accessibility of your website, there should be no more than 2 H1 heading elements, HTML heading elements must be concise, used for structuring information on the page and non-empty.
</td>
<td>6.0.0</td>
</tr>
<tr>
<td>
<code>react-a11y-anchors</code>
Expand Down Expand Up @@ -886,6 +868,15 @@ We recommend you specify exact versions of lint libraries, including `tslint-mic
</td>
<td>2.0.11</td>
</tr>
<tr>
<td>
<code>react-a11y-iframes</code>
</td>
<td>
Enforce that iframe elements are not empty, have title, and are unique.
</td>
<td>6.1.0</td>
</tr>
<tr>
<td>
<code>react-a11y-image-button-has-alt</code>
Expand Down Expand Up @@ -1004,6 +995,21 @@ We recommend you specify exact versions of lint libraries, including `tslint-mic
</td>
<td>6.0.0-beta</td>
</tr>
<tr>
<td>
<code>react-a11y-role</code>
</td>
<td>
For accessibility of your website, elements with aria roles must use a **valid**, **non-abstract** aria role.
A reference to role definitions can be found at [WAI-ARIA roles](https://www.w3.org/TR/wai-aria/roles#role_definitions).
<br />
References:
<ul>
<li><a href="http://oaa-accessibility.org/wcag20/rule/92">WCAG Rule 92: Role value must be valid</a></li>
</ul>
</td>
<td>2.0.11</td>
</tr>
<tr>
<td>
<code>react-a11y-role-has-required-aria-props</code>
Expand Down Expand Up @@ -1039,21 +1045,6 @@ We recommend you specify exact versions of lint libraries, including `tslint-mic
</td>
<td>2.0.11</td>
</tr>
<tr>
<td>
<code>react-a11y-role</code>
</td>
<td>
For accessibility of your website, elements with aria roles must use a **valid**, **non-abstract** aria role.
A reference to role definitions can be found at [WAI-ARIA roles](https://www.w3.org/TR/wai-aria/roles#role_definitions).
<br />
References:
<ul>
<li><a href="http://oaa-accessibility.org/wcag20/rule/92">WCAG Rule 92: Role value must be valid</a></li>
</ul>
</td>
<td>2.0.11</td>
</tr>
<tr>
<td>
<code>react-a11y-tabindex-no-positive</code>
Expand Down Expand Up @@ -1244,6 +1235,15 @@ We recommend you specify exact versions of lint libraries, including `tslint-mic
</td>
<td>1.0</td>
</tr>
<tr>
<td>
<code>void-zero</code>
</td>
<td>
<code>void 0</code>, which resolves to <code>undefined</code>, can be confusing to newcomers. Exclusively use <code>undefined</code> to reduce ambiguity.
</td>
<td>6.1.0</td>
</tr>
</tbody>
</table>

Expand Down
50 changes: 50 additions & 0 deletions build-tasks/common/readme-rules.js
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
};
13 changes: 11 additions & 2 deletions build-tasks/create-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const fs = require('fs');
const inquirer = require('inquirer');
const path = require('path');
const { writeFile } = require('./common/files');
const readmeTemplate = require('./templates/readme-rule-entry.template');
const { addNewRule } = require('./common/readme-rules');

const questions = [
{
Expand Down Expand Up @@ -88,13 +90,15 @@ const questions = [
inquirer.prompt(questions).then(answers => {
const sourceFileName = createImplementationFile(answers);
const testFileNames = createTestFiles(answers);
const readmePosition = createReadmeEntry(answers);

console.log(`Rule '${answers.name}' created.`);
console.log(`Source file: ${sourceFileName}`);
console.log(`Test files: ${testFileNames.join(', ')}`);
console.log(`Test files: ${testFileNames.join(' ')}`);
console.log(`README.md entry: ${readmePosition}`);

// Attempt to open the files in the current editor.
tryOpenFiles([...testFileNames, sourceFileName]);
tryOpenFiles([...testFileNames, sourceFileName, readmePosition]);
});

function createImplementationFile(answers) {
Expand Down Expand Up @@ -139,6 +143,11 @@ function createTestFiles(answers) {
return testFiles;
}

function createReadmeEntry(answers) {
const content = readmeTemplate(answers);
return addNewRule(answers.name, content);
}

function camelCase(input) {
return input.toLowerCase().replace(/-(.)/g, (match, group1) => group1.toUpperCase());
}
Expand Down
8 changes: 8 additions & 0 deletions build-tasks/templates/readme-rule-entry.template.js
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>`;
18 changes: 18 additions & 0 deletions build-tasks/validate-rules-order.js
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);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"tslint:check": "tslint-config-prettier-check ./tslint.json",
"validate:config": "node build-tasks/validate-config.js",
"validate:documentation": "node build-tasks/validate-documentation.js",
"validate:rules-order": "node build-tasks/validate-rules-order.js",
"watch:run-tests": "node build-tasks/watch-run-tests.js",
"watch:src": "tsc --watch"
},
Expand Down

0 comments on commit f70d669

Please sign in to comment.