diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..3c14291 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,3 @@ +coverage/ +example.js +test/fixtures/fail-could-not-parse-package/index.js diff --git a/.remarkignore b/.remarkignore new file mode 100644 index 0000000..ffa2e1f --- /dev/null +++ b/.remarkignore @@ -0,0 +1,2 @@ +example/ +test/ diff --git a/example.js b/example.js index 0c14e33..da796e3 100644 --- a/example.js +++ b/example.js @@ -1,26 +1,36 @@ -// This section is rendered by this module from [example.js][example-js]. +// > This section is rendered by this module from [`example.js`][example-js]. +// > Turtles all the way down. šŸ¢šŸ¢šŸ¢ -// Dependencies: -var fs = require('fs') -var remark = require('remark') -var usage = require('./index.js') // This is changed from `./index.js` to `remark-usage` +// remark-usage-ignore-next 4 - Get the files to show them in our usage example. +var readFile = require('fs').readFileSync +var join = require('path').join +var exampleJs = readFile(join('example', 'example.js'), 'utf8') +var exampleMd = readFile(join('example', 'readme.md'), 'utf8') -// Read and parse `readme.md`: -var readme = fs.readFileSync('readme.md', 'utf-8') -var ast = remark() - .use(usage) - .parse(readme) +// remark-usage-ignore-next - Use async/await so it looks nicer. +;(async function (){ -// Log something with a language flag: -console.log('markdown', remark().stringify(ast.children[2])) +// Say we are making a module that exports just enough Pi (3.14159). +// Weā€™re documenting it with a readme file, [`example/readme.md`][example-md]: +console.log('markdown', exampleMd) -// Or without language: -console.log(remark().stringify(ast.children[3])) +// ā€¦and an example script to document it [`example/example.js`][example-js-2]: +console.log('js', exampleJs) + +// ā€¦If we use `remark-usage`, we can generate the `Usage` section +var path = require('path') +var vfile = require('to-vfile') +var remark = require('remark') +var usage = require('.') + +var file = vfile.readSync({path: 'readme.md', cwd: 'example'}) + +var file = await remark() + .use(usage) + .process(file) -// Log something which is never captured: -function neverCalled() { - console.log('javascript', 'alert("test")') -} +// Now, printing `file` (the newly generated readme) yields: +console.log('markdown', String(file)) -// Log something which isnā€™t captured because itā€™s not a string. -console.log(this) +// remark-usage-ignore-next +}()) diff --git a/example/example.js b/example/example.js new file mode 100644 index 0000000..93d28b6 --- /dev/null +++ b/example/example.js @@ -0,0 +1,5 @@ +// Load dependencies: +var pi = require('.') + +// Logging `pi` yields: +console.log('txt', pi) diff --git a/example/index.js b/example/index.js new file mode 100644 index 0000000..561d1ff --- /dev/null +++ b/example/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.floor(Math.PI * 1e5) / 1e5 diff --git a/example/package.json b/example/package.json new file mode 100644 index 0000000..1e579ed --- /dev/null +++ b/example/package.json @@ -0,0 +1,5 @@ +{ + "private": true, + "name": "pi", + "main": "./index.js" +} diff --git a/example/readme.md b/example/readme.md new file mode 100644 index 0000000..cb1a64b --- /dev/null +++ b/example/readme.md @@ -0,0 +1,9 @@ +# PI + +More than enough šŸ° + +## Usage + +## License + +MIT diff --git a/index.js b/index.js index 2356077..3541ac5 100644 --- a/index.js +++ b/index.js @@ -1,339 +1,3 @@ 'use strict' -var fs = require('fs') -var path = require('path') -var uncached = require('require-uncached') -var heading = require('mdast-util-heading-range') -var trimTrailingLines = require('trim-trailing-lines') -var unquote = require('unquote') -var cept = require('cept') -var unified = require('unified') -var markdown = require('remark-parse') -var resolveFrom = require('resolve-from') - -module.exports = usage - -var exists = fs.existsSync -var read = fs.readFileSync -var write = fs.writeFileSync -var remove = fs.unlinkSync -var resolve = path.resolve - -var processor = unified().use(markdown) - -// List of locations to look for an example. -var examples = [ - 'docs/example.js', - 'doc/example.js', - 'examples', - 'example', - 'example.js' -] - -// Expressions. -var expressionLog = /(console\.log\()(.+)(\);?)/g -var expressionRequire = /(require\()(.+)(\);?)/g -var expressionComment = /^(?:\s*)(?:\/\/)(?:\s*)(.+)/ -var expressionIgnore = /^remark-usage-ignore-next(?:(?:\s+)(\d+))?/ - -// Constants. -var defaultHeading = 'usage' - -// Post-process the example document. -function postprocess(value, logs, options) { - var tokens = [] - var start = 0 - var match - var content - var info - var parameters - var end - var markdown - - expressionLog.lastIndex = 0 - match = expressionLog.exec(value) - - while (match) { - end = expressionLog.lastIndex - - content = value.slice(start, end - match[0].length) - - parameters = match[2].split(/\s*,\s*/) - - info = parameters[0] - info = unquote(info.trim()) - info = logs[info] - - if (info && info.value) { - tokens = tokens.concat(script(content, options), log(info)) - } else { - parameters = parameters.slice(1).join(', ') - - tokens = tokens.concat( - script(content + match[1] + parameters + match[3], options) - ) - } - - start = end - match = expressionLog.exec(value) - } - - if (start < value.length) { - tokens = tokens.concat(script(value.slice(start), options)) - } - - markdown = [] - - tokens.forEach(each) - - markdown = markdown.filter(filter) - - return markdown - - function each(token) { - var prev - var lang - - if (token.type === 'markdown') { - markdown = markdown.concat(parse(token.value)) - } else { - prev = markdown[markdown.length - 1] - lang = 'lang' in token ? token.lang : token.type - - if (prev && prev.type === 'code' && prev.lang === lang) { - prev.value += token.value ? '\n' + token.value : '' - } else { - markdown.push({ - type: 'code', - lang: lang, - value: token.value - }) - } - } - } - - function filter(token) { - if ('value' in token) { - token.value = trimTrailingLines(token.value) - - return token.value !== '' - } - - return true - } -} - -// Update the example section. -function usage(options) { - var settings = {} - var pack - var main - var example - var name - var cwd - var header - - if (!options) { - options = {} - } - - cwd = options.cwd || process.cwd() - - try { - pack = require(resolve(cwd, 'package.json')) - } catch (error) { - pack = {} - } - - name = options.name || pack.name || null - - main = resolve(cwd, options.main || pack.main || 'index.js') - - example = options.example - - if (example) { - example = require.resolve(resolve(cwd, example)) - } else { - examples.some(some) - } - - settings.cwd = cwd - settings.name = name - settings.main = main - settings.example = example - - header = toExpression(options.heading || defaultHeading) - - return transform - - function transform(tree) { - heading(tree, header, runFactory(settings)) - } - - function some(filepath) { - filepath = resolve(cwd, filepath) - - if (exists(filepath)) { - example = require.resolve(filepath) - return true - } - - return false - } -} - -// Construct a transformer based on `options`. -function runFactory(options) { - return run - - // Add an example section based on a valid example JavaScript document to a - // `Usage` section. - function run(start, nodes, end) { - var logs = {} - var example = options.example - var source - var tmp - var stop - - if (!exists(example)) { - throw new Error( - 'Missing example: `' + - example + - '`. Pass an `example` or use a file at: ' + - examples.join(', ') - ) - } - - tmp = example + '-tmp' - - source = preprocess(read(example, 'utf-8')) - - write(tmp, source, 'utf-8') - - // To Do: better tmp file management. - stop = cept(console, 'log', intercept) - - try { - uncached(tmp) - } catch (error) { - error.message = - 'Invalid example `' + - example + - '`. ' + - 'Ensure example is a valid JavaScript file:\n\n' + - error.message - - throw error - } finally { - stop() - - /* istanbul ignore next */ - if (exists(tmp)) { - remove(tmp) - } - } - - // Add Markdown. - return [start].concat(postprocess(source, logs, options), end) - - function intercept(id, lang, value) { - if (!value) { - value = lang - lang = null - } - - if (typeof value === 'string' && typeof id === 'string') { - logs[id] = {id: id, lang: lang, value: value} - } - } - } -} - -// Transform a script into an intermediate nodes, removes the IDs from -// `console.log` calls, and resolves the main `require` call. -function script(source, options) { - var tokens - var lines - var index - var length - var line - var match - - // Make sure the require to the main module is shown as if it was a require - // from `./node_modules`. - // For example, when the example file (`test/example.js`) requires the main - // file (as listed in `test/package.json`, `main: "module.js"`) as `./module`, - // it is replaced with `test`. - source = source.replace(expressionRequire, replace) - - // Transform comments into markdown: - tokens = [] - - lines = source.split('\n') - index = -1 - length = lines.length - - while (++index < length) { - line = lines[index] - match = line.match(expressionComment) - - if (match) { - line = match[1] - match = line.match(expressionIgnore) - - if (match) { - index += (match[1] !== undefined && parseInt(match[1], 10)) || 1 - } else { - tokens.push({type: 'markdown', value: line}) - } - } else { - tokens.push({type: 'javascript', value: line}) - } - } - - return tokens - - function replace($0, $1, $2, $3) { - var filepath = resolveFrom.silent( - path.dirname(options.example), - unquote($2) - ) - var quote - - if (options.main === filepath && options.name) { - // Honour quote style. - quote = $2.charAt(0) - - return $1 + quote + options.name + quote + $3 - } - - return $0 - } -} - -// Preprocess `value` to add IDs to `console.log` invocations. -function preprocess(value) { - var index = 0 - - return value.replace(expressionLog, replace) - - function replace($0, $1, $2, $3) { - index++ - return $1 + '"remark-usage-' + index + '",' + $2 + $3 - } -} - -// Parse Markdown into nodes, without positional information. -function parse(value) { - return processor.parse(value, {position: false}).children -} - -// Transform a log into an mdast `code` node. -function log(info) { - return {type: 'code', lang: info.lang, value: info.value} -} - -// Transform a string into an applicable expression. -function toExpression(value) { - return new RegExp('^(' + value + ')$', 'i') -} +module.exports = require('./lib') diff --git a/lib/generate/config.js b/lib/generate/config.js new file mode 100644 index 0000000..db0c2b1 --- /dev/null +++ b/lib/generate/config.js @@ -0,0 +1,25 @@ +'use strict' + +var nanoid = require('nanoid') +var resolveFrom = require('resolve-from') +var relativeModule = require('../util/relative-module') + +module.exports = config + +function config(ctx) { + var settings = ctx.settings + var pkg = ctx.pkg || {} + var cwd = ctx.file.cwd + var mainRoot = settings.main ? cwd : ctx.pkgRoot || cwd + var mainId = relativeModule(settings.main || pkg.main || '') + var main + + try { + main = resolveFrom(mainRoot, mainId) + } catch (error) {} + + ctx.cwd = cwd + ctx.main = main + ctx.name = settings.name || pkg.name || null + ctx.id = 'remark-usage-example-' + nanoid().toLowerCase() +} diff --git a/lib/generate/find-example.js b/lib/generate/find-example.js new file mode 100644 index 0000000..586f0ea --- /dev/null +++ b/lib/generate/find-example.js @@ -0,0 +1,73 @@ +'use strict' + +var fs = require('fs') +var resolveFrom = require('resolve-from') +var relativeModule = require('../util/relative-module') + +module.exports = findExample + +function findExample(ctx, next) { + var fn = ctx.settings.example ? findExplicitExample : findImplicitExample + + fn(ctx, onexample) + + function onexample(filePath) { + if (filePath) { + fs.readFile(filePath, onfile) + } else { + next(new Error('Could not find example')) + } + + function onfile(err, buf) { + var example + + /* istanbul ignore if - ENOENT is already caught above, so this rarely happens */ + if (err) { + return next(new Error('Could not read example: ' + err)) + } + + example = String(buf) + + // Make sure there is a final line feed. + if (example.charAt(example.length - 1) !== '\n') { + example += '\n' + } + + ctx.examplePath = filePath + ctx.example = example + next() + } + } +} + +function findExplicitExample(ctx, next) { + var cwd = ctx.cwd + var moduleId = relativeModule(ctx.settings.example) + var filePath + + try { + filePath = resolveFrom(cwd, moduleId) + } catch (error) {} + + next(filePath) +} + +function findImplicitExample(ctx, next) { + var cwd = ctx.cwd + var examples = ['./example', './examples', './doc/example', './docs/example'] + var length = examples.length + var index = -1 + var filePath + + while (++index < length) { + try { + filePath = resolveFrom(cwd, examples[index]) + } catch (error) { + continue + } + + return next(filePath) + } + + next() +} diff --git a/lib/generate/find-package.js b/lib/generate/find-package.js new file mode 100644 index 0000000..e3ae9ae --- /dev/null +++ b/lib/generate/find-package.js @@ -0,0 +1,56 @@ +'use strict' + +var fs = require('fs') +var path = require('path') + +var packageName = 'package.json' + +module.exports = findPackage + +function findPackage(ctx, next) { + var file = ctx.file + var base = file.cwd + + if (file.path) { + base = path.dirname(path.resolve(base, file.path)) + } + + read() + + function read() { + fs.readFile(path.join(base, packageName), onread) + } + + function onread(err, buf) { + var parent + var pkg + + if (err) { + /* istanbul ignore if - Doesnā€™t consistently happen. */ + if (err.code !== 'ENOENT') { + return next(new Error('Could not read package: ' + err)) + } + + parent = path.dirname(base) + + // No `package.json`. + if (parent === base) { + return next() + } + + base = parent + return read() + } + + try { + pkg = JSON.parse(buf) + } catch (error) { + // Invalid JSON. + return next(new Error('Could not parse package: ' + error)) + } + + ctx.pkg = pkg + ctx.pkgRoot = base + return next() + } +} diff --git a/lib/generate/generate.js b/lib/generate/generate.js new file mode 100644 index 0000000..afe26f9 --- /dev/null +++ b/lib/generate/generate.js @@ -0,0 +1,120 @@ +'use strict' + +var unified = require('unified') +var markdown = require('remark-parse') + +module.exports = generate + +var processor = unified().use(markdown, {position: false}) + +function generate(ctx) { + var nodes = [] + var tokens = ctx.tokens + var length = tokens.length + var index = -1 + var node + var fn + + while (++index < length) { + node = tokens[index] + fn = node.type === 'comment' ? comment : node.type === 'code' ? code : noop + nodes = nodes.concat(fn(node, ctx) || []) + } + + ctx.nodes = nodes +} + +function noop() {} + +function comment(node) { + return processor.parse(node.values.join('')).children +} + +function code(node, ctx) { + var example = ctx.example + var start = node.start + var end = node.end + var tokens = [] + var token + var consoleCall + var mainReference + var nodes + var length + var index + var lineEnd + var line + + while (start < end) { + lineEnd = example.indexOf('\n', start) + lineEnd = lineEnd === -1 || lineEnd >= end ? end : lineEnd + + consoleCall = findInLine(ctx.logs, start, lineEnd) + + if (consoleCall && consoleCall.values.length !== 0) { + line = example.slice(start, lineEnd) + + if (token && token === consoleCall) { + // Ignore: itā€™s the same multiline console call. + } else { + token = consoleCall + tokens.push(token) + } + } else { + mainReference = ctx.name && findInLine(ctx.mainReferences, start, lineEnd) + + if (mainReference) { + line = + example.slice(start, mainReference.start) + + mainReference.quote + + ctx.name + + mainReference.quote + + example.slice(mainReference.end, lineEnd) + } else { + line = example.slice(start, lineEnd) + } + + if (!token || token.type !== 'code') { + token = {type: 'code', values: []} + tokens.push(token) + } + + token.values.push(line) + } + + start = lineEnd + 1 + } + + nodes = [] + length = tokens.length + index = -1 + + while (++index < length) { + token = tokens[index] + nodes.push({ + type: 'code', + lang: token.type === 'code' ? 'javascript' : token.language, + value: token.values.join('\n').replace(/^\n+|\n+$/g, '') + }) + } + + return nodes +} + +function findInLine(values, start, end) { + var length = values.length + var index = -1 + var reference + + while (++index < length) { + reference = values[index] + + if ( + // Reference in: + (reference.start >= start && reference.end <= end) || + // Line in reference: + (start >= reference.start && end <= reference.end) + ) { + return reference + } + } +} diff --git a/lib/generate/index.js b/lib/generate/index.js new file mode 100644 index 0000000..64dc535 --- /dev/null +++ b/lib/generate/index.js @@ -0,0 +1,21 @@ +'use strict' + +var trough = require('trough') +var config = require('./config') +var findPackage = require('./find-package') +var findExample = require('./find-example') +var generate = require('./generate') +var instrument = require('./instrument') +var run = require('./run') +var tokenize = require('./tokenize') +var write = require('./write') + +module.exports = trough() + .use(findPackage) + .use(config) + .use(findExample) + .use(instrument) + .use(write) + .use(run) + .use(tokenize) + .use(generate) diff --git a/lib/generate/instrument.js b/lib/generate/instrument.js new file mode 100644 index 0000000..319e46b --- /dev/null +++ b/lib/generate/instrument.js @@ -0,0 +1,107 @@ +'use strict' + +var path = require('path') +var babel = require('@babel/core') +var resolveFrom = require('resolve-from') + +module.exports = instrument + +function instrument(ctx, next) { + var logs = [] + var mainReferences = [] + + babel.transform( + ctx.example, + { + plugins: [addIdToConsoleLog], + cwd: ctx.cwd, + filename: ctx.examplePath, + caller: {name: 'remark-usage'}, + sourceType: 'unambiguous' + }, + ontransform + ) + + function ontransform(err, result) { + if (err) { + next(new Error('Could not parse example: ' + err)) + } else { + ctx.exampleInstrumented = result.code + ctx.logs = logs + ctx.mainReferences = mainReferences + next() + } + } + + function addIdToConsoleLog() { + var t = babel.types + var index = -1 + + return { + visitor: { + CallExpression: function(path) { + var callee = path.get('callee') + + if ( + callee.isIdentifier({name: 'require'}) && + path.get('arguments.0').isStringLiteral() + ) { + instrumentMainReference(path.get('arguments.0').node) + } + + if (callee.matchesPattern('console.log')) { + instrumentConsoleLog(path) + } + } + } + } + + function instrumentMainReference(node) { + var raw = node && node.extra && node.extra.raw + var filePath = resolveFrom.silent( + path.dirname(ctx.examplePath), + node.value + ) + + /* istanbul ignore if - Babel always adds raw, but just to be sure. */ + if (!raw) { + raw = "'" + } + + if (filePath && filePath === ctx.main) { + mainReferences.push({ + start: node.start, + end: node.end, + quote: raw.charAt(0) + }) + } + } + + function instrumentConsoleLog(path) { + var node = path.node + var args = node.arguments.concat() + var head + var language + + index++ + + if (args.length > 1 && args[0].type === 'StringLiteral') { + head = args.shift() + language = head.value + } + + logs[index] = { + start: node.start, + end: node.end, + language: language, + values: [] + } + + node.arguments = [].concat( + t.stringLiteral('<' + ctx.id + '-' + index + '>'), + args, + t.stringLiteral('') + ) + } + } +} diff --git a/lib/generate/run.js b/lib/generate/run.js new file mode 100644 index 0000000..f565e13 --- /dev/null +++ b/lib/generate/run.js @@ -0,0 +1,56 @@ +'use strict' + +var exec = require('child_process').exec + +module.exports = run + +function run(ctx, next) { + exec('node ' + ctx.exampleInstrumentedPath, onexec) + + function onexec(err, stdout) { + var logs = ctx.logs + var out + var open + var close + var startMatch + var endMatch + var start + var end + var logIndex + var value + + if (err) { + return next(new Error('Could not run example: ' + err)) + } + + out = stdout + open = new RegExp('<' + ctx.id + '-(\\d+)>', 'g') + close = new RegExp('', 'g') + + while ((startMatch = open.exec(out))) { + close.lastIndex = startMatch.index + endMatch = close.exec(out) + + /* istanbul ignore else - should never occur, console is sync, but just to + * be sure. */ + if (endMatch) { + start = startMatch.index + startMatch[0].length + end = endMatch.index + logIndex = parseInt(startMatch[1], 10) + value = out.slice(start, end) + + /* istanbul ignore else - console adds spaces at start and end, just to + * be sure weā€™re checking it though. */ + if (value.charAt(0) === ' ' && value.charAt(value.length - 1) === ' ') { + value = value.slice(1, -1) + } + + logs[logIndex].values.push(value) + } + + open.lastIndex = end + } + + next() + } +} diff --git a/lib/generate/tokenize.js b/lib/generate/tokenize.js new file mode 100644 index 0000000..45e6ea1 --- /dev/null +++ b/lib/generate/tokenize.js @@ -0,0 +1,72 @@ +'use strict' + +module.exports = tokenize + +function tokenize(ctx) { + var lineBreak = /\r?\n/g + var lineComment = /^\s*\/\/\s*/ + var ignoreComment = /^remark-usage-ignore-next(?:(?:\s+)(\d+))?/ + var example = ctx.example + var start = 0 + var end + var skip = 0 + var match + var line + var comment + var ignore + var tokens = [] + var token + + while ((match = lineBreak.exec(example))) { + end = match.index + line = example.slice(start, end) + + // If the line is supposed to be skipped, skip it. + // Skipping can only happen by starting a comment. + if (skip) { + skip-- + } + // Empty: + else if (line.trim().length === 0) { + if (token) { + if (token.type === 'comment') { + token.values.push(match[0]) + } else if (token.type === 'code') { + token.end = end + } + } + } else { + comment = line.match(lineComment) + + if (comment) { + line = line.slice(comment[0].length) + ignore = line.match(ignoreComment) + + if (ignore) { + // Skip next couple of lines. + skip = (ignore[1] !== undefined && parseInt(ignore[1], 10)) || 1 + token = {type: 'skip', skip: skip} + tokens.push(token) + } else { + if (!token || token.type !== 'comment') { + token = {type: 'comment', values: []} + tokens.push(token) + } + + token.values.push(line, match[0]) + } + } else { + if (!token || token.type !== 'code') { + token = {type: 'code', start: start, end: end} + tokens.push(token) + } + + token.end = end + } + } + + start = end + match[0].length + } + + ctx.tokens = tokens +} diff --git a/lib/generate/write.js b/lib/generate/write.js new file mode 100644 index 0000000..c2c135f --- /dev/null +++ b/lib/generate/write.js @@ -0,0 +1,23 @@ +'use strict' + +var fs = require('fs') +var path = require('path') + +module.exports = write + +function write(ctx, next) { + var dir = path.dirname(ctx.examplePath) + var filePath = path.join(dir, ctx.id + '.js') + + fs.writeFile(filePath, ctx.exampleInstrumented, onwrite) + + function onwrite(err) { + /* istanbul ignore if - Doesnā€™t happen consistently */ + if (err) { + return next(new Error('Could not write example: ' + err)) + } + + ctx.exampleInstrumentedPath = filePath + next() + } +} diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..3190300 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,61 @@ +'use strict' + +var fs = require('fs') +var heading = require('mdast-util-heading-range') +var generate = require('./generate') + +var defaultHeading = 'usage' + +module.exports = usage + +function usage(options) { + var settings = options || {} + var header = new RegExp( + '^(' + (settings.heading || defaultHeading) + ')$', + 'i' + ) + + return transform + + function transform(tree, file, next) { + var ctx = {tree: tree, file: file, settings: settings} + var exists = false + + // Walk the tree once to check if the heading exists. + // Walking the tree may be slow, but itā€™s much more slow to run Babel, spawn + // node, and generate the example. + heading(tree, header, ifExists) + + if (exists) { + generate.run(ctx, done) + } else { + next() + } + + function done(err) { + // If something failed and thereā€™s an example, remove it. + if (ctx && ctx.exampleInstrumentedPath) { + try { + fs.unlinkSync(ctx.exampleInstrumentedPath) + } catch (error) {} + } + + if (err) { + return next(err) + } + + // Add example. + heading(tree, header, run) + next() + + function run(start, _, end) { + return [start].concat(ctx.nodes, end) + } + } + + function ifExists(start, nodes, end) { + exists = true + return [start].concat(nodes, end) + } + } +} diff --git a/lib/util/relative-module.js b/lib/util/relative-module.js new file mode 100644 index 0000000..32f8d82 --- /dev/null +++ b/lib/util/relative-module.js @@ -0,0 +1,9 @@ +'use strict' + +var prefix = './' + +module.exports = relativeModule + +function relativeModule(moduleId) { + return moduleId.slice(0, 2) === prefix ? moduleId : prefix + moduleId +} diff --git a/package.json b/package.json index f9d0e70..89971e0 100644 --- a/package.json +++ b/package.json @@ -24,14 +24,13 @@ "index.js" ], "dependencies": { - "cept": "^1.0.0", + "@babel/core": "^7.0.0", "mdast-util-heading-range": "^2.0.0", + "nanoid": "^2.0.0", "remark-parse": "^6.0.0", - "require-uncached": "^2.0.0", "resolve-from": "^5.0.0", - "trim-trailing-lines": "^1.0.0", - "unified": "^7.0.0", - "unquote": "^1.1.0" + "trough": "^1.0.0", + "unified": "^7.0.0" }, "devDependencies": { "is-hidden": "^1.0.0", @@ -42,10 +41,13 @@ "remark-cli": "^6.0.0", "remark-preset-wooorm": "^5.0.0", "tape": "^4.0.0", + "to-vfile": "^6.0.0", "xo": "^0.24.0" }, "scripts": { - "format": "remark *.md -qfo && prettier --write *.js test/*.js && xo *.js test/*.js --fix", + "format:md": "remark . -qfo", + "format:js": "prettier --no-config --config ./package.json --write \"**/*.js\" && xo --fix", + "format": "npm run format:md && npm run format:js", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test/index.js", "test": "npm run format && npm run test-coverage" @@ -67,17 +69,9 @@ "xo": { "prettier": true, "esnext": false, - "rules": { - "no-unused-expressions": "off", - "no-unused-vars": "off", - "eol-last": "off", - "import/no-dynamic-require": "off", - "import/order": "off", - "unicorn/import-index": "off", - "ava/no-import-test-files": "off" - }, "ignore": [ - "example.js" + "example.js", + "test/fixtures" ] }, "remarkConfig": { @@ -87,7 +81,12 @@ "remark-lint-fenced-code-flag", false ], - "./" + [ + "./", + { + "heading": "use" + } + ] ] } } diff --git a/readme.md b/readme.md index 919fdf1..8a3d326 100644 --- a/readme.md +++ b/readme.md @@ -20,51 +20,75 @@ npm install remark-usage ## Usage -This section is rendered by this module from [example.js][example-js]. +> This section is rendered by this module from [`example.js`][example-js]. +> Turtles all the way down. šŸ¢šŸ¢šŸ¢ -Dependencies: +Say we are making a module that exports just enough Pi (3.14159). +Weā€™re documenting it with a readme file, [`example/readme.md`][example-md]: -```javascript -var fs = require('fs') -var remark = require('remark') -var usage = require('remark-usage') // This is changed from `./index.js` to `remark-usage` +```markdown +# PI + +More than enough šŸ° + +## Use + +## License + +MIT +``` + +ā€¦and an example script to document it [`example/example.js`][example-js-2]: + +```js +// Load dependencies: +var pi = require('.') + +// Logging `pi` yields: +console.log('txt', pi) ``` -Read and parse `readme.md`: +ā€¦If we use `remark-usage`, we can generate the `Usage` section ```javascript -var readme = fs.readFileSync('readme.md', 'utf-8') -var ast = remark() +var path = require('path') +var vfile = require('to-vfile') +var remark = require('remark') +var usage = require('remark-usage') + +var file = vfile.readSync({path: 'readme.md', cwd: 'example'}) + +var file = await remark() .use(usage) - .parse(readme) + .process(file) ``` -Log something with a language flag: +Now, printing `file` (the newly generated readme) yields: -```markdown -[**remark**][remark] plugin to add a [usage][] example to a readme. -``` +````markdown +# PI -Or without language: +More than enough šŸ° -``` -## Install -``` +## Usage -Log something which is never captured: +Load dependencies: ```javascript -function neverCalled() { - console.log('javascript', 'alert("test")') -} +var pi = require('pi') ``` -Log something which isnā€™t captured because itā€™s not a string. +Logging `pi` yields: -```javascript -console.log(this) +```txt +3.14159 ``` +## License + +MIT +```` + ## API @@ -77,7 +101,7 @@ Removes the current content between the heading containing the text ā€œusageā€, the next heading of the same (or higher) depth, and replaces it with the example. -The example is run as JavaScript. +The example is run in Node. Line comments are parsed as Markdown. Calls to `console.log()` are exposed as code blocks, containing the logged values (optionally with a language flag). @@ -85,10 +109,8 @@ values (optionally with a language flag). Itā€™s easiest to check out and compare [`example.js`][example-js] with the above [Usage][] section. -* Operate this from an npm package, or provide a `cwd` +* Operate this from an npm package * Make sure no side effects occur when running `example.js` -* Donā€™t do weird things. - This is mostly regexes You can ignore lines like so: @@ -106,33 +128,30 @@ function sum(a, b) { ##### `options` -###### `options.cwd` - -Path to a directory containing a node module (`string?`). -Used to infer `name`, `main`, and `example`. - -###### `options.name` - -Name of the module (`string?`). -Inferred from `package.json`s `name` property. -Used to rewrite `require('.')` to `require('some-name')`. - -###### `options.main` +###### `options.heading` -Path to the main script (`string?`). -Resolved from `package.json`s `main` property (or `index.js`). -Used to rewrite `require('./index.js')` to `require('some-name')`. +Heading to look for (`string?`, default: `'usage'`). +Wrapped in `new RegExp('^(' + value + ')$', 'i');`. ###### `options.example` Path to the example script (`string?`). -`remark-usage` checks for `docs/example.js`, `doc/example.js`, -`examples/index.js`, `example/index.js`, and `example.js`. +If given, resolved from [`file.cwd`][file-cwd]. +If not given, the following values are attempted and resolved from `file.cwd`: +`'./example'`, `'./examples'`, `'./doc/example'`, `'./docs/example'`. +The first that exists, is used. -###### `options.heading` +###### `options.name` -Heading to look for (`string?`, default: `'usage'`). -Wrapped in `new RegExp('^(' + value + ')$', 'i');`. +Name of the module (`string?`, default: `pkg.name`, optional). +Used to rewrite `require('.')` to `require('name')`. + +###### `options.main` + +Path to the main file (`string?`, default: `pkg.main` or `'.'`, optional). +If given, resolved from [`file.cwd`][file-cwd]. +If inferred from `package.json`, resolved relating to that package root. +Used to rewrite `require('.')` to `require('name')`. ## Contribute @@ -192,6 +211,12 @@ abide by its terms. [remark]: https://github.com/remarkjs/remark +[file-cwd]: https://github.com/vfile/vfile#vfilecwd + [usage]: #usage [example-js]: example.js + +[example-md]: ./example/readme.md + +[example-js-2]: ./example/example.js diff --git a/test/fixtures/code-without-eof/example.js b/test/fixtures/code-without-eof/example.js new file mode 100644 index 0000000..36e3e40 --- /dev/null +++ b/test/fixtures/code-without-eof/example.js @@ -0,0 +1,3 @@ +var pi = require('./index.js') + +pi + pi \ No newline at end of file diff --git a/test/fixtures/code-without-eof/index.js b/test/fixtures/code-without-eof/index.js new file mode 100644 index 0000000..45a6d8c --- /dev/null +++ b/test/fixtures/code-without-eof/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/code-without-eof/output.md b/test/fixtures/code-without-eof/output.md new file mode 100644 index 0000000..eaea538 --- /dev/null +++ b/test/fixtures/code-without-eof/output.md @@ -0,0 +1,9 @@ +# PI + +## Usage + +```javascript +var pi = require('pi') + +pi + pi +``` diff --git a/test/fixtures/fail-invalid-example/package.json b/test/fixtures/code-without-eof/package.json similarity index 89% rename from test/fixtures/fail-invalid-example/package.json rename to test/fixtures/code-without-eof/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/fail-invalid-example/package.json +++ b/test/fixtures/code-without-eof/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/fail-invalid-example/readme.md b/test/fixtures/code-without-eof/readme.md similarity index 100% rename from test/fixtures/fail-invalid-example/readme.md rename to test/fixtures/code-without-eof/readme.md diff --git a/test/fixtures/fail-could-not-find-example/index.js b/test/fixtures/fail-could-not-find-example/index.js new file mode 100644 index 0000000..45a6d8c --- /dev/null +++ b/test/fixtures/fail-could-not-find-example/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/fail-missing-example/package.json b/test/fixtures/fail-could-not-find-example/package.json similarity index 89% rename from test/fixtures/fail-missing-example/package.json rename to test/fixtures/fail-could-not-find-example/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/fail-missing-example/package.json +++ b/test/fixtures/fail-could-not-find-example/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/fail-missing-example/readme.md b/test/fixtures/fail-could-not-find-example/readme.md similarity index 100% rename from test/fixtures/fail-missing-example/readme.md rename to test/fixtures/fail-could-not-find-example/readme.md diff --git a/test/fixtures/fail-could-not-parse-example/example.js b/test/fixtures/fail-could-not-parse-example/example.js new file mode 100644 index 0000000..eef843b --- /dev/null +++ b/test/fixtures/fail-could-not-parse-example/example.js @@ -0,0 +1 @@ +' diff --git a/test/fixtures/fail-could-not-parse-example/index.js b/test/fixtures/fail-could-not-parse-example/index.js new file mode 100644 index 0000000..45a6d8c --- /dev/null +++ b/test/fixtures/fail-could-not-parse-example/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/fail-could-not-parse-example/package.json b/test/fixtures/fail-could-not-parse-example/package.json new file mode 100644 index 0000000..7d35bc7 --- /dev/null +++ b/test/fixtures/fail-could-not-parse-example/package.json @@ -0,0 +1,3 @@ +{ + "name": "pi" +} diff --git a/test/fixtures/fail-could-not-parse-example/readme.md b/test/fixtures/fail-could-not-parse-example/readme.md new file mode 100644 index 0000000..f78b72b --- /dev/null +++ b/test/fixtures/fail-could-not-parse-example/readme.md @@ -0,0 +1,3 @@ +# PI + +## Usage diff --git a/test/fixtures/fail-could-not-parse-package/index.js b/test/fixtures/fail-could-not-parse-package/index.js new file mode 100644 index 0000000..45a6d8c --- /dev/null +++ b/test/fixtures/fail-could-not-parse-package/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/fail-could-not-parse-package/package.json b/test/fixtures/fail-could-not-parse-package/package.json new file mode 100644 index 0000000..98232c6 --- /dev/null +++ b/test/fixtures/fail-could-not-parse-package/package.json @@ -0,0 +1 @@ +{ diff --git a/test/fixtures/fail-could-not-parse-package/readme.md b/test/fixtures/fail-could-not-parse-package/readme.md new file mode 100644 index 0000000..f78b72b --- /dev/null +++ b/test/fixtures/fail-could-not-parse-package/readme.md @@ -0,0 +1,3 @@ +# PI + +## Usage diff --git a/test/fixtures/fail-invalid-example/example.js b/test/fixtures/fail-could-not-run-example/example.js similarity index 100% rename from test/fixtures/fail-invalid-example/example.js rename to test/fixtures/fail-could-not-run-example/example.js diff --git a/test/fixtures/fail-could-not-run-example/index.js b/test/fixtures/fail-could-not-run-example/index.js new file mode 100644 index 0000000..45a6d8c --- /dev/null +++ b/test/fixtures/fail-could-not-run-example/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/fail-could-not-run-example/package.json b/test/fixtures/fail-could-not-run-example/package.json new file mode 100644 index 0000000..7d35bc7 --- /dev/null +++ b/test/fixtures/fail-could-not-run-example/package.json @@ -0,0 +1,3 @@ +{ + "name": "pi" +} diff --git a/test/fixtures/fail-could-not-run-example/readme.md b/test/fixtures/fail-could-not-run-example/readme.md new file mode 100644 index 0000000..f78b72b --- /dev/null +++ b/test/fixtures/fail-could-not-run-example/readme.md @@ -0,0 +1,3 @@ +# PI + +## Usage diff --git a/test/fixtures/fail-invalid-example/index.js b/test/fixtures/fail-invalid-example/index.js deleted file mode 100644 index 2cc299c..0000000 --- a/test/fixtures/fail-invalid-example/index.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict'; - -module.exports = Math.PI.toString().slice(0, 6); diff --git a/test/fixtures/fail-missing-example/index.js b/test/fixtures/fail-missing-example/index.js deleted file mode 100644 index 2cc299c..0000000 --- a/test/fixtures/fail-missing-example/index.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict'; - -module.exports = Math.PI.toString().slice(0, 6); diff --git a/test/fixtures/file-without-file-path/config.json b/test/fixtures/file-without-file-path/config.json new file mode 100644 index 0000000..84ec767 --- /dev/null +++ b/test/fixtures/file-without-file-path/config.json @@ -0,0 +1,3 @@ +{ + "withoutFilePath": true +} diff --git a/test/fixtures/file-without-file-path/example.js b/test/fixtures/file-without-file-path/example.js new file mode 100644 index 0000000..ccad136 --- /dev/null +++ b/test/fixtures/file-without-file-path/example.js @@ -0,0 +1,4 @@ +var pi = require('./index.js') + +// Logs: +console.log('text', pi) diff --git a/test/fixtures/file-without-file-path/index.js b/test/fixtures/file-without-file-path/index.js new file mode 100644 index 0000000..45a6d8c --- /dev/null +++ b/test/fixtures/file-without-file-path/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/file-without-file-path/output.md b/test/fixtures/file-without-file-path/output.md new file mode 100644 index 0000000..644ddb2 --- /dev/null +++ b/test/fixtures/file-without-file-path/output.md @@ -0,0 +1,13 @@ +# PI + +## Usage + +```javascript +var pi = require('pi') +``` + +Logs: + +```text +3.1415 +``` diff --git a/test/fixtures/file-without-file-path/package.json b/test/fixtures/file-without-file-path/package.json new file mode 100644 index 0000000..7d35bc7 --- /dev/null +++ b/test/fixtures/file-without-file-path/package.json @@ -0,0 +1,3 @@ +{ + "name": "pi" +} diff --git a/test/fixtures/file-without-file-path/readme.md b/test/fixtures/file-without-file-path/readme.md new file mode 100644 index 0000000..f78b72b --- /dev/null +++ b/test/fixtures/file-without-file-path/readme.md @@ -0,0 +1,3 @@ +# PI + +## Usage diff --git a/test/fixtures/ignore-lines/index.js b/test/fixtures/ignore-lines/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/ignore-lines/index.js +++ b/test/fixtures/ignore-lines/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/image-with-alt/example.js b/test/fixtures/image-with-alt/example.js index 78a8167..ff50019 100644 --- a/test/fixtures/image-with-alt/example.js +++ b/test/fixtures/image-with-alt/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./index.js'); +var pi = require('./index.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/image-with-alt/index.js b/test/fixtures/image-with-alt/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/image-with-alt/index.js +++ b/test/fixtures/image-with-alt/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/image-with-alt/output.md b/test/fixtures/image-with-alt/output.md index e6f9a90..7e7fdd8 100644 --- a/test/fixtures/image-with-alt/output.md +++ b/test/fixtures/image-with-alt/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/image-with-alt/package.json b/test/fixtures/image-with-alt/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/image-with-alt/package.json +++ b/test/fixtures/image-with-alt/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/initial-code/example.js b/test/fixtures/initial-code/example.js new file mode 100644 index 0000000..ccad136 --- /dev/null +++ b/test/fixtures/initial-code/example.js @@ -0,0 +1,4 @@ +var pi = require('./index.js') + +// Logs: +console.log('text', pi) diff --git a/test/fixtures/initial-code/index.js b/test/fixtures/initial-code/index.js new file mode 100644 index 0000000..45a6d8c --- /dev/null +++ b/test/fixtures/initial-code/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/initial-code/output.md b/test/fixtures/initial-code/output.md new file mode 100644 index 0000000..644ddb2 --- /dev/null +++ b/test/fixtures/initial-code/output.md @@ -0,0 +1,13 @@ +# PI + +## Usage + +```javascript +var pi = require('pi') +``` + +Logs: + +```text +3.1415 +``` diff --git a/test/fixtures/initial-code/package.json b/test/fixtures/initial-code/package.json new file mode 100644 index 0000000..7d35bc7 --- /dev/null +++ b/test/fixtures/initial-code/package.json @@ -0,0 +1,3 @@ +{ + "name": "pi" +} diff --git a/test/fixtures/initial-code/readme.md b/test/fixtures/initial-code/readme.md new file mode 100644 index 0000000..f78b72b --- /dev/null +++ b/test/fixtures/initial-code/readme.md @@ -0,0 +1,3 @@ +# PI + +## Usage diff --git a/test/fixtures/initial-whitespace/example.js b/test/fixtures/initial-whitespace/example.js new file mode 100644 index 0000000..6b6f132 --- /dev/null +++ b/test/fixtures/initial-whitespace/example.js @@ -0,0 +1,7 @@ + + +// Require `pi`: +var pi = require('./index.js') + +// Logs: +console.log('text', pi) diff --git a/test/fixtures/initial-whitespace/index.js b/test/fixtures/initial-whitespace/index.js new file mode 100644 index 0000000..45a6d8c --- /dev/null +++ b/test/fixtures/initial-whitespace/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/initial-whitespace/output.md b/test/fixtures/initial-whitespace/output.md new file mode 100644 index 0000000..62885c5 --- /dev/null +++ b/test/fixtures/initial-whitespace/output.md @@ -0,0 +1,15 @@ +# PI + +## Usage + +Require `pi`: + +```javascript +var pi = require('pi') +``` + +Logs: + +```text +3.1415 +``` diff --git a/test/fixtures/initial-whitespace/package.json b/test/fixtures/initial-whitespace/package.json new file mode 100644 index 0000000..7d35bc7 --- /dev/null +++ b/test/fixtures/initial-whitespace/package.json @@ -0,0 +1,3 @@ +{ + "name": "pi" +} diff --git a/test/fixtures/initial-whitespace/readme.md b/test/fixtures/initial-whitespace/readme.md new file mode 100644 index 0000000..f78b72b --- /dev/null +++ b/test/fixtures/initial-whitespace/readme.md @@ -0,0 +1,3 @@ +# PI + +## Usage diff --git a/test/fixtures/link-with-title/example.js b/test/fixtures/link-with-title/example.js index 78a8167..ff50019 100644 --- a/test/fixtures/link-with-title/example.js +++ b/test/fixtures/link-with-title/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./index.js'); +var pi = require('./index.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/link-with-title/index.js b/test/fixtures/link-with-title/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/link-with-title/index.js +++ b/test/fixtures/link-with-title/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/link-with-title/output.md b/test/fixtures/link-with-title/output.md index 4fa9381..6d518bc 100644 --- a/test/fixtures/link-with-title/output.md +++ b/test/fixtures/link-with-title/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/link-with-title/package.json b/test/fixtures/link-with-title/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/link-with-title/package.json +++ b/test/fixtures/link-with-title/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/log-handling/example.js b/test/fixtures/log-handling/example.js index 05e1cb0..2417fd6 100644 --- a/test/fixtures/log-handling/example.js +++ b/test/fixtures/log-handling/example.js @@ -1,16 +1,16 @@ // Require `pi`: -var pi = require('./index.js'); +var pi = require('./index.js') // Log an object: -console.log(this); +console.log(this) // Log nothing, _twice_: // Another comment. -console.log(); -console.log(); +console.log() +console.log() // A commented log: `console.log()`. // A final comment, missing an EOL. -console.log('markdown', 'Some **thing**.'); \ No newline at end of file +console.log('markdown', 'Some **thing**.') \ No newline at end of file diff --git a/test/fixtures/log-handling/index.js b/test/fixtures/log-handling/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/log-handling/index.js +++ b/test/fixtures/log-handling/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/log-handling/output.md b/test/fixtures/log-handling/output.md index 8d94ca8..239c29c 100644 --- a/test/fixtures/log-handling/output.md +++ b/test/fixtures/log-handling/output.md @@ -5,26 +5,26 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Log an object: -```javascript -console.log(this); -``` + {} Log nothing, _twice_: Another comment. -```javascript -console.log(); -console.log(); ``` -A commented log: `console.log()`. +``` +``` + +``` + +A commented log: `console.log()`. A final comment, missing an EOL. ```markdown diff --git a/test/fixtures/log-then-code/example.js b/test/fixtures/log-then-code/example.js new file mode 100644 index 0000000..0cd798c --- /dev/null +++ b/test/fixtures/log-then-code/example.js @@ -0,0 +1,6 @@ +var pi = require('./index.js') + +// Logs: +console.log('text', pi) + +pi + pi diff --git a/test/fixtures/log-then-code/index.js b/test/fixtures/log-then-code/index.js new file mode 100644 index 0000000..aadb3b0 --- /dev/null +++ b/test/fixtures/log-then-code/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.floor(Math.PI * 1e6) / 1e6 diff --git a/test/fixtures/log-then-code/output.md b/test/fixtures/log-then-code/output.md new file mode 100644 index 0000000..e1c0c22 --- /dev/null +++ b/test/fixtures/log-then-code/output.md @@ -0,0 +1,17 @@ +# PI + +## Usage + +```javascript +var pi = require('pi') +``` + +Logs: + +```text +3.141592 +``` + +```javascript +pi + pi +``` diff --git a/test/fixtures/log-then-code/package.json b/test/fixtures/log-then-code/package.json new file mode 100644 index 0000000..7d35bc7 --- /dev/null +++ b/test/fixtures/log-then-code/package.json @@ -0,0 +1,3 @@ +{ + "name": "pi" +} diff --git a/test/fixtures/log-then-code/readme.md b/test/fixtures/log-then-code/readme.md new file mode 100644 index 0000000..f78b72b --- /dev/null +++ b/test/fixtures/log-then-code/readme.md @@ -0,0 +1,3 @@ +# PI + +## Usage diff --git a/test/fixtures/multiline-console-calls/example.js b/test/fixtures/multiline-console-calls/example.js new file mode 100644 index 0000000..447904e --- /dev/null +++ b/test/fixtures/multiline-console-calls/example.js @@ -0,0 +1,7 @@ +var pi = require('./index.js') + +// Logs: +console.log( + 'text', + pi +) diff --git a/test/fixtures/multiline-console-calls/index.js b/test/fixtures/multiline-console-calls/index.js new file mode 100644 index 0000000..45a6d8c --- /dev/null +++ b/test/fixtures/multiline-console-calls/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/multiline-console-calls/output.md b/test/fixtures/multiline-console-calls/output.md new file mode 100644 index 0000000..644ddb2 --- /dev/null +++ b/test/fixtures/multiline-console-calls/output.md @@ -0,0 +1,13 @@ +# PI + +## Usage + +```javascript +var pi = require('pi') +``` + +Logs: + +```text +3.1415 +``` diff --git a/test/fixtures/multiline-console-calls/package.json b/test/fixtures/multiline-console-calls/package.json new file mode 100644 index 0000000..7d35bc7 --- /dev/null +++ b/test/fixtures/multiline-console-calls/package.json @@ -0,0 +1,3 @@ +{ + "name": "pi" +} diff --git a/test/fixtures/multiline-console-calls/readme.md b/test/fixtures/multiline-console-calls/readme.md new file mode 100644 index 0000000..f78b72b --- /dev/null +++ b/test/fixtures/multiline-console-calls/readme.md @@ -0,0 +1,3 @@ +# PI + +## Usage diff --git a/test/fixtures/multiple-calls/example.js b/test/fixtures/multiple-calls/example.js new file mode 100644 index 0000000..4907c69 --- /dev/null +++ b/test/fixtures/multiple-calls/example.js @@ -0,0 +1,8 @@ +var pi = require('./index.js') + +var i = 0 + +while (++i < 5) { + // Print `Number(pi) + i`: + console.log(Number(pi) + i) +} diff --git a/test/fixtures/multiple-calls/index.js b/test/fixtures/multiple-calls/index.js new file mode 100644 index 0000000..45a6d8c --- /dev/null +++ b/test/fixtures/multiple-calls/index.js @@ -0,0 +1,3 @@ +'use strict' + +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/multiple-calls/output.md b/test/fixtures/multiple-calls/output.md new file mode 100644 index 0000000..95c6f2f --- /dev/null +++ b/test/fixtures/multiple-calls/output.md @@ -0,0 +1,22 @@ +# PI + +## Usage + +```javascript +var pi = require('pi') + +var i = 0 + +while (++i < 5) { +``` + +Print `Number(pi) + i`: + + 4.141500000000001 + 5.141500000000001 + 6.141500000000001 + 7.141500000000001 + +```javascript +} +``` diff --git a/test/fixtures/multiple-calls/package.json b/test/fixtures/multiple-calls/package.json new file mode 100644 index 0000000..7d35bc7 --- /dev/null +++ b/test/fixtures/multiple-calls/package.json @@ -0,0 +1,3 @@ +{ + "name": "pi" +} diff --git a/test/fixtures/multiple-calls/readme.md b/test/fixtures/multiple-calls/readme.md new file mode 100644 index 0000000..f78b72b --- /dev/null +++ b/test/fixtures/multiple-calls/readme.md @@ -0,0 +1,3 @@ +# PI + +## Usage diff --git a/test/fixtures/multiple-usage-headings/example.js b/test/fixtures/multiple-usage-headings/example.js index 78a8167..53941c1 100644 --- a/test/fixtures/multiple-usage-headings/example.js +++ b/test/fixtures/multiple-usage-headings/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./index.js'); +var pi = require('./index.js') // Logs: console.log('text', pi); diff --git a/test/fixtures/multiple-usage-headings/index.js b/test/fixtures/multiple-usage-headings/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/multiple-usage-headings/index.js +++ b/test/fixtures/multiple-usage-headings/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/multiple-usage-headings/output.md b/test/fixtures/multiple-usage-headings/output.md index 0094730..38f4a07 100644 --- a/test/fixtures/multiple-usage-headings/output.md +++ b/test/fixtures/multiple-usage-headings/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/multiple-usage-headings/package.json b/test/fixtures/multiple-usage-headings/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/multiple-usage-headings/package.json +++ b/test/fixtures/multiple-usage-headings/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/no-main/example.js b/test/fixtures/no-main/example.js new file mode 100644 index 0000000..5ee225b --- /dev/null +++ b/test/fixtures/no-main/example.js @@ -0,0 +1,7 @@ +var a = 1 +var b = 2 + +var result = a + b + +// Yields: +console.log(result) diff --git a/test/fixtures/no-main/output.md b/test/fixtures/no-main/output.md new file mode 100644 index 0000000..07783f6 --- /dev/null +++ b/test/fixtures/no-main/output.md @@ -0,0 +1,14 @@ +# PI + +## Usage + +```javascript +var a = 1 +var b = 2 + +var result = a + b +``` + +Yields: + + 3 diff --git a/test/fixtures/no-main/package.json b/test/fixtures/no-main/package.json new file mode 100644 index 0000000..7d35bc7 --- /dev/null +++ b/test/fixtures/no-main/package.json @@ -0,0 +1,3 @@ +{ + "name": "pi" +} diff --git a/test/fixtures/no-main/readme.md b/test/fixtures/no-main/readme.md new file mode 100644 index 0000000..f78b72b --- /dev/null +++ b/test/fixtures/no-main/readme.md @@ -0,0 +1,3 @@ +# PI + +## Usage diff --git a/test/fixtures/no-usage-heading/example.js b/test/fixtures/no-usage-heading/example.js index 78a8167..ff50019 100644 --- a/test/fixtures/no-usage-heading/example.js +++ b/test/fixtures/no-usage-heading/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./index.js'); +var pi = require('./index.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/no-usage-heading/index.js b/test/fixtures/no-usage-heading/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/no-usage-heading/index.js +++ b/test/fixtures/no-usage-heading/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-custom-example/config.json b/test/fixtures/normal-custom-example/config.json index 6f9135b..f66f9c2 100644 --- a/test/fixtures/normal-custom-example/config.json +++ b/test/fixtures/normal-custom-example/config.json @@ -1,3 +1,3 @@ { - "example": "ex" + "example": "./ex" } diff --git a/test/fixtures/normal-custom-example/ex.js b/test/fixtures/normal-custom-example/ex.js index 78a8167..ff50019 100644 --- a/test/fixtures/normal-custom-example/ex.js +++ b/test/fixtures/normal-custom-example/ex.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./index.js'); +var pi = require('./index.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal-custom-example/index.js b/test/fixtures/normal-custom-example/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-custom-example/index.js +++ b/test/fixtures/normal-custom-example/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-custom-example/output.md b/test/fixtures/normal-custom-example/output.md index 0bd7b11..62885c5 100644 --- a/test/fixtures/normal-custom-example/output.md +++ b/test/fixtures/normal-custom-example/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/normal-custom-example/package.json b/test/fixtures/normal-custom-example/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/normal-custom-example/package.json +++ b/test/fixtures/normal-custom-example/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/normal-custom-heading/example.js b/test/fixtures/normal-custom-heading/example.js index 78a8167..ff50019 100644 --- a/test/fixtures/normal-custom-heading/example.js +++ b/test/fixtures/normal-custom-heading/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./index.js'); +var pi = require('./index.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal-custom-heading/index.js b/test/fixtures/normal-custom-heading/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-custom-heading/index.js +++ b/test/fixtures/normal-custom-heading/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-custom-heading/output.md b/test/fixtures/normal-custom-heading/output.md index 9bf7d62..b48434f 100644 --- a/test/fixtures/normal-custom-heading/output.md +++ b/test/fixtures/normal-custom-heading/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/normal-custom-main-with-package/example.js b/test/fixtures/normal-custom-main-with-package/example.js index 6d35f2a..7007c42 100644 --- a/test/fixtures/normal-custom-main-with-package/example.js +++ b/test/fixtures/normal-custom-main-with-package/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./pi.js'); +var pi = require('./pi.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal-custom-main-with-package/output.md b/test/fixtures/normal-custom-main-with-package/output.md index 0bd7b11..62885c5 100644 --- a/test/fixtures/normal-custom-main-with-package/output.md +++ b/test/fixtures/normal-custom-main-with-package/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/normal-custom-main-with-package/pi.js b/test/fixtures/normal-custom-main-with-package/pi.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-custom-main-with-package/pi.js +++ b/test/fixtures/normal-custom-main-with-package/pi.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-custom-main-without-package/example.js b/test/fixtures/normal-custom-main-without-package/example.js index 6d35f2a..7007c42 100644 --- a/test/fixtures/normal-custom-main-without-package/example.js +++ b/test/fixtures/normal-custom-main-without-package/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./pi.js'); +var pi = require('./pi.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal-custom-main-without-package/output.md b/test/fixtures/normal-custom-main-without-package/output.md index 0bd7b11..62885c5 100644 --- a/test/fixtures/normal-custom-main-without-package/output.md +++ b/test/fixtures/normal-custom-main-without-package/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/normal-custom-main-without-package/pi.js b/test/fixtures/normal-custom-main-without-package/pi.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-custom-main-without-package/pi.js +++ b/test/fixtures/normal-custom-main-without-package/pi.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-custom-main/example.js b/test/fixtures/normal-custom-main/example.js index 6d35f2a..7007c42 100644 --- a/test/fixtures/normal-custom-main/example.js +++ b/test/fixtures/normal-custom-main/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./pi.js'); +var pi = require('./pi.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal-custom-main/output.md b/test/fixtures/normal-custom-main/output.md index 0bd7b11..62885c5 100644 --- a/test/fixtures/normal-custom-main/output.md +++ b/test/fixtures/normal-custom-main/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/normal-custom-main/package.json b/test/fixtures/normal-custom-main/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/normal-custom-main/package.json +++ b/test/fixtures/normal-custom-main/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/normal-custom-main/pi.js b/test/fixtures/normal-custom-main/pi.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-custom-main/pi.js +++ b/test/fixtures/normal-custom-main/pi.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-custom-name-without-package/example.js b/test/fixtures/normal-custom-name-without-package/example.js index 78a8167..7579560 100644 --- a/test/fixtures/normal-custom-name-without-package/example.js +++ b/test/fixtures/normal-custom-name-without-package/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./index.js'); +var pi = require('.') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal-custom-name-without-package/index.js b/test/fixtures/normal-custom-name-without-package/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-custom-name-without-package/index.js +++ b/test/fixtures/normal-custom-name-without-package/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-custom-name-without-package/output.md b/test/fixtures/normal-custom-name-without-package/output.md index 20fe2ac..dbd936e 100644 --- a/test/fixtures/normal-custom-name-without-package/output.md +++ b/test/fixtures/normal-custom-name-without-package/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('usage'); +var pi = require('usage') ``` Logs: diff --git a/test/fixtures/normal-custom-name/example.js b/test/fixtures/normal-custom-name/example.js index 78a8167..ff50019 100644 --- a/test/fixtures/normal-custom-name/example.js +++ b/test/fixtures/normal-custom-name/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./index.js'); +var pi = require('./index.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal-custom-name/index.js b/test/fixtures/normal-custom-name/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-custom-name/index.js +++ b/test/fixtures/normal-custom-name/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-custom-name/output.md b/test/fixtures/normal-custom-name/output.md index 20fe2ac..dbd936e 100644 --- a/test/fixtures/normal-custom-name/output.md +++ b/test/fixtures/normal-custom-name/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('usage'); +var pi = require('usage') ``` Logs: diff --git a/test/fixtures/normal-custom-name/package.json b/test/fixtures/normal-custom-name/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/normal-custom-name/package.json +++ b/test/fixtures/normal-custom-name/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/normal-doc-directory/doc/example.js b/test/fixtures/normal-doc-directory/doc/example.js index 491babc..4439dbb 100644 --- a/test/fixtures/normal-doc-directory/doc/example.js +++ b/test/fixtures/normal-doc-directory/doc/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('../index.js'); +var pi = require('../index.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal-doc-directory/index.js b/test/fixtures/normal-doc-directory/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-doc-directory/index.js +++ b/test/fixtures/normal-doc-directory/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-doc-directory/output.md b/test/fixtures/normal-doc-directory/output.md index 0bd7b11..62885c5 100644 --- a/test/fixtures/normal-doc-directory/output.md +++ b/test/fixtures/normal-doc-directory/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/normal-doc-directory/package.json b/test/fixtures/normal-doc-directory/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/normal-doc-directory/package.json +++ b/test/fixtures/normal-doc-directory/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/normal-docs-directory/docs/example.js b/test/fixtures/normal-docs-directory/docs/example.js index 491babc..4439dbb 100644 --- a/test/fixtures/normal-docs-directory/docs/example.js +++ b/test/fixtures/normal-docs-directory/docs/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('../index.js'); +var pi = require('../index.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal-docs-directory/index.js b/test/fixtures/normal-docs-directory/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-docs-directory/index.js +++ b/test/fixtures/normal-docs-directory/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-docs-directory/output.md b/test/fixtures/normal-docs-directory/output.md index 0bd7b11..62885c5 100644 --- a/test/fixtures/normal-docs-directory/output.md +++ b/test/fixtures/normal-docs-directory/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/normal-docs-directory/package.json b/test/fixtures/normal-docs-directory/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/normal-docs-directory/package.json +++ b/test/fixtures/normal-docs-directory/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/normal-example-directory/example/index.js b/test/fixtures/normal-example-directory/example/index.js index 491babc..4439dbb 100644 --- a/test/fixtures/normal-example-directory/example/index.js +++ b/test/fixtures/normal-example-directory/example/index.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('../index.js'); +var pi = require('../index.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal-example-directory/index.js b/test/fixtures/normal-example-directory/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-example-directory/index.js +++ b/test/fixtures/normal-example-directory/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-example-directory/output.md b/test/fixtures/normal-example-directory/output.md index 0bd7b11..62885c5 100644 --- a/test/fixtures/normal-example-directory/output.md +++ b/test/fixtures/normal-example-directory/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/normal-example-directory/package.json b/test/fixtures/normal-example-directory/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/normal-example-directory/package.json +++ b/test/fixtures/normal-example-directory/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/normal-examples-directory/examples/index.js b/test/fixtures/normal-examples-directory/examples/index.js index 491babc..4439dbb 100644 --- a/test/fixtures/normal-examples-directory/examples/index.js +++ b/test/fixtures/normal-examples-directory/examples/index.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('../index.js'); +var pi = require('../index.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal-examples-directory/index.js b/test/fixtures/normal-examples-directory/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-examples-directory/index.js +++ b/test/fixtures/normal-examples-directory/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal-examples-directory/output.md b/test/fixtures/normal-examples-directory/output.md index 0bd7b11..62885c5 100644 --- a/test/fixtures/normal-examples-directory/output.md +++ b/test/fixtures/normal-examples-directory/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/normal-examples-directory/package.json b/test/fixtures/normal-examples-directory/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/normal-examples-directory/package.json +++ b/test/fixtures/normal-examples-directory/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/normal-without-options-or-package/index.js b/test/fixtures/normal-without-options-or-package/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal-without-options-or-package/index.js +++ b/test/fixtures/normal-without-options-or-package/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal/example.js b/test/fixtures/normal/example.js index 78a8167..ff50019 100644 --- a/test/fixtures/normal/example.js +++ b/test/fixtures/normal/example.js @@ -1,5 +1,5 @@ // Require `pi`: -var pi = require('./index.js'); +var pi = require('./index.js') // Logs: -console.log('text', pi); +console.log('text', pi) diff --git a/test/fixtures/normal/index.js b/test/fixtures/normal/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/normal/index.js +++ b/test/fixtures/normal/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/normal/output.md b/test/fixtures/normal/output.md index 0bd7b11..62885c5 100644 --- a/test/fixtures/normal/output.md +++ b/test/fixtures/normal/output.md @@ -5,7 +5,7 @@ Require `pi`: ```javascript -var pi = require('pi'); +var pi = require('pi') ``` Logs: diff --git a/test/fixtures/require-handling/index.js b/test/fixtures/require-handling/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/require-handling/index.js +++ b/test/fixtures/require-handling/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/require-handling/package.json b/test/fixtures/require-handling/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/require-handling/package.json +++ b/test/fixtures/require-handling/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/require-last/index.js b/test/fixtures/require-last/index.js index 2cc299c..45a6d8c 100644 --- a/test/fixtures/require-last/index.js +++ b/test/fixtures/require-last/index.js @@ -1,3 +1,3 @@ -'use strict'; +'use strict' -module.exports = Math.PI.toString().slice(0, 6); +module.exports = Math.PI.toString().slice(0, 6) diff --git a/test/fixtures/require-last/package.json b/test/fixtures/require-last/package.json index 34386b7..7d35bc7 100644 --- a/test/fixtures/require-last/package.json +++ b/test/fixtures/require-last/package.json @@ -1,3 +1,3 @@ { "name": "pi" -} \ No newline at end of file +} diff --git a/test/fixtures/stdout/example.js b/test/fixtures/stdout/example.js new file mode 100644 index 0000000..d97c9a5 --- /dev/null +++ b/test/fixtures/stdout/example.js @@ -0,0 +1,3 @@ +process.stdout.write('foo') +console.log('bar') +process.stdout.write('baz') diff --git a/test/fixtures/stdout/output.md b/test/fixtures/stdout/output.md new file mode 100644 index 0000000..b1caec3 --- /dev/null +++ b/test/fixtures/stdout/output.md @@ -0,0 +1,13 @@ +# PI + +## Usage + +```javascript +process.stdout.write('foo') +``` + + bar + +```javascript +process.stdout.write('baz') +``` diff --git a/test/fixtures/stdout/package.json b/test/fixtures/stdout/package.json new file mode 100644 index 0000000..7d35bc7 --- /dev/null +++ b/test/fixtures/stdout/package.json @@ -0,0 +1,3 @@ +{ + "name": "pi" +} diff --git a/test/fixtures/stdout/readme.md b/test/fixtures/stdout/readme.md new file mode 100644 index 0000000..f78b72b --- /dev/null +++ b/test/fixtures/stdout/readme.md @@ -0,0 +1,3 @@ +# PI + +## Usage diff --git a/test/index.js b/test/index.js index ad94276..1f67962 100644 --- a/test/index.js +++ b/test/index.js @@ -9,7 +9,6 @@ var negate = require('negate') var usage = require('..') var read = fs.readFileSync -var exists = fs.existsSync test('usage()', function(t) { t.equal(typeof usage, 'function', 'should be a function') @@ -24,40 +23,62 @@ test('usage()', function(t) { var root = path.join(__dirname, 'fixtures') var fixtures = fs.readdirSync(root).filter(negate(hidden)) +fs.renameSync('package.json', 'package.json.bak') + +test.onFinish(function() { + fs.renameSync('package.json.bak', 'package.json') +}) + test('Fixtures', function(t) { fixtures.forEach(function(fixture) { - var filepath = root + '/' + fixture - var config = filepath + '/config.json' - var output = filepath + '/output.md' - var input - var result - var fail - - config = exists(config) ? require(config) : {} - output = exists(output) ? read(output, 'utf-8') : '' + t.test(fixture, function(st) { + var base = path.join(root, fixture) + var input = read(path.join(base, 'readme.md')) + var expected = '' + var config = {} + var file - input = read(filepath + '/readme.md', 'utf-8') + st.plan(1) - config.cwd = filepath + try { + expected = String(read(path.join(base, 'output.md'))) + } catch (error) {} - fail = fixture.indexOf('fail-') === 0 ? fixture.slice(5) : '' + try { + config = JSON.parse(read(path.join(base, 'config.json'))) + } catch (error) {} - try { - result = remark() - .use(usage, config) - .processSync(input) - .toString() + file = {contents: input, cwd: base} - t.equal(result, output, 'should work on `' + fixture + '`') - } catch (error) { - if (!fail) { - throw error + if (!config.withoutFilePath) { + file.path = 'readme.md' } - fail = new RegExp(fail.replace(/-/, ' '), 'i') - - t.equal(fail.test(error), true, 'should fail on `' + fixture + '`') - } + remark() + .use(usage, config) + .process(file, onprocess) + + function onprocess(err, file) { + var fail = fixture.indexOf('fail-') === 0 ? fixture.slice(5) : '' + var errMessage = fail ? new RegExp(fail.replace(/-/g, ' '), 'i') : null + + if (fail) { + if (err) { + if (errMessage.test(err)) { + st.pass('should fail') + } else { + st.error(err, 'should fail') + } + } else { + st.fail('should fail instead of work') + } + } else if (err) { + st.error(err, 'should work instead of fail') + } else { + st.equal(String(file), expected, 'should work') + } + } + }) }) t.end()