-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for doctesting ESM files
This commit adds a new choice to the 'module' option: 'esm'. When running doctest with Node version 9 or up available, setting 'module' to 'esm' allows for the documentation comments to be embedded in ECMAScript modules, and use 'import' to load dependencies. The approach included in this commit has the following consequences: 1. The CLI transparently switches between ESM and non-ESM enabled based on the Node version running it. This means the CLI is fully backwards compatible. 2. The programmatic version also transparently switches between ESM and non-ESM depending on whether it's loaded via import or via require. 3. The programmatic version has a breaking change, in that its primary function returns a Promise now. Co-Authored-By: David Chambers <dc@davidchambers.me>
- Loading branch information
1 parent
10715cf
commit 3b6d44c
Showing
15 changed files
with
349 additions
and
137 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
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 |
---|---|---|
@@ -1,62 +1,19 @@ | ||
'use strict'; | ||
|
||
var program = require ('commander'); | ||
|
||
var common = require ('./common'); | ||
var program = require ('./program'); | ||
var doctest = require ('..'); | ||
var pkg = require ('../package.json'); | ||
|
||
|
||
program | ||
.version (pkg.version) | ||
.usage ('[options] path/to/js/or/coffee/module') | ||
.option ('-m, --module <type>', | ||
'specify module system ("amd" or "commonjs")') | ||
.option (' --nodejs <options>', | ||
'pass options directly to the "node" binary') | ||
.option (' --prefix <prefix>', | ||
'specify Transcribe-style prefix (e.g. ".")') | ||
.option (' --opening-delimiter <delimiter>', | ||
'specify line preceding doctest block (e.g. "```javascript")') | ||
.option (' --closing-delimiter <delimiter>', | ||
'specify line following doctest block (e.g. "```")') | ||
.option ('-p, --print', | ||
'output the rewritten source without running tests') | ||
.option ('-s, --silent', | ||
'suppress output') | ||
.option ('-t, --type <type>', | ||
'specify file type ("coffee" or "js")') | ||
.parse (process.argv); | ||
|
||
// formatErrors :: Array String -> String | ||
function formatErrors(errors) { | ||
return (errors.map (function(s) { return 'error: ' + s + '\n'; })).join (''); | ||
} | ||
|
||
var errors = []; | ||
if (program.module != null && | ||
program.module !== 'amd' && | ||
program.module !== 'commonjs') { | ||
errors.push ('Invalid module `' + program.module + "'"); | ||
} | ||
if (program.type != null && | ||
program.type !== 'coffee' && | ||
program.type !== 'js') { | ||
errors.push ('Invalid type `' + program.type + "'"); | ||
} | ||
if (errors.length > 0) { | ||
process.stderr.write (formatErrors (errors)); | ||
if (program.module === 'esm') { | ||
process.stderr.write ( | ||
common.formatErrors ([ | ||
'Node.js v' + | ||
process.versions.node + | ||
' does not support ECMAScript modules (supported since v9.0.0)' | ||
]) | ||
); | ||
process.exit (1); | ||
} | ||
|
||
process.exit (program.args.reduce (function(status, path) { | ||
var results; | ||
try { | ||
results = doctest (path, program); | ||
} catch (err) { | ||
process.stderr.write (formatErrors ([err.message])); | ||
process.exit (1); | ||
} | ||
return results.reduce (function(status, tuple) { | ||
return tuple[0] ? status : 1; | ||
}, status); | ||
}, 0)); | ||
common.runDoctests (doctest, program); |
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,5 @@ | ||
import common from './common'; | ||
import program from './program'; | ||
import doctest from '..'; | ||
|
||
common.runDoctests (doctest, program); |
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,38 @@ | ||
'use strict'; | ||
|
||
|
||
// formatErrors :: Array String -> String | ||
exports.formatErrors = function(errors) { | ||
return (errors.map (function(s) { return 'error: ' + s + '\n'; })).join (''); | ||
}; | ||
|
||
// sanitizeFileContents :: String -> String | ||
exports.sanitizeFileContents = function(contents) { | ||
return contents.replace (/\r\n?/g, '\n').replace (/^#!.*/, ''); | ||
}; | ||
|
||
// unlines :: Array String -> String | ||
exports.unlines = function(lines) { | ||
return lines.reduce (function(s, line) { return s + line + '\n'; }, ''); | ||
}; | ||
|
||
exports.runDoctests = function(doctest, program) { | ||
if (program.args.length === 0) { | ||
process.stderr.write (exports.formatErrors ([ | ||
'No files for doctesting provided' | ||
])); | ||
process.exit (1); | ||
} | ||
Promise.all (program.args.map (function(path) { | ||
return (doctest (path, program)).then (function(results) { | ||
return results.reduce (function(status, tuple) { | ||
return tuple[0] ? status : 1; | ||
}, 0); | ||
}); | ||
})).then (function(statuses) { | ||
process.exit (statuses.every (function(s) { return s === 0; }) ? 0 : 1); | ||
}, function(err) { | ||
process.stderr.write (exports.formatErrors ([err.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
Oops, something went wrong.