UNPKG

prettier

Version:

Prettier is an opinionated code formatter

1,870 lines (1,530 loc) 995 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var path = _interopDefault(require('path')); var os = _interopDefault(require('os')); var assert = _interopDefault(require('assert')); var fs = _interopDefault(require('fs')); var util = _interopDefault(require('util')); var events = _interopDefault(require('events')); var thirdParty = require('./third-party'); var thirdParty__default = thirdParty['default']; var name = "prettier"; var version$1 = "1.13.0"; var description = "Prettier is an opinionated code formatter"; var bin = { "prettier": "./bin/prettier.js" }; var repository = "prettier/prettier"; var homepage = "https://prettier.io"; var author = "James Long"; var license = "MIT"; var main = "./index.js"; var engines = { "node": ">=6" }; var dependencies = { "@babel/code-frame": "7.0.0-beta.49", "@babel/parser": "7.0.0-beta.49", "@glimmer/syntax": "0.30.3", "camelcase": "4.1.0", "chalk": "2.1.0", "cjk-regex": "1.0.2", "cosmiconfig": "3.1.0", "dashify": "0.2.2", "dedent": "0.7.0", "diff": "3.2.0", "editorconfig": "0.15.0", "editorconfig-to-prettier": "0.0.6", "emoji-regex": "6.5.1", "escape-string-regexp": "1.0.5", "esutils": "2.0.2", "find-parent-dir": "0.3.0", "find-project-root": "1.1.1", "flow-parser": "0.73.0", "get-stream": "3.0.0", "globby": "6.1.0", "graphql": "0.13.2", "html-tag-names": "1.1.2", "ignore": "3.3.7", "jest-docblock": "22.2.2", "json-stable-stringify": "1.0.1", "leven": "2.1.0", "lodash.uniqby": "4.7.0", "mem": "1.1.0", "minimatch": "3.0.4", "minimist": "1.2.0", "parse5": "3.0.3", "postcss-less": "1.1.5", "postcss-media-query-parser": "0.2.3", "postcss-scss": "1.0.5", "postcss-selector-parser": "2.2.3", "postcss-values-parser": "1.5.0", "remark-parse": "5.0.0", "resolve": "1.5.0", "semver": "5.4.1", "string-width": "2.1.1", "typescript": "2.9.0-dev.20180421", "typescript-eslint-parser": "eslint/typescript-eslint-parser#2960b002746c01fb9cb15bb5f4c1e7e925c6519a", "unicode-regex": "1.0.1", "unified": "6.1.6" }; var devDependencies = { "@babel/cli": "7.0.0-beta.49", "@babel/core": "7.0.0-beta.49", "@babel/preset-env": "7.0.0-beta.49", "builtin-modules": "2.0.0", "codecov": "2.2.0", "cross-env": "5.0.5", "eslint": "4.18.2", "eslint-config-prettier": "2.9.0", "eslint-friendly-formatter": "3.0.0", "eslint-plugin-import": "2.9.0", "eslint-plugin-prettier": "2.6.0", "eslint-plugin-react": "7.7.0", "jest": "21.1.0", "mkdirp": "0.5.1", "prettier": "1.12.1", "prettylint": "1.0.0", "rimraf": "2.6.2", "rollup": "0.47.6", "rollup-plugin-babel": "4.0.0-beta.4", "rollup-plugin-commonjs": "8.2.6", "rollup-plugin-json": "2.1.1", "rollup-plugin-node-builtins": "2.0.0", "rollup-plugin-node-globals": "1.1.0", "rollup-plugin-node-resolve": "2.0.0", "rollup-plugin-replace": "1.2.1", "rollup-plugin-uglify": "3.0.0", "shelljs": "0.8.1", "snapshot-diff": "0.2.2", "strip-ansi": "4.0.0", "tempy": "0.2.1", "webpack": "2.6.1" }; var scripts = { "prepublishOnly": "echo \"Error: must publish from dist/\" && exit 1", "prepare-release": "yarn && yarn build && yarn test:dist", "test": "jest", "test:dist": "node ./scripts/test-dist.js", "test-integration": "jest tests_integration", "lint": "cross-env EFF_NO_LINK_RULES=true eslint . --format node_modules/eslint-friendly-formatter", "lint-docs": "prettylint {.,docs,website,website/blog}/*.md", "build": "node ./scripts/build/build.js", "build-docs": "node ./scripts/old-build/build-docs.js", "check-deps": "node ./scripts/check-deps.js" }; var _package = { name: name, version: version$1, description: description, bin: bin, repository: repository, homepage: homepage, author: author, license: license, main: main, engines: engines, dependencies: dependencies, devDependencies: devDependencies, scripts: scripts }; var _package$1 = Object.freeze({ name: name, version: version$1, description: description, bin: bin, repository: repository, homepage: homepage, author: author, license: license, main: main, engines: engines, dependencies: dependencies, devDependencies: devDependencies, scripts: scripts, default: _package }); var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function unwrapExports (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } var base = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports['default'] = /*istanbul ignore end*/ Diff; function Diff() {} Diff.prototype = { /*istanbul ignore start*/ /*istanbul ignore end*/ diff: function diff(oldString, newString) { /*istanbul ignore start*/ var /*istanbul ignore end*/ options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; var callback = options.callback; if (typeof options === 'function') { callback = options; options = {}; } this.options = options; var self = this; function done(value) { if (callback) { setTimeout(function () { callback(undefined, value); }, 0); return true; } else { return value; } } // Allow subclasses to massage the input prior to running oldString = this.castInput(oldString); newString = this.castInput(newString); oldString = this.removeEmpty(this.tokenize(oldString)); newString = this.removeEmpty(this.tokenize(newString)); var newLen = newString.length, oldLen = oldString.length; var editLength = 1; var maxEditLength = newLen + oldLen; var bestPath = [{ newPos: -1, components: [] }]; // Seed editLength = 0, i.e. the content starts with the same values var oldPos = this.extractCommon(bestPath[0], newString, oldString, 0); if (bestPath[0].newPos + 1 >= newLen && oldPos + 1 >= oldLen) { // Identity per the equality and tokenizer return done([{ value: this.join(newString), count: newString.length }]); } // Main worker method. checks all permutations of a given edit length for acceptance. function execEditLength() { for (var diagonalPath = -1 * editLength; diagonalPath <= editLength; diagonalPath += 2) { var basePath = /*istanbul ignore start*/ void 0; var addPath = bestPath[diagonalPath - 1], removePath = bestPath[diagonalPath + 1], _oldPos = (removePath ? removePath.newPos : 0) - diagonalPath; if (addPath) { // No one else is going to attempt to use this value, clear it bestPath[diagonalPath - 1] = undefined; } var canAdd = addPath && addPath.newPos + 1 < newLen, canRemove = removePath && 0 <= _oldPos && _oldPos < oldLen; if (!canAdd && !canRemove) { // If this path is a terminal then prune bestPath[diagonalPath] = undefined; continue; } // Select the diagonal that we want to branch from. We select the prior // path whose position in the new string is the farthest from the origin // and does not pass the bounds of the diff graph if (!canAdd || canRemove && addPath.newPos < removePath.newPos) { basePath = clonePath(removePath); self.pushComponent(basePath.components, undefined, true); } else { basePath = addPath; // No need to clone, we've pulled it from the list basePath.newPos++; self.pushComponent(basePath.components, true, undefined); } _oldPos = self.extractCommon(basePath, newString, oldString, diagonalPath); // If we have hit the end of both strings, then we are done if (basePath.newPos + 1 >= newLen && _oldPos + 1 >= oldLen) { return done(buildValues(self, basePath.components, newString, oldString, self.useLongestToken)); } else { // Otherwise track this path as a potential candidate and continue. bestPath[diagonalPath] = basePath; } } editLength++; } // Performs the length of edit iteration. Is a bit fugly as this has to support the // sync and async mode which is never fun. Loops over execEditLength until a value // is produced. if (callback) { (function exec() { setTimeout(function () { // This should not happen, but we want to be safe. /* istanbul ignore next */ if (editLength > maxEditLength) { return callback(); } if (!execEditLength()) { exec(); } }, 0); })(); } else { while (editLength <= maxEditLength) { var ret = execEditLength(); if (ret) { return ret; } } } }, /*istanbul ignore start*/ /*istanbul ignore end*/ pushComponent: function pushComponent(components, added, removed) { var last = components[components.length - 1]; if (last && last.added === added && last.removed === removed) { // We need to clone here as the component clone operation is just // as shallow array clone components[components.length - 1] = { count: last.count + 1, added: added, removed: removed }; } else { components.push({ count: 1, added: added, removed: removed }); } }, /*istanbul ignore start*/ /*istanbul ignore end*/ extractCommon: function extractCommon(basePath, newString, oldString, diagonalPath) { var newLen = newString.length, oldLen = oldString.length, newPos = basePath.newPos, oldPos = newPos - diagonalPath, commonCount = 0; while (newPos + 1 < newLen && oldPos + 1 < oldLen && this.equals(newString[newPos + 1], oldString[oldPos + 1])) { newPos++; oldPos++; commonCount++; } if (commonCount) { basePath.components.push({ count: commonCount }); } basePath.newPos = newPos; return oldPos; }, /*istanbul ignore start*/ /*istanbul ignore end*/ equals: function equals(left, right) { return left === right; }, /*istanbul ignore start*/ /*istanbul ignore end*/ removeEmpty: function removeEmpty(array) { var ret = []; for (var i = 0; i < array.length; i++) { if (array[i]) { ret.push(array[i]); } } return ret; }, /*istanbul ignore start*/ /*istanbul ignore end*/ castInput: function castInput(value) { return value; }, /*istanbul ignore start*/ /*istanbul ignore end*/ tokenize: function tokenize(value) { return value.split(''); }, /*istanbul ignore start*/ /*istanbul ignore end*/ join: function join(chars) { return chars.join(''); } }; function buildValues(diff, components, newString, oldString, useLongestToken) { var componentPos = 0, componentLen = components.length, newPos = 0, oldPos = 0; for (; componentPos < componentLen; componentPos++) { var component = components[componentPos]; if (!component.removed) { if (!component.added && useLongestToken) { var value = newString.slice(newPos, newPos + component.count); value = value.map(function (value, i) { var oldValue = oldString[oldPos + i]; return oldValue.length > value.length ? oldValue : value; }); component.value = diff.join(value); } else { component.value = diff.join(newString.slice(newPos, newPos + component.count)); } newPos += component.count; // Common case if (!component.added) { oldPos += component.count; } } else { component.value = diff.join(oldString.slice(oldPos, oldPos + component.count)); oldPos += component.count; // Reverse add and remove so removes are output first to match common convention // The diffing algorithm is tied to add then remove output and this is the simplest // route to get the desired output with minimal overhead. if (componentPos && components[componentPos - 1].added) { var tmp = components[componentPos - 1]; components[componentPos - 1] = components[componentPos]; components[componentPos] = tmp; } } } // Special case handle for when one terminal is ignored. For this case we merge the // terminal into the prior string and drop the change. var lastComponent = components[componentLen - 1]; if (componentLen > 1 && (lastComponent.added || lastComponent.removed) && diff.equals('', lastComponent.value)) { components[componentLen - 2].value += lastComponent.value; components.pop(); } return components; } function clonePath(path$$1) { return { newPos: path$$1.newPos, components: path$$1.components.slice(0) }; } }); unwrapExports(base); var character = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports.characterDiff = undefined; exports. /*istanbul ignore end*/ diffChars = diffChars; /*istanbul ignore start*/ var _base2 = _interopRequireDefault(base); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } /*istanbul ignore end*/ var characterDiff = /*istanbul ignore start*/ exports. /*istanbul ignore end*/ characterDiff = new /*istanbul ignore start*/ _base2['default'](); function diffChars(oldStr, newStr, callback) { return characterDiff.diff(oldStr, newStr, callback); } }); unwrapExports(character); var params = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports. /*istanbul ignore end*/ generateOptions = generateOptions; function generateOptions(options, defaults) { if (typeof options === 'function') { defaults.callback = options; } else if (options) { for (var name in options) { /* istanbul ignore else */ if (options.hasOwnProperty(name)) { defaults[name] = options[name]; } } } return defaults; } }); unwrapExports(params); var word = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports.wordDiff = undefined; exports. /*istanbul ignore end*/ diffWords = diffWords; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ diffWordsWithSpace = diffWordsWithSpace; /*istanbul ignore start*/ var _base2 = _interopRequireDefault(base); /*istanbul ignore end*/ /*istanbul ignore start*/ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } /*istanbul ignore end*/ // Based on https://en.wikipedia.org/wiki/Latin_script_in_Unicode // // Ranges and exceptions: // Latin-1 Supplement, 0080–00FF // - U+00D7 × Multiplication sign // - U+00F7 ÷ Division sign // Latin Extended-A, 0100–017F // Latin Extended-B, 0180–024F // IPA Extensions, 0250–02AF // Spacing Modifier Letters, 02B0–02FF // - U+02C7 ˇ &#711; Caron // - U+02D8 ˘ &#728; Breve // - U+02D9 ˙ &#729; Dot Above // - U+02DA ˚ &#730; Ring Above // - U+02DB ˛ &#731; Ogonek // - U+02DC ˜ &#732; Small Tilde // - U+02DD ˝ &#733; Double Acute Accent // Latin Extended Additional, 1E00–1EFF var extendedWordChars = /^[A-Za-z\xC0-\u02C6\u02C8-\u02D7\u02DE-\u02FF\u1E00-\u1EFF]+$/; var reWhitespace = /\S/; var wordDiff = /*istanbul ignore start*/ exports. /*istanbul ignore end*/ wordDiff = new /*istanbul ignore start*/ _base2['default'](); wordDiff.equals = function (left, right) { return left === right || this.options.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right); }; wordDiff.tokenize = function (value) { var tokens = value.split(/(\s+|\b)/); // Join the boundary splits that we do not consider to be boundaries. This is primarily the extended Latin character set. for (var i = 0; i < tokens.length - 1; i++) { // If we have an empty string in the next field and we have only word chars before and after, merge if (!tokens[i + 1] && tokens[i + 2] && extendedWordChars.test(tokens[i]) && extendedWordChars.test(tokens[i + 2])) { tokens[i] += tokens[i + 2]; tokens.splice(i + 1, 2); i--; } } return tokens; }; function diffWords(oldStr, newStr, callback) { var options = /*istanbul ignore start*/ (0, params.generateOptions /*istanbul ignore end*/ )(callback, { ignoreWhitespace: true }); return wordDiff.diff(oldStr, newStr, options); } function diffWordsWithSpace(oldStr, newStr, callback) { return wordDiff.diff(oldStr, newStr, callback); } }); unwrapExports(word); var line = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports.lineDiff = undefined; exports. /*istanbul ignore end*/ diffLines = diffLines; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ diffTrimmedLines = diffTrimmedLines; /*istanbul ignore start*/ var _base2 = _interopRequireDefault(base); /*istanbul ignore end*/ /*istanbul ignore start*/ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } /*istanbul ignore end*/ var lineDiff = /*istanbul ignore start*/ exports. /*istanbul ignore end*/ lineDiff = new /*istanbul ignore start*/ _base2['default'](); lineDiff.tokenize = function (value) { var retLines = [], linesAndNewlines = value.split(/(\n|\r\n)/); // Ignore the final empty token that occurs if the string ends with a new line if (!linesAndNewlines[linesAndNewlines.length - 1]) { linesAndNewlines.pop(); } // Merge the content and line separators into single tokens for (var i = 0; i < linesAndNewlines.length; i++) { var line = linesAndNewlines[i]; if (i % 2 && !this.options.newlineIsToken) { retLines[retLines.length - 1] += line; } else { if (this.options.ignoreWhitespace) { line = line.trim(); } retLines.push(line); } } return retLines; }; function diffLines(oldStr, newStr, callback) { return lineDiff.diff(oldStr, newStr, callback); } function diffTrimmedLines(oldStr, newStr, callback) { var options = /*istanbul ignore start*/ (0, params.generateOptions /*istanbul ignore end*/ )(callback, { ignoreWhitespace: true }); return lineDiff.diff(oldStr, newStr, options); } }); unwrapExports(line); var sentence = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports.sentenceDiff = undefined; exports. /*istanbul ignore end*/ diffSentences = diffSentences; /*istanbul ignore start*/ var _base2 = _interopRequireDefault(base); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } /*istanbul ignore end*/ var sentenceDiff = /*istanbul ignore start*/ exports. /*istanbul ignore end*/ sentenceDiff = new /*istanbul ignore start*/ _base2['default'](); sentenceDiff.tokenize = function (value) { return value.split(/(\S.+?[.!?])(?=\s+|$)/); }; function diffSentences(oldStr, newStr, callback) { return sentenceDiff.diff(oldStr, newStr, callback); } }); unwrapExports(sentence); var css = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports.cssDiff = undefined; exports. /*istanbul ignore end*/ diffCss = diffCss; /*istanbul ignore start*/ var _base2 = _interopRequireDefault(base); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } /*istanbul ignore end*/ var cssDiff = /*istanbul ignore start*/ exports. /*istanbul ignore end*/ cssDiff = new /*istanbul ignore start*/ _base2['default'](); cssDiff.tokenize = function (value) { return value.split(/([{}:;,]|\s+)/); }; function diffCss(oldStr, newStr, callback) { return cssDiff.diff(oldStr, newStr, callback); } }); unwrapExports(css); var json = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports.jsonDiff = undefined; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; exports. /*istanbul ignore end*/ diffJson = diffJson; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ canonicalize = canonicalize; /*istanbul ignore start*/ var _base2 = _interopRequireDefault(base); /*istanbul ignore end*/ /*istanbul ignore start*/ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } /*istanbul ignore end*/ var objectPrototypeToString = Object.prototype.toString; var jsonDiff = /*istanbul ignore start*/ exports. /*istanbul ignore end*/ jsonDiff = new /*istanbul ignore start*/ _base2['default'](); // Discriminate between two lines of pretty-printed, serialized JSON where one of them has a // dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output: jsonDiff.useLongestToken = true; jsonDiff.tokenize = /*istanbul ignore start*/ line.lineDiff. /*istanbul ignore end*/ tokenize; jsonDiff.castInput = function (value) { /*istanbul ignore start*/ var /*istanbul ignore end*/ undefinedReplacement = this.options.undefinedReplacement; return typeof value === 'string' ? value : JSON.stringify(canonicalize(value), function (k, v) { if (typeof v === 'undefined') { return undefinedReplacement; } return v; }, ' '); }; jsonDiff.equals = function (left, right) { return ( /*istanbul ignore start*/ _base2['default']. /*istanbul ignore end*/ prototype.equals(left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1')) ); }; function diffJson(oldObj, newObj, options) { return jsonDiff.diff(oldObj, newObj, options); } // This function handles the presence of circular references by bailing out when encountering an // object that is already on the "stack" of items being processed. function canonicalize(obj, stack, replacementStack) { stack = stack || []; replacementStack = replacementStack || []; var i = /*istanbul ignore start*/ void 0; for (i = 0; i < stack.length; i += 1) { if (stack[i] === obj) { return replacementStack[i]; } } var canonicalizedObj = /*istanbul ignore start*/ void 0; if ('[object Array]' === objectPrototypeToString.call(obj)) { stack.push(obj); canonicalizedObj = new Array(obj.length); replacementStack.push(canonicalizedObj); for (i = 0; i < obj.length; i += 1) { canonicalizedObj[i] = canonicalize(obj[i], stack, replacementStack); } stack.pop(); replacementStack.pop(); return canonicalizedObj; } if (obj && obj.toJSON) { obj = obj.toJSON(); } if ( /*istanbul ignore start*/ (typeof /*istanbul ignore end*/ obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && obj !== null) { stack.push(obj); canonicalizedObj = {}; replacementStack.push(canonicalizedObj); var sortedKeys = [], key = /*istanbul ignore start*/ void 0; for (key in obj) { /* istanbul ignore else */ if (obj.hasOwnProperty(key)) { sortedKeys.push(key); } } sortedKeys.sort(); for (i = 0; i < sortedKeys.length; i += 1) { key = sortedKeys[i]; canonicalizedObj[key] = canonicalize(obj[key], stack, replacementStack); } stack.pop(); replacementStack.pop(); } else { canonicalizedObj = obj; } return canonicalizedObj; } }); unwrapExports(json); var array = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports.arrayDiff = undefined; exports. /*istanbul ignore end*/ diffArrays = diffArrays; /*istanbul ignore start*/ var _base2 = _interopRequireDefault(base); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } /*istanbul ignore end*/ var arrayDiff = /*istanbul ignore start*/ exports. /*istanbul ignore end*/ arrayDiff = new /*istanbul ignore start*/ _base2['default'](); arrayDiff.tokenize = arrayDiff.join = function (value) { return value.slice(); }; function diffArrays(oldArr, newArr, callback) { return arrayDiff.diff(oldArr, newArr, callback); } }); unwrapExports(array); var parse = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports. /*istanbul ignore end*/ parsePatch = parsePatch; function parsePatch(uniDiff) { /*istanbul ignore start*/ var /*istanbul ignore end*/ options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; var diffstr = uniDiff.split(/\r\n|[\n\v\f\r\x85]/), delimiters = uniDiff.match(/\r\n|[\n\v\f\r\x85]/g) || [], list = [], i = 0; function parseIndex() { var index = {}; list.push(index); // Parse diff metadata while (i < diffstr.length) { var line = diffstr[i]; // File header found, end parsing diff metadata if (/^(\-\-\-|\+\+\+|@@)\s/.test(line)) { break; } // Diff index var header = /^(?:Index:|diff(?: -r \w+)+)\s+(.+?)\s*$/.exec(line); if (header) { index.index = header[1]; } i++; } // Parse file headers if they are defined. Unified diff requires them, but // there's no technical issues to have an isolated hunk without file header parseFileHeader(index); parseFileHeader(index); // Parse hunks index.hunks = []; while (i < diffstr.length) { var _line = diffstr[i]; if (/^(Index:|diff|\-\-\-|\+\+\+)\s/.test(_line)) { break; } else if (/^@@/.test(_line)) { index.hunks.push(parseHunk()); } else if (_line && options.strict) { // Ignore unexpected content unless in strict mode throw new Error('Unknown line ' + (i + 1) + ' ' + JSON.stringify(_line)); } else { i++; } } } // Parses the --- and +++ headers, if none are found, no lines // are consumed. function parseFileHeader(index) { var headerPattern = /^(---|\+\+\+)\s+([\S ]*)(?:\t(.*?)\s*)?$/; var fileHeader = headerPattern.exec(diffstr[i]); if (fileHeader) { var keyPrefix = fileHeader[1] === '---' ? 'old' : 'new'; index[keyPrefix + 'FileName'] = fileHeader[2]; index[keyPrefix + 'Header'] = fileHeader[3]; i++; } } // Parses a hunk // This assumes that we are at the start of a hunk. function parseHunk() { var chunkHeaderIndex = i, chunkHeaderLine = diffstr[i++], chunkHeader = chunkHeaderLine.split(/@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/); var hunk = { oldStart: +chunkHeader[1], oldLines: +chunkHeader[2] || 1, newStart: +chunkHeader[3], newLines: +chunkHeader[4] || 1, lines: [], linedelimiters: [] }; var addCount = 0, removeCount = 0; for (; i < diffstr.length; i++) { // Lines starting with '---' could be mistaken for the "remove line" operation // But they could be the header for the next file. Therefore prune such cases out. if (diffstr[i].indexOf('--- ') === 0 && i + 2 < diffstr.length && diffstr[i + 1].indexOf('+++ ') === 0 && diffstr[i + 2].indexOf('@@') === 0) { break; } var operation = diffstr[i][0]; if (operation === '+' || operation === '-' || operation === ' ' || operation === '\\') { hunk.lines.push(diffstr[i]); hunk.linedelimiters.push(delimiters[i] || '\n'); if (operation === '+') { addCount++; } else if (operation === '-') { removeCount++; } else if (operation === ' ') { addCount++; removeCount++; } } else { break; } } // Handle the empty block count case if (!addCount && hunk.newLines === 1) { hunk.newLines = 0; } if (!removeCount && hunk.oldLines === 1) { hunk.oldLines = 0; } // Perform optional sanity checking if (options.strict) { if (addCount !== hunk.newLines) { throw new Error('Added line count did not match for hunk at line ' + (chunkHeaderIndex + 1)); } if (removeCount !== hunk.oldLines) { throw new Error('Removed line count did not match for hunk at line ' + (chunkHeaderIndex + 1)); } } return hunk; } while (i < diffstr.length) { parseIndex(); } return list; } }); unwrapExports(parse); var distanceIterator = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ "use strict"; exports.__esModule = true; exports["default"] = /*istanbul ignore end*/ function (start, minLine, maxLine) { var wantForward = true, backwardExhausted = false, forwardExhausted = false, localOffset = 1; return function iterator() { if (wantForward && !forwardExhausted) { if (backwardExhausted) { localOffset++; } else { wantForward = false; } // Check if trying to fit beyond text length, and if not, check it fits // after offset location (or desired location on first iteration) if (start + localOffset <= maxLine) { return localOffset; } forwardExhausted = true; } if (!backwardExhausted) { if (!forwardExhausted) { wantForward = true; } // Check if trying to fit before text beginning, and if not, check it fits // before offset location if (minLine <= start - localOffset) { return -localOffset++; } backwardExhausted = true; return iterator(); } // We tried to fit hunk before text beginning and beyond text lenght, then // hunk can't fit on the text. Return undefined }; }; }); unwrapExports(distanceIterator); var apply = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports. /*istanbul ignore end*/ applyPatch = applyPatch; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ applyPatches = applyPatches; /*istanbul ignore start*/ var _distanceIterator2 = _interopRequireDefault(distanceIterator); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } /*istanbul ignore end*/ function applyPatch(source, uniDiff) { /*istanbul ignore start*/ var /*istanbul ignore end*/ options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; if (typeof uniDiff === 'string') { uniDiff = /*istanbul ignore start*/ (0, parse.parsePatch /*istanbul ignore end*/ )(uniDiff); } if (Array.isArray(uniDiff)) { if (uniDiff.length > 1) { throw new Error('applyPatch only works with a single input.'); } uniDiff = uniDiff[0]; } // Apply the diff to the input var lines = source.split(/\r\n|[\n\v\f\r\x85]/), delimiters = source.match(/\r\n|[\n\v\f\r\x85]/g) || [], hunks = uniDiff.hunks, compareLine = options.compareLine || function (lineNumber, line, operation, patchContent) /*istanbul ignore start*/ { return ( /*istanbul ignore end*/ line === patchContent ); }, errorCount = 0, fuzzFactor = options.fuzzFactor || 0, minLine = 0, offset = 0, removeEOFNL = /*istanbul ignore start*/ void 0 /*istanbul ignore end*/ , addEOFNL = /*istanbul ignore start*/ void 0; /** * Checks if the hunk exactly fits on the provided location */ function hunkFits(hunk, toPos) { for (var j = 0; j < hunk.lines.length; j++) { var line = hunk.lines[j], operation = line[0], content = line.substr(1); if (operation === ' ' || operation === '-') { // Context sanity check if (!compareLine(toPos + 1, lines[toPos], operation, content)) { errorCount++; if (errorCount > fuzzFactor) { return false; } } toPos++; } } return true; } // Search best fit offsets for each hunk based on the previous ones for (var i = 0; i < hunks.length; i++) { var hunk = hunks[i], maxLine = lines.length - hunk.oldLines, localOffset = 0, toPos = offset + hunk.oldStart - 1; var iterator = /*istanbul ignore start*/ (0, _distanceIterator2['default'] /*istanbul ignore end*/ )(toPos, minLine, maxLine); for (; localOffset !== undefined; localOffset = iterator()) { if (hunkFits(hunk, toPos + localOffset)) { hunk.offset = offset += localOffset; break; } } if (localOffset === undefined) { return false; } // Set lower text limit to end of the current hunk, so next ones don't try // to fit over already patched text minLine = hunk.offset + hunk.oldStart + hunk.oldLines; } // Apply patch hunks for (var _i = 0; _i < hunks.length; _i++) { var _hunk = hunks[_i], _toPos = _hunk.offset + _hunk.newStart - 1; if (_hunk.newLines == 0) { _toPos++; } for (var j = 0; j < _hunk.lines.length; j++) { var line = _hunk.lines[j], operation = line[0], content = line.substr(1), delimiter = _hunk.linedelimiters[j]; if (operation === ' ') { _toPos++; } else if (operation === '-') { lines.splice(_toPos, 1); delimiters.splice(_toPos, 1); /* istanbul ignore else */ } else if (operation === '+') { lines.splice(_toPos, 0, content); delimiters.splice(_toPos, 0, delimiter); _toPos++; } else if (operation === '\\') { var previousOperation = _hunk.lines[j - 1] ? _hunk.lines[j - 1][0] : null; if (previousOperation === '+') { removeEOFNL = true; } else if (previousOperation === '-') { addEOFNL = true; } } } } // Handle EOFNL insertion/removal if (removeEOFNL) { while (!lines[lines.length - 1]) { lines.pop(); delimiters.pop(); } } else if (addEOFNL) { lines.push(''); delimiters.push('\n'); } for (var _k = 0; _k < lines.length - 1; _k++) { lines[_k] = lines[_k] + delimiters[_k]; } return lines.join(''); } // Wrapper that supports multiple file patches via callbacks. function applyPatches(uniDiff, options) { if (typeof uniDiff === 'string') { uniDiff = /*istanbul ignore start*/ (0, parse.parsePatch /*istanbul ignore end*/ )(uniDiff); } var currentIndex = 0; function processIndex() { var index = uniDiff[currentIndex++]; if (!index) { return options.complete(); } options.loadFile(index, function (err, data) { if (err) { return options.complete(err); } var updatedContent = applyPatch(data, index, options); options.patched(index, updatedContent, function (err) { if (err) { return options.complete(err); } processIndex(); }); }); } processIndex(); } }); unwrapExports(apply); var create = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports. /*istanbul ignore end*/ structuredPatch = structuredPatch; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ createTwoFilesPatch = createTwoFilesPatch; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ createPatch = createPatch; /*istanbul ignore start*/ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } /*istanbul ignore end*/ function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) { if (!options) { options = {}; } if (typeof options.context === 'undefined') { options.context = 4; } var diff = /*istanbul ignore start*/ (0, line.diffLines /*istanbul ignore end*/ )(oldStr, newStr, options); diff.push({ value: '', lines: [] }); // Append an empty value to make cleanup easier function contextLines(lines) { return lines.map(function (entry) { return ' ' + entry; }); } var hunks = []; var oldRangeStart = 0, newRangeStart = 0, curRange = [], oldLine = 1, newLine = 1; /*istanbul ignore start*/ var _loop = function _loop( /*istanbul ignore end*/ i) { var current = diff[i], lines = current.lines || current.value.replace(/\n$/, '').split('\n'); current.lines = lines; if (current.added || current.removed) { /*istanbul ignore start*/ var _curRange; /*istanbul ignore end*/ // If we have previous context, start with that if (!oldRangeStart) { var prev = diff[i - 1]; oldRangeStart = oldLine; newRangeStart = newLine; if (prev) { curRange = options.context > 0 ? contextLines(prev.lines.slice(-options.context)) : []; oldRangeStart -= curRange.length; newRangeStart -= curRange.length; } } // Output our changes /*istanbul ignore start*/ (_curRange = /*istanbul ignore end*/ curRange).push. /*istanbul ignore start*/ apply /*istanbul ignore end*/ ( /*istanbul ignore start*/ _curRange /*istanbul ignore end*/ , /*istanbul ignore start*/ _toConsumableArray( /*istanbul ignore end*/ lines.map(function (entry) { return (current.added ? '+' : '-') + entry; }))); // Track the updated file position if (current.added) { newLine += lines.length; } else { oldLine += lines.length; } } else { // Identical context lines. Track line changes if (oldRangeStart) { // Close out any changes that have been output (or join overlapping) if (lines.length <= options.context * 2 && i < diff.length - 2) { /*istanbul ignore start*/ var _curRange2; /*istanbul ignore end*/ // Overlapping /*istanbul ignore start*/ (_curRange2 = /*istanbul ignore end*/ curRange).push. /*istanbul ignore start*/ apply /*istanbul ignore end*/ ( /*istanbul ignore start*/ _curRange2 /*istanbul ignore end*/ , /*istanbul ignore start*/ _toConsumableArray( /*istanbul ignore end*/ contextLines(lines))); } else { /*istanbul ignore start*/ var _curRange3; /*istanbul ignore end*/ // end the range and output var contextSize = Math.min(lines.length, options.context); /*istanbul ignore start*/ (_curRange3 = /*istanbul ignore end*/ curRange).push. /*istanbul ignore start*/ apply /*istanbul ignore end*/ ( /*istanbul ignore start*/ _curRange3 /*istanbul ignore end*/ , /*istanbul ignore start*/ _toConsumableArray( /*istanbul ignore end*/ contextLines(lines.slice(0, contextSize)))); var hunk = { oldStart: oldRangeStart, oldLines: oldLine - oldRangeStart + contextSize, newStart: newRangeStart, newLines: newLine - newRangeStart + contextSize, lines: curRange }; if (i >= diff.length - 2 && lines.length <= options.context) { // EOF is inside this hunk var oldEOFNewline = /\n$/.test(oldStr); var newEOFNewline = /\n$/.test(newStr); if (lines.length == 0 && !oldEOFNewline) { // special case: old has no eol and no trailing context; no-nl can end up before adds curRange.splice(hunk.oldLines, 0, '\\ No newline at end of file'); } else if (!oldEOFNewline || !newEOFNewline) { curRange.push('\\ No newline at end of file'); } } hunks.push(hunk); oldRangeStart = 0; newRangeStart = 0; curRange = []; } } oldLine += lines.length; newLine += lines.length; } }; for (var i = 0; i < diff.length; i++) { /*istanbul ignore start*/ _loop( /*istanbul ignore end*/ i); } return { oldFileName: oldFileName, newFileName: newFileName, oldHeader: oldHeader, newHeader: newHeader, hunks: hunks }; } function createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options) { var diff = structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHeader, newHeader, options); var ret = []; if (oldFileName == newFileName) { ret.push('Index: ' + oldFileName); } ret.push('==================================================================='); ret.push('--- ' + diff.oldFileName + (typeof diff.oldHeader === 'undefined' ? '' : '\t' + diff.oldHeader)); ret.push('+++ ' + diff.newFileName + (typeof diff.newHeader === 'undefined' ? '' : '\t' + diff.newHeader)); for (var i = 0; i < diff.hunks.length; i++) { var hunk = diff.hunks[i]; ret.push('@@ -' + hunk.oldStart + ',' + hunk.oldLines + ' +' + hunk.newStart + ',' + hunk.newLines + ' @@'); ret.push.apply(ret, hunk.lines); } return ret.join('\n') + '\n'; } function createPatch(fileName, oldStr, newStr, oldHeader, newHeader, options) { return createTwoFilesPatch(fileName, fileName, oldStr, newStr, oldHeader, newHeader, options); } }); unwrapExports(create); var dmp = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ "use strict"; exports.__esModule = true; exports. /*istanbul ignore end*/ convertChangesToDMP = convertChangesToDMP; // See: http://code.google.com/p/google-diff-match-patch/wiki/API function convertChangesToDMP(changes) { var ret = [], change = /*istanbul ignore start*/ void 0 /*istanbul ignore end*/ , operation = /*istanbul ignore start*/ void 0; for (var i = 0; i < changes.length; i++) { change = changes[i]; if (change.added) { operation = 1; } else if (change.removed) { operation = -1; } else { operation = 0; } ret.push([operation, change.value]); } return ret; } }); unwrapExports(dmp); var xml = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports. /*istanbul ignore end*/ convertChangesToXML = convertChangesToXML; function convertChangesToXML(changes) { var ret = []; for (var i = 0; i < changes.length; i++) { var change = changes[i]; if (change.added) { ret.push('<ins>'); } else if (change.removed) { ret.push('<del>'); } ret.push(escapeHTML(change.value)); if (change.added) { ret.push('</ins>'); } else if (change.removed) { ret.push('</del>'); } } return ret.join(''); } function escapeHTML(s) { var n = s; n = n.replace(/&/g, '&amp;'); n = n.replace(/</g, '&lt;'); n = n.replace(/>/g, '&gt;'); n = n.replace(/"/g, '&quot;'); return n; } }); unwrapExports(xml); var lib = createCommonjsModule(function (module, exports) { /*istanbul ignore start*/ 'use strict'; exports.__esModule = true; exports.canonicalize = exports.convertChangesToXML = exports.convertChangesToDMP = exports.parsePatch = exports.applyPatches = exports.applyPatch = exports.createPatch = exports.createTwoFilesPatch = exports.structuredPatch = exports.diffArrays = exports.diffJson = exports.diffCss = exports.diffSentences = exports.diffTrimmedLines = exports.diffLines = exports.diffWordsWithSpace = exports.diffWords = exports.diffChars = exports.Diff = undefined; /*istanbul ignore end*/ /*istanbul ignore start*/ var _base2 = _interopRequireDefault(base); /*istanbul ignore end*/ /*istanbul ignore start*/ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } exports. /*istanbul ignore end*/ Diff = _base2['default']; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ diffChars = character.diffChars; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ diffWords = word.diffWords; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ diffWordsWithSpace = word.diffWordsWithSpace; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ diffLines = line.diffLines; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ diffTrimmedLines = line.diffTrimmedLines; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ diffSentences = sentence.diffSentences; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ diffCss = css.diffCss; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ diffJson = json.diffJson; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ diffArrays = array.diffArrays; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ structuredPatch = create.structuredPatch; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ createTwoFilesPatch = create.createTwoFilesPatch; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ createPatch = create.createPatch; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ applyPatch = apply.applyPatch; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ applyPatches = apply.applyPatches; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ parsePatch = parse.parsePatch; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ convertChangesToDMP = dmp.convertChangesToDMP; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ convertChangesToXML = xml.convertChangesToXML; /*istanbul ignore start*/ exports. /*istanbul ignore end*/ canonicalize = json.canonicalize; /* See LICENSE file for terms of use */ /* * Text diff implementation. * * This library supports the following APIS: * JsDiff.diffChars: Character by character diff * JsDiff.diffWords: Word (as defined by \b regex) diff which ignores whitespace * JsDiff.diffLines: Line based diff * * JsDiff.diffCss: Diff targeted at CSS content * * These methods are based on the implementation proposed in * "An O(ND) Difference Algorithm and its Variations" (Myers, 1986). * http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927 */ }); unwrapExports(lib); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _inherits(