textlint-util-to-string
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -14,2 +14,6 @@ // LICENSE : MIT | ||
var _structuredSource = require("structured-source"); | ||
var _structuredSource2 = _interopRequireDefault(_structuredSource); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -28,2 +32,4 @@ | ||
this._stringify(this.rootNode); | ||
this.originalSource = new _structuredSource2.default(this.rootNode.raw); | ||
this.generatedSource = new _structuredSource2.default(this.generatedString); | ||
/* | ||
@@ -54,3 +60,3 @@ [ | ||
* Pass index value and return original index value. | ||
* @param {number} position - position is a index value. | ||
* @param {number} targetIndex - position is a index value. | ||
* @returns {number|undefined} original | ||
@@ -60,7 +66,7 @@ */ | ||
}, { | ||
key: "originalPositionFor", | ||
value: function originalPositionFor(position) { | ||
key: "originalIndexFor", | ||
value: function originalIndexFor(targetIndex) { | ||
var hitTokenMaps = this.tokenMaps.filter(function (tokenMap) { | ||
var generated = tokenMap.generated; | ||
if (generated[0] <= position && position < generated[1]) { | ||
if (generated[0] <= targetIndex && targetIndex < generated[1]) { | ||
return true; | ||
@@ -82,3 +88,3 @@ } | ||
// position-generated intermediate-origin | ||
var outAdjust = position - hitTokenMap.generated[0]; | ||
var outAdjust = targetIndex - hitTokenMap.generated[0]; | ||
var inAdjust = hitTokenMap.intermediate[0] - hitTokenMap.original[0]; | ||
@@ -88,2 +94,16 @@ return outAdjust + inAdjust + hitTokenMap.original[0]; | ||
}, { | ||
key: "originalPositionFor", | ||
value: function originalPositionFor(position) { | ||
if (typeof position.line === "undefined" || typeof position.column === "undefined") { | ||
throw new Error("position.{line, column} should not undefined: " + JSON.stringify(position)); | ||
} | ||
var generatedIndex = this.generatedSource.positionToIndex(position); | ||
if (isNaN(generatedIndex)) { | ||
// Not Found | ||
return; | ||
} | ||
var originalIndex = this.originalIndexFor(generatedIndex); | ||
return this.originalSource.indexToPosition(originalIndex); | ||
} | ||
}, { | ||
key: "isParagraphNode", | ||
@@ -192,3 +212,5 @@ value: function isParagraphNode(node) { | ||
var tokenMap = _this._stringify(childNode, node); | ||
_this._addTokenMap(tokenMap); | ||
if (tokenMap) { | ||
_this._addTokenMap(tokenMap); | ||
} | ||
}); | ||
@@ -195,0 +217,0 @@ } |
@@ -14,3 +14,3 @@ { | ||
}, | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "textlint util convert Paragraph Node to text with SourceMap.", | ||
@@ -40,4 +40,5 @@ "main": "lib/StringSource.js", | ||
"dependencies": { | ||
"object-assign": "^4.0.1" | ||
"object-assign": "^4.0.1", | ||
"structured-source": "^3.0.2" | ||
} | ||
} |
// LICENSE : MIT | ||
"use strict"; | ||
import ObjectAssign from "object-assign"; | ||
import StructuredSource from "structured-source"; | ||
export default class StringSource { | ||
@@ -11,2 +12,4 @@ constructor(node) { | ||
this._stringify(this.rootNode); | ||
this.originalSource = new StructuredSource(this.rootNode.raw); | ||
this.generatedSource = new StructuredSource(this.generatedString); | ||
/* | ||
@@ -35,9 +38,9 @@ [ | ||
* Pass index value and return original index value. | ||
* @param {number} position - position is a index value. | ||
* @param {number} targetIndex - position is a index value. | ||
* @returns {number|undefined} original | ||
*/ | ||
originalPositionFor(position) { | ||
originalIndexFor(targetIndex) { | ||
let hitTokenMaps = this.tokenMaps.filter(tokenMap => { | ||
let generated = tokenMap.generated; | ||
if (generated[0] <= position && position < generated[1]) { | ||
if (generated[0] <= targetIndex && targetIndex < generated[1]) { | ||
return true; | ||
@@ -59,3 +62,3 @@ } | ||
// position-generated intermediate-origin | ||
let outAdjust = position - hitTokenMap.generated[0]; | ||
let outAdjust = targetIndex - hitTokenMap.generated[0]; | ||
let inAdjust = hitTokenMap.intermediate[0] - hitTokenMap.original[0]; | ||
@@ -65,3 +68,16 @@ return outAdjust + inAdjust + hitTokenMap.original[0]; | ||
originalPositionFor(position) { | ||
if (typeof position.line === "undefined" || typeof position.column === "undefined") { | ||
throw new Error("position.{line, column} should not undefined: " + JSON.stringify(position)); | ||
} | ||
var generatedIndex = this.generatedSource.positionToIndex(position); | ||
if (isNaN(generatedIndex)) { | ||
// Not Found | ||
return; | ||
} | ||
let originalIndex = this.originalIndexFor(generatedIndex); | ||
return this.originalSource.indexToPosition(originalIndex); | ||
} | ||
isParagraphNode(node) { | ||
@@ -165,5 +181,7 @@ return node.type === "Paragraph"; | ||
let tokenMap = this._stringify(childNode, node); | ||
this._addTokenMap(tokenMap); | ||
if (tokenMap) { | ||
this._addTokenMap(tokenMap); | ||
} | ||
}); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26964
366
2
+ Addedstructured-source@^3.0.2
+ Addedboundary@1.0.1(transitive)
+ Addedstructured-source@3.0.2(transitive)