@react-pug/pug-lexer
Advanced tools
+87
-12
@@ -33,2 +33,19 @@ 'use strict'; | ||
| function hasOpenJavaScriptSyntax(str) { | ||
| var state = characterParser.default(str); | ||
| return state.isNesting() || state.isString(); | ||
| } | ||
| function endsWithJavaScriptContinuation(str) { | ||
| var trimmed = str.replace(/[ \t]+$/, ''); | ||
| return /(?:=>|[([{,:?+\-*/%&|^=!<>]|\b(?:as|satisfies|in|instanceof)\b)$/.test(trimmed); | ||
| } | ||
| function startsWithJavaScriptContinuationLine(str) { | ||
| var trimmed = str.trim(); | ||
| if (!trimmed) return false; | ||
| return /^(?:\?|:|&&|\|\||\?\?|as\b|satisfies\b|instanceof\b|in\b)/.test(trimmed); | ||
| } | ||
| /** | ||
@@ -131,2 +148,44 @@ * Initialize `Lexer` with the given `str`. | ||
| incrementPositionByText: function(text) { | ||
| var lines = text.split('\n'); | ||
| var newlineCount = lines.length - 1; | ||
| if (newlineCount === 0) { | ||
| this.incrementColumn(text.length); | ||
| return; | ||
| } | ||
| this.incrementLine(newlineCount); | ||
| this.incrementColumn(lines[newlineCount].length); | ||
| }, | ||
| collectMultilineCodeFragment: function(startIndex, initialCode) { | ||
| var consumed = startIndex + initialCode.length; | ||
| var code = initialCode; | ||
| while (consumed < this.input.length && this.input[consumed] === '\n') { | ||
| var nextLineStart = consumed + 1; | ||
| var nextLineEnd = this.input.indexOf('\n', nextLineStart); | ||
| if (nextLineEnd === -1) { | ||
| nextLineEnd = this.input.length; | ||
| } | ||
| var nextLine = this.input.slice(nextLineStart, nextLineEnd); | ||
| var shouldContinue = | ||
| hasOpenJavaScriptSyntax(code) || | ||
| endsWithJavaScriptContinuation(code) || | ||
| startsWithJavaScriptContinuationLine(nextLine); | ||
| if (!shouldContinue) { | ||
| break; | ||
| } | ||
| code += this.input.slice(consumed, nextLineEnd); | ||
| consumed = nextLineEnd; | ||
| } | ||
| return { | ||
| code: code, | ||
| consumed: consumed, | ||
| }; | ||
| }, | ||
| /** | ||
@@ -624,2 +683,3 @@ * Construct a token with the given `type` and `val`. | ||
| var rest = matchOfStringInterp[3]; | ||
| var fullRest = rest + this.input; | ||
| var range; | ||
@@ -629,3 +689,3 @@ tok = this.tok('interpolated-code'); | ||
| try { | ||
| range = characterParser.parseUntil(rest, '}'); | ||
| range = characterParser.parseUntil(fullRest, '}'); | ||
| } catch (ex) { | ||
@@ -650,12 +710,21 @@ if (ex.index !== undefined) { | ||
| this.assertExpression(range.src); | ||
| var consumedFromRest = range.end + 1; | ||
| var extraInputConsumed = Math.max(0, consumedFromRest - rest.length); | ||
| var tailInValue = | ||
| consumedFromRest < rest.length ? rest.substr(consumedFromRest) : ''; | ||
| if (range.end + 1 < rest.length) { | ||
| rest = rest.substr(range.end + 1); | ||
| this.incrementColumn(range.end + 1); | ||
| this.tokens.push(this.tokEnd(tok)); | ||
| this.addText(type, rest); | ||
| } else { | ||
| this.incrementColumn(rest.length); | ||
| this.tokens.push(this.tokEnd(tok)); | ||
| this.input = this.input.substr(extraInputConsumed); | ||
| this.incrementPositionByText(range.src); | ||
| this.incrementColumn(1); | ||
| this.tokens.push(this.tokEnd(tok)); | ||
| var tailFromInput = ''; | ||
| if (this.input.length && this.input[0] !== '\n' && this.input[0] !== ']') { | ||
| tailFromInput = /^[^\n\]]*/.exec(this.input)[0]; | ||
| this.consume(tailFromInput.length); | ||
| } | ||
| if (tailInValue || tailFromInput) { | ||
| this.addText(type, tailInValue + tailFromInput); | ||
| } | ||
| return; | ||
@@ -1161,2 +1230,4 @@ } | ||
| var shortened = 0; | ||
| var prefixLength = captures[0].length - captures[2].length; | ||
| var consumed = captures[0].length; | ||
| if (this.interpolated) { | ||
@@ -1183,4 +1254,8 @@ var parsed; | ||
| code = parsed.src; | ||
| consumed -= shortened; | ||
| } else if (flags.charAt(0) === '=' || flags.charAt(1) === '=') { | ||
| var collected = this.collectMultilineCodeFragment(prefixLength, code); | ||
| code = collected.code; | ||
| consumed = collected.consumed; | ||
| } | ||
| var consumed = captures[0].length - shortened; | ||
| this.consume(consumed); | ||
@@ -1204,3 +1279,3 @@ var tok = this.tok('code', code); | ||
| // ^ after colno | ||
| this.incrementColumn(captures[0].length - captures[2].length); | ||
| this.incrementColumn(prefixLength); | ||
| if (tok.buffer) this.assertExpression(code); | ||
@@ -1220,3 +1295,3 @@ this.tokens.push(tok); | ||
| // ^ after colno | ||
| this.incrementColumn(code.length); | ||
| this.incrementPositionByText(code); | ||
| this.tokEnd(tok); | ||
@@ -1223,0 +1298,0 @@ return true; |
+2
-2
| { | ||
| "name": "@react-pug/pug-lexer", | ||
| "version": "0.1.7", | ||
| "version": "0.1.17", | ||
| "publishConfig": { | ||
@@ -35,3 +35,3 @@ "access": "public" | ||
| "license": "MIT", | ||
| "gitHead": "1542e65097612bb6aef497ce6080784e2fa7950c" | ||
| "gitHead": "ffa9ba20c16242d791fc4c6579fc695051ae54b6" | ||
| } |
63997
3.95%1998
3.2%