| 1 |
import { locate } from 'locate-character'; |
| 2 |
|
| 3 |
const keywords = /\b(case|default|delete|do|else|in|instanceof|new|return|throw|typeof|void)\s*$/; |
| 4 |
const punctuators = /(^|\{|\(|\[\.|;|,|<|>|<=|>=|==|!=|===|!==|\+|-|\*\%|<<|>>|>>>|&|\||\^|!|~|&&|\|\||\?|:|=|\+=|-=|\*=|%=|<<=|>>=|>>>=|&=|\|=|\^=|\/=|\/)\s*$/; |
| 5 |
const ambiguous = /(\}|\)|\+\+|--)\s*$/; |
| 6 |
|
| 7 |
const punctuatorChars = /[{}()[.;,<>=+\-*%&|\^!~?:/]/; |
| 8 |
const keywordChars = /[a-z]/; |
| 9 |
const beforeJsxChars = /[=:;,({}[&+]/; |
| 10 |
|
| 11 |
const whitespace = /\s/; |
| 12 |
|
| 13 |
function isWhitespace ( char ) { |
| 14 |
return whitespace.test( char ); |
| 15 |
} |
| 16 |
|
| 17 |
function isPunctuatorChar ( char ) { |
| 18 |
return punctuatorChars.test( char ); |
| 19 |
} |
| 20 |
|
| 21 |
function isKeywordChar ( char ) { |
| 22 |
return keywordChars.test( char ); |
| 23 |
} |
| 24 |
|
| 25 |
function isPunctuator ( str ) { |
| 26 |
return punctuators.test( str ); |
| 27 |
} |
| 28 |
|
| 29 |
function isKeyword ( str ) { |
| 30 |
return keywords.test( str ); |
| 31 |
} |
| 32 |
|
| 33 |
function isAmbiguous ( str ) { |
| 34 |
return ambiguous.test( str ); |
| 35 |
} |
| 36 |
|
| 37 |
export function find ( str ) { |
| 38 |
let quote; |
| 39 |
let escapedFrom; |
| 40 |
let regexEnabled = true; |
| 41 |
let pfixOp = false; |
| 42 |
let jsxTagDepth = 0; |
| 43 |
let stack = []; |
| 44 |
|
| 45 |
let start; |
| 46 |
let found = []; |
| 47 |
let state = base; |
| 48 |
|
| 49 |
let lsci = -1; // last significant character index |
| 50 |
const lsc = () => str[ lsci ]; |
| 51 |
|
| 52 |
const parenMatches = {}; |
| 53 |
const openingParenPositions = {}; |
| 54 |
let parenDepth = 0; |
| 55 |
|
| 56 |
function tokenClosesExpression () { |
| 57 |
if ( lsc() === ')' ) { |
| 58 |
let c = parenMatches[ lsci ]; |
| 59 |
while ( isWhitespace( str[ c - 1 ] ) ) c -= 1; |
| 60 |
|
| 61 |
// if parenthesized expression is immediately preceded by `if`/`while`, it's not closing an expression |
| 62 |
return !/(if|while)$/.test( str.slice( c - 5, c ) ); |
| 63 |
} |
| 64 |
|
| 65 |
// TODO handle }, ++ and -- tokens immediately followed by / character |
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
function base ( char, i ) { |
| 70 |
// the order of these tests is based on which characters are |
| 71 |
// typically more prevalent in a codebase |
| 72 |
if ( char === '(' ) { |
| 73 |
lsci = i; |
| 74 |
openingParenPositions[ parenDepth++ ] = i; |
| 75 |
return base; |
| 76 |
} |
| 77 |
|
| 78 |
if ( char === ')' ) { |
| 79 |
lsci = i; |
| 80 |
parenMatches[i] = openingParenPositions[ --parenDepth ]; |
| 81 |
return base; |
| 82 |
} |
| 83 |
|
| 84 |
if ( char === '{' ) { |
| 85 |
lsci = i; |
| 86 |
stack.push( base ); |
| 87 |
return base; |
| 88 |
} |
| 89 |
|
| 90 |
if ( char === '}' ) { |
| 91 |
lsci = i; |
| 92 |
return start = i + 1, stack.pop(); |
| 93 |
} |
| 94 |
|
| 95 |
if ( char === '"' || char === "'" ) { |
| 96 |
start = i + 1; |
| 97 |
quote = char; |
| 98 |
stack.push( base ); |
| 99 |
return string; |
| 100 |
} |
| 101 |
|
| 102 |
if ( char === '/' ) { |
| 103 |
// could be start of regex literal OR division punctuator. Solution via |
| 104 |
// http://stackoverflow.com/questions/5519596/when-parsing-javascript-what-determines-the-meaning-of-a-slash/27120110#27120110 |
| 105 |
|
| 106 |
let b = i; |
| 107 |
while ( b > 0 && isWhitespace( str[ b - 1 ] ) ) b -= 1; |
| 108 |
|
| 109 |
if ( b > 0 ) { |
| 110 |
let a = b; |
| 111 |
|
| 112 |
if ( isPunctuatorChar( str[ a - 1 ] ) ) { |
| 113 |
while ( a > 0 && isPunctuatorChar( str[ a - 1 ] ) ) a -= 1; |
| 114 |
} else { |
| 115 |
while ( a > 0 && isKeywordChar( str[ a - 1 ] ) ) a -= 1; |
| 116 |
} |
| 117 |
|
| 118 |
const token = str.slice( a, b ); |
| 119 |
|
| 120 |
regexEnabled = token ? ( |
| 121 |
isKeyword( token ) || |
| 122 |
isPunctuator( token ) || |
| 123 |
( isAmbiguous( token ) && !tokenClosesExpression() ) |
| 124 |
) : false; |
| 125 |
} else { |
| 126 |
regexEnabled = true; |
| 127 |
} |
| 128 |
|
| 129 |
start = i; |
| 130 |
return slash; |
| 131 |
} |
| 132 |
|
| 133 |
if ( char === '`' ) { |
| 134 |
start = i + 1; |
| 135 |
return templateString; |
| 136 |
} |
| 137 |
|
| 138 |
if ( char === '<' && ( !~lsci || beforeJsxChars.test( lsc() ) ) ) { |
| 139 |
stack.push( base ); |
| 140 |
return jsxTagStart; |
| 141 |
} |
| 142 |
|
| 143 |
if ( char === '+' && !pfixOp && str[ i - 1 ] === '+' ) { |
| 144 |
pfixOp = true; |
| 145 |
} else if ( char === '-' && !pfixOp && str[ i - 1 ] === '-' ) { |
| 146 |
pfixOp = true; |
| 147 |
} |
| 148 |
|
| 149 |
if ( !isWhitespace( char ) ) lsci = i; |
| 150 |
return base; |
| 151 |
} |
| 152 |
|
| 153 |
function slash ( char, i ) { |
| 154 |
if ( char === '/' ) return start = i + 1, lineComment; |
| 155 |
if ( char === '*' ) return start = i + 1, blockComment; |
| 156 |
if ( char === '[' ) return regexEnabled ? ( start = i, regexCharacter ) : base; |
| 157 |
if ( char === '\\' ) return start = i, escapedFrom = regex, escaped; |
| 158 |
return regexEnabled && !pfixOp ? ( start = i, regex ) : base; |
| 159 |
} |
| 160 |
|
| 161 |
function regex ( char, i ) { |
| 162 |
if ( char === '[' ) return regexCharacter; |
| 163 |
if ( char === '\\' ) return escapedFrom = regex, escaped; |
| 164 |
|
| 165 |
if ( char === '/' ) { |
| 166 |
const end = i; |
| 167 |
const value = str.slice( start, end ); |
| 168 |
|
| 169 |
found.push({ start, end, value, type: 'regex' }); |
| 170 |
|
| 171 |
return base; |
| 172 |
} |
| 173 |
|
| 174 |
return regex; |
| 175 |
} |
| 176 |
|
| 177 |
function regexCharacter ( char ) { |
| 178 |
if ( char === ']' ) return regex; |
| 179 |
if ( char === '\\' ) return escapedFrom = regexCharacter, escaped; |
| 180 |
return regexCharacter; |
| 181 |
} |
| 182 |
|
| 183 |
function string ( char, i ) { |
| 184 |
if ( char === '\\' ) return escapedFrom = string, escaped; |
| 185 |
if ( char === quote ) { |
| 186 |
const end = i; |
| 187 |
const value = str.slice( start, end ); |
| 188 |
|
| 189 |
found.push({ start, end, value, type: 'string' }); |
| 190 |
|
| 191 |
return stack.pop(); |
| 192 |
} |
| 193 |
|
| 194 |
return string; |
| 195 |
} |
| 196 |
|
| 197 |
function escaped () { |
| 198 |
return escapedFrom; |
| 199 |
} |
| 200 |
|
| 201 |
function templateString ( char, i ) { |
| 202 |
if ( char === '$' ) return templateStringDollar; |
| 203 |
if ( char === '\\' ) return escapedFrom = templateString, escaped; |
| 204 |
|
| 205 |
if ( char === '`' ) { |
| 206 |
const end = i; |
| 207 |
const value = str.slice( start, end ); |
| 208 |
|
| 209 |
found.push({ start, end, value, type: 'templateEnd' }); |
| 210 |
|
| 211 |
return base; |
| 212 |
} |
| 213 |
|
| 214 |
return templateString; |
| 215 |
} |
| 216 |
|
| 217 |
function templateStringDollar ( char, i ) { |
| 218 |
if ( char === '{' ) { |
| 219 |
const end = i - 1; |
| 220 |
const value = str.slice( start, end ); |
| 221 |
|
| 222 |
found.push({ start, end, value, type: 'templateChunk' }); |
| 223 |
|
| 224 |
stack.push( templateString ); |
| 225 |
return base; |
| 226 |
} |
| 227 |
return templateString( char, i ); |
| 228 |
} |
| 229 |
|
| 230 |
// JSX is an XML-like extension to ECMAScript |
| 231 |
// https://facebook.github.io/jsx/ |
| 232 |
|
| 233 |
function jsxTagStart ( char ) { |
| 234 |
if ( char === '/' ) return jsxTagDepth--, jsxTag; |
| 235 |
return jsxTagDepth++, jsxTag; |
| 236 |
} |
| 237 |
|
| 238 |
function jsxTag ( char, i ) { |
| 239 |
if ( char === '"' || char === "'" ) return start = i + 1, quote = char, stack.push( jsxTag ), string; |
| 240 |
if ( char === '{' ) return stack.push( jsxTag ), base; |
| 241 |
if ( char === '>' ) { |
| 242 |
if ( jsxTagDepth <= 0 ) return base; |
| 243 |
start = i + 1; |
| 244 |
return jsx; |
| 245 |
} |
| 246 |
if ( char === '/' ) return jsxTagSelfClosing; |
| 247 |
|
| 248 |
return jsxTag; |
| 249 |
} |
| 250 |
|
| 251 |
function jsxTagSelfClosing ( char ) { |
| 252 |
if ( char === '>' ) { |
| 253 |
jsxTagDepth--; |
| 254 |
if ( jsxTagDepth <= 0 ) return base; |
| 255 |
return jsx; |
| 256 |
} |
| 257 |
|
| 258 |
return jsxTag; |
| 259 |
} |
| 260 |
|
| 261 |
function jsx ( char, end ) { |
| 262 |
if ( char === '{' || char === '<' ) { |
| 263 |
const value = str.slice( start, end ); |
| 264 |
found.push({ start, end, value, type: 'jsx' }); |
| 265 |
|
| 266 |
if ( char === '{' ) { |
| 267 |
return stack.push( jsx ), base; |
| 268 |
} |
| 269 |
|
| 270 |
if ( char === '<' ) { |
| 271 |
return jsxTagStart; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
return jsx; |
| 276 |
} |
| 277 |
|
| 278 |
function lineComment ( char, end ) { |
| 279 |
if ( char === '\n' ) { |
| 280 |
const value = str.slice( start, end ); |
| 281 |
|
| 282 |
found.push({ start, end, value, type: 'line' }); |
| 283 |
|
| 284 |
return base; |
| 285 |
} |
| 286 |
|
| 287 |
return lineComment; |
| 288 |
} |
| 289 |
|
| 290 |
function blockComment ( char ) { |
| 291 |
if ( char === '*' ) return blockCommentEnding; |
| 292 |
return blockComment; |
| 293 |
} |
| 294 |
|
| 295 |
function blockCommentEnding ( char, i ) { |
| 296 |
if ( char === '/' ) { |
| 297 |
const end = i - 1; |
| 298 |
const value = str.slice( start, end ); |
| 299 |
|
| 300 |
found.push({ start, end, value, type: 'block' }); |
| 301 |
|
| 302 |
return base; |
| 303 |
} |
| 304 |
|
| 305 |
return blockComment( char ); |
| 306 |
} |
| 307 |
|
| 308 |
for ( let i = 0; i < str.length; i += 1 ) { |
| 309 |
if ( !state ) { |
| 310 |
const { line, column } = locate( str, i, { offsetLine: 1 }); |
| 311 |
const before = str.slice( 0, i ); |
| 312 |
const beforeLine = /(^|\n).+$/.exec( before )[0]; |
| 313 |
const after = str.slice( i ); |
| 314 |
const afterLine = /.+(\n|$)/.exec( after )[0]; |
| 315 |
|
| 316 |
const snippet = `${beforeLine}${afterLine}\n${ Array( beforeLine.length + 1 ).join( ' ' )}^`; |
| 317 |
|
| 318 |
throw new Error( `Unexpected character (${line}:${column}). If this is valid JavaScript, it's probably a bug in tippex. Please raise an issue at https://github.com/Rich-Harris/tippex/issues – thanks!\n\n${snippet}` ); |
| 319 |
} |
| 320 |
|
| 321 |
state = state( str[i], i ); |
| 322 |
} |
| 323 |
|
| 324 |
// cheeky hack |
| 325 |
if ( state.name === 'lineComment' ) state( '\n', str.length ); |
| 326 |
|
| 327 |
return found; |
| 328 |
} |
| 329 |
|
| 330 |
export function erase ( str ) { |
| 331 |
const found = find( str ); |
| 332 |
return _erase( str, found ); |
| 333 |
} |
| 334 |
|
| 335 |
function _erase ( str, found ) { |
| 336 |
let erased = ''; |
| 337 |
let charIndex = 0; |
| 338 |
|
| 339 |
for ( let i = 0; i < found.length; i += 1 ) { |
| 340 |
const chunk = found[i]; |
| 341 |
erased += str.slice( charIndex, chunk.start ); |
| 342 |
erased += chunk.value.replace( /[^\n]/g, ' ' ); |
| 343 |
|
| 344 |
charIndex = chunk.end; |
| 345 |
} |
| 346 |
|
| 347 |
erased += str.slice( charIndex ); |
| 348 |
return erased; |
| 349 |
} |
| 350 |
|
| 351 |
function makeGlobalRegExp ( original ) { |
| 352 |
let flags = 'g'; |
| 353 |
|
| 354 |
if ( original.multiline ) flags += 'm'; |
| 355 |
if ( original.ignoreCase ) flags += 'i'; |
| 356 |
if ( original.sticky ) flags += 'y'; |
| 357 |
if ( original.unicode ) flags += 'u'; |
| 358 |
|
| 359 |
return new RegExp( original.source, flags ); |
| 360 |
} |
| 361 |
|
| 362 |
export function match ( str, pattern, callback ) { |
| 363 |
const g = pattern.global; |
| 364 |
if ( !g ) pattern = makeGlobalRegExp( pattern ); |
| 365 |
|
| 366 |
const found = find( str ); |
| 367 |
|
| 368 |
let match; |
| 369 |
let chunkIndex = 0; |
| 370 |
|
| 371 |
while ( match = pattern.exec( str ) ) { |
| 372 |
let chunk; |
| 373 |
|
| 374 |
do { |
| 375 |
chunk = found[ chunkIndex ]; |
| 376 |
|
| 377 |
if ( chunk && chunk.end < match.index ) { |
| 378 |
chunkIndex += 1; |
| 379 |
} else { |
| 380 |
break; |
| 381 |
} |
| 382 |
} while ( chunk ); |
| 383 |
|
| 384 |
if ( !chunk || chunk.start > match.index ) { |
| 385 |
const args = [].slice.call( match ).concat( match.index, str ); |
| 386 |
callback.apply( null, args ); |
| 387 |
if ( !g ) break; |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
export function replace ( str, pattern, callback ) { |
| 393 |
let replacements = []; |
| 394 |
|
| 395 |
match( str, pattern, function ( match ) { |
| 396 |
const start = arguments[ arguments.length - 2 ]; |
| 397 |
const end = start + match.length; |
| 398 |
const content = callback.apply( null, arguments ); |
| 399 |
|
| 400 |
replacements.push({ start, end, content }); |
| 401 |
}); |
| 402 |
|
| 403 |
let replaced = ''; |
| 404 |
let lastIndex = 0; |
| 405 |
|
| 406 |
for ( let i = 0; i < replacements.length; i += 1 ) { |
| 407 |
const { start, end, content } = replacements[i]; |
| 408 |
replaced += str.slice( lastIndex, start ) + content; |
| 409 |
|
| 410 |
lastIndex = end; |
| 411 |
} |
| 412 |
|
| 413 |
replaced += str.slice( lastIndex ); |
| 414 |
|
| 415 |
return replaced; |
| 416 |
} |