UNPKG

canonical

Version:

Canonical code style linter and formatter for JavaScript, SCSS and CSS.

1 lines 2.36 kB
'use strict';var TokenType=require('../token-types');module.exports = (function(){function markSC(tokens){var tokensLength=tokens.length, ws=-1, sc=-1, t;for(var i=0; i < tokensLength; i++) {t = tokens[i];switch(t.type){case TokenType.Space:case TokenType.Tab:t.ws = true;t.sc = true;if(ws === -1)ws = i;if(sc === -1)sc = i;break;case TokenType.Newline:t.ws = true;t.sc = true;ws = ws === -1?i:ws;sc = sc === -1?i:ws;tokens[ws].ws_last = i - 1;tokens[sc].sc_last = i - 1;tokens[i].ws_last = i;tokens[i].sc_last = i;ws = -1;sc = -1;break;case TokenType.CommentML:case TokenType.CommentSL:if(ws !== -1){tokens[ws].ws_last = i - 1;ws = -1;}t.sc = true;break;default:if(ws !== -1){tokens[ws].ws_last = i - 1;ws = -1;}if(sc !== -1){tokens[sc].sc_last = i - 1;sc = -1;}}}if(ws !== -1)tokens[ws].ws_last = i - 1;if(sc !== -1)tokens[sc].sc_last = i - 1;}function markBrackets(tokens){var tokensLength=tokens.length;var ps=[], sbs=[], cbs=[], t;for(var i=0; i < tokensLength; i++) {t = tokens[i];switch(t.type){case TokenType.LeftParenthesis:ps.push(i);break;case TokenType.RightParenthesis:if(ps.length){t.left = ps.pop();tokens[t.left].right = i;}break;case TokenType.LeftSquareBracket:sbs.push(i);break;case TokenType.RightSquareBracket:if(sbs.length){t.left = sbs.pop();tokens[t.left].right = i;}break;case TokenType.LeftCurlyBracket:cbs.push(i);break;case TokenType.RightCurlyBracket:if(cbs.length){t.left = cbs.pop();tokens[t.left].right = i;}break;}}}function markBlocks(tokens){var blocks=[], currentLN=1, currentIL=0, prevIL=0, i=0, l=tokens.length, iw;for(; i != l; i++) {if(!tokens[i - 1])continue;if(tokens[i].ln == currentLN)continue;else currentLN = tokens[i].ln;prevIL = currentIL;if(tokens[i].type === TokenType.Newline)continue;else if(tokens[i].type === TokenType.Space && tokens[i + 1] && tokens[i + 1].type === TokenType.Newline)continue;else if(tokens[i].type !== TokenType.Space)currentIL = 0;else {if(!iw)iw = tokens[i].value.length;prevIL = currentIL;currentIL = tokens[i].value.length / iw;}if(prevIL === currentIL)continue;else if(currentIL > prevIL){blocks.push(i);continue;}else {var il=prevIL;while(blocks.length > 0 && il !== currentIL) {tokens[blocks.pop()].block_end = i - 1;il--;}}}while(blocks.length > 0) {tokens[blocks.pop()].block_end = i - 1;}}return function(tokens){markBrackets(tokens);markSC(tokens);markBlocks(tokens);};})();