UNPKG

jsinspect-plus

Version:

Detect copy-pasted and structurally similar code. Supports ES2020 standard (and most proposed features), TS and TSX files. Using Babel 8's parser.

60 lines (51 loc) 1.51 kB
const chalk = require('chalk'); const BaseReporter = require('./base'); class JSONReporter extends BaseReporter { /** * A JSON reporter, which displays both file and line information for * each given match. * * @constructor * * @param {Inspector} inspector Instance on which to register its listeners * @param {object} opts Options to set for the reporter */ constructor(inspector, opts) { opts = opts || {}; super(inspector, opts); const enabled = chalk.enabled; inspector.on('start', () => { chalk.enabled = false; this._writableStream.write('['); }); inspector.on('end', () => { chalk.enabled = enabled; this._writableStream.write(']\n'); }); } /** * Returns the string output to print for the given reporter. The formatted * JSON string contains the number of instances associated with the match and * the files and lines involved. * * @private * * @param {Match} match The inspector match to output * @returns {string} The formatted output */ _getOutput(match) { let output = (this._found > 1) ? ',\n' : ''; output += JSON.stringify({ id: match.hash, instances: match.instances.map(instance => { return { path: this._getRelativePath(instance.filename), lines: [instance.start.line, instance.end.line], code: this._getLines(instance) }; }) }); return output; } } module.exports = JSONReporter;