-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Linter: New Rule (DPW005) for Code Samples
- Loading branch information
1 parent
4df9ec8
commit 379a278
Showing
3 changed files
with
61 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @fileOverview Custom rule for code sample validation. | ||
* | ||
* This rule expects MD files in the `docs` dir, and code samples files | ||
* present as sibling directory of `docs` dir. The struct is is: | ||
* -- project | ||
* -- docs | ||
* -- code-samples | ||
* -- mdx_includes | ||
* | ||
* The pattern is `/{!>\s*(.+?)\s*(\[.+])?\s*!}/g` which matches: | ||
* - {!>code-samples/messaging/code-snippets/send-a2p-sms.rb!} | ||
* - {!> code-samples/messaging/code-snippets/send-a2p-sms.rb !} | ||
* - {!> code-samples/messaging/code-snippets/send-a2p-sms.py !} | ||
* - {!> code-samples/messaging/code-snippets/send-a2p-sms.php [ln:2-]!} | ||
* - {!> mdx_includes/rcv-sdk-quick-start-js.md !} | ||
*/ | ||
const { pathToFileURL } = require("url"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const { addError } = require("../helpers"); | ||
|
||
module.exports = { | ||
names: ["DPW005"], | ||
description: "Code samples validation", | ||
tags: ["custom"], | ||
function: function rule(params, onError) { | ||
const pattern = /{!>\s*(.+?)\s*(\[.+])?\s*!}/g; | ||
const rootPath = params.name.split("/docs/")[0]; | ||
|
||
params.tokens | ||
.filter((token) => { | ||
return ( | ||
token.type === "inline" || | ||
token.type === "fence" || | ||
token.type === "code_block" | ||
); | ||
}) | ||
.forEach((token) => { | ||
let match; | ||
|
||
while ((match = pattern.exec(token.content)) !== null) { | ||
const fileName = match[1]; | ||
|
||
if (!fileName) { | ||
addError(onError, token.lineNumber, `Code sample is empty`); | ||
} else { | ||
const filePath = path.join(rootPath, fileName); | ||
const fileUrl = pathToFileURL(filePath); | ||
|
||
if (!fs.existsSync(fileUrl)) { | ||
const detail = `Not Exist: "${fileName}"`; | ||
addError(onError, token.lineNumber, detail); | ||
} | ||
} | ||
} | ||
}); | ||
}, | ||
}; |
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 |
---|---|---|
|
@@ -14,4 +14,5 @@ module.exports = { | |
"DPW002": true, | ||
"DPW003": true, | ||
"DPW004": true, | ||
"DPW005": true, | ||
}; |