Skip to content

Commit

Permalink
Merge pull request #26 from sanctuary-js/avaq/doctest-module-type
Browse files Browse the repository at this point in the history
Add 'module-type' option determining how README is linted
  • Loading branch information
Avaq committed Apr 4, 2019
2 parents 2f409b0 + 39dcbe3 commit 52c4da1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Many variables have default values and are therefore optional.
| `comment-prefix` | `.` | The character which follows `//` to signify documentation to transcribe. |
| `opening-delimiter` | `` ```javascript `` | The opening delimiter of doctest blocks in the source files. |
| `closing-delimiter` | `` ``` `` | The closing delimiter of doctest blocks in the source files. |
| `module-type` | `commonjs` | The module system doctest should use (`amd`, `commonjs`, or `esm`). |
| `version-tag-prefix` | `v` | The prefix of annotated version tags (`version-tag-prefix =` for no prefix). |

### Custom scripts
Expand Down Expand Up @@ -122,7 +123,7 @@ Runs [`doctest`↗︎][] with suitable `--module`, `--prefix`,
`--opening-delimiter`, and `--closing-delimiter` values.

Configurable via [variables][] (`source-files`, `comment-prefix`,
`opening-delimiter`, `closing-delimiter`).
`opening-delimiter`, `closing-delimiter`, `module-type`).

### `generate-readme`

Expand Down Expand Up @@ -190,7 +191,8 @@ undefined link references or unused link definitions.
Uses [`eslint`↗︎][] and [`eslint-plugin-markdown`↗︎][] to assert that the readme,
when built, will not contain examples which violate the project's style guide.

Configurable via [variables][] (`opening-delimiter`, `closing-delimiter`).
Configurable via [variables][] (`opening-delimiter`, `closing-delimiter`
`module-type`).

### `prepublish`

Expand Down
8 changes: 7 additions & 1 deletion bin/doctest
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ set -f ; shopt -u nullglob
prefix="$(get comment-prefix)"
opening="$(get opening-delimiter)"
closing="$(get closing-delimiter)"
module="$(get module-type)"

if [[ "$module" == esm ]] && [[ "$(node_major_version)" -lt 9 ]] ; then
echo 'Skipping ESM doctests on Node.js version less than v9.0.0' >&2
exit 0
fi

node_modules/.bin/doctest \
--module commonjs \
--module "$module" \
--prefix "$prefix" \
--opening-delimiter "$opening" \
--closing-delimiter "$closing" \
Expand Down
11 changes: 10 additions & 1 deletion bin/lint-readme
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fi

opening="$(get opening-delimiter)"
closing="$(get closing-delimiter)"
module="$(get module-type)"

# https://github.com/davidchambers/doctest/blob/0.16.0/lib/doctest.js#L173-L209
# displays the algorithm upon which this function is based. Differences:
Expand Down Expand Up @@ -68,4 +69,12 @@ node_modules/.bin/remark \
cp README.md README.md.temp
rewrite <README.md.temp >README.md

node_modules/.bin/eslint -- README.md
if [[ $module == esm ]] ; then
source=module
else
source=script
fi

node_modules/.bin/eslint \
--parser-options "sourceType:$source" \
-- README.md
5 changes: 5 additions & 0 deletions functions
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ get() {
printf '```javascript' ;;
closing-delimiter) # shellcheck disable=SC2016
printf '```' ;;
module-type) printf 'commonjs' ;;
version-tag-prefix) printf 'v' ;;
*)
echo "'$1' not defined in $config" >&2
Expand Down Expand Up @@ -71,3 +72,7 @@ pass() {
fail() {
echo $'\x1B[0;31m\xE2\x9C\x98\x1B[0m' "$@"
}

node_major_version() {
node --print process.versions.node | cut -d . -f 1
}

0 comments on commit 52c4da1

Please sign in to comment.