@@ -33,7 +33,7 @@ function parseRuleNames(text, enabled) {
3333 * @param {Element } firstToken
3434 * @constructor
3535 */
36- function PragmaIndex ( firstToken ) {
36+ function TokenIndex ( firstToken ) {
3737 this . _buildIndex ( firstToken ) ;
3838}
3939
@@ -43,18 +43,36 @@ function PragmaIndex(firstToken) {
4343 * @param {Element } firstToken
4444 * @private
4545 */
46- PragmaIndex . prototype . _buildIndex = function ( firstToken ) {
46+ TokenIndex . prototype . _buildIndex = function ( firstToken ) {
4747 this . _hasPragmas = false ;
4848
4949 var tokens = [ ] ;
5050 var index = [ ] ;
51+ var positions = [ ] ;
5152 var currentPosition = 0 ;
5253 var currentToken = firstToken ;
5354 var lastBlockState = { '*' : true } ;
5455 var tokenState ;
56+ var previousLoc = { line : 1 , column : 0 } ;
5557
5658 while ( currentToken ) {
5759 tokens . push ( currentToken ) ;
60+ currentToken . __loc = previousLoc ;
61+
62+ var newlineCount = currentToken . getNewlineCount ( ) ;
63+ if ( newlineCount > 0 ) {
64+ var lines = currentToken . getSourceCodeLines ( ) ;
65+ previousLoc = {
66+ line : previousLoc . line + newlineCount ,
67+ column : lines [ lines . length - 1 ] . length
68+ } ;
69+ } else {
70+ previousLoc = {
71+ line : previousLoc . line ,
72+ column : previousLoc . column + currentToken . getSourceCodeLength ( )
73+ } ;
74+ }
75+
5876 if ( currentToken . isComment ) {
5977 var value = currentToken . value ;
6078 var blockMatch = BLOCK_REGEXP . exec ( value ) ;
@@ -106,6 +124,7 @@ PragmaIndex.prototype._buildIndex = function(firstToken) {
106124 }
107125 this . _tokens = tokens ;
108126 this . _index = index ;
127+ this . _positions = positions ;
109128} ;
110129
111130/**
@@ -115,7 +134,7 @@ PragmaIndex.prototype._buildIndex = function(firstToken) {
115134 * @param {Element } element
116135 * @returns {Boolean }
117136 */
118- PragmaIndex . prototype . isRuleEnabled = function ( ruleName , element ) {
137+ TokenIndex . prototype . isRuleEnabled = function ( ruleName , element ) {
119138 if ( ! this . _hasPragmas ) {
120139 return true ;
121140 }
@@ -132,5 +151,18 @@ PragmaIndex.prototype.isRuleEnabled = function(ruleName, element) {
132151 return true ;
133152} ;
134153
135- module . exports = PragmaIndex ;
154+ /**
155+ * Return calculated element location.
156+ *
157+ * @param {Element } element
158+ * @returns {Object }
159+ */
160+ TokenIndex . prototype . getElementLoc = function ( element ) {
161+ return element . getFirstToken ( ) . __loc || {
162+ line : 1 ,
163+ column : 0
164+ } ;
165+ } ;
166+
167+ module . exports = TokenIndex ;
136168
0 commit comments