canonical
Version:
Canonical code style linter and formatter for JavaScript, SCSS, CSS and JSON.
1 lines • 1.75 kB
JavaScript
;var TokenType=require('../token-types');function markSpacesAndComments(tokens){var tokensLength=tokens.length;var spaces=[-1, -1];var type;for(var i=0; i < tokensLength; i++) {type = tokens[i].type;if(type === TokenType.Space || type === TokenType.Tab || type === TokenType.Newline){markSpace(tokens, i, spaces);}else if(type === TokenType.CommentML){markComment(tokens, i, spaces);}else {markEndOfSpacesAndComments(tokens, i, spaces);}}markEndOfSpacesAndComments(tokens, i, spaces);}function markSpace(tokens, i, spaces){var token=tokens[i];token.ws = true;token.sc = true;if(spaces[0] === -1)spaces[0] = i;if(spaces[1] === -1)spaces[1] = i;}function markComment(tokens, i, spaces){var ws=spaces[0];tokens[i].sc = true;if(ws !== -1){tokens[ws].ws_last = i - 1;spaces[0] = -1;}}function markEndOfSpacesAndComments(tokens, i, spaces){var ws=spaces[0];var sc=spaces[1];if(ws !== -1){tokens[ws].ws_last = i - 1;spaces[0] = -1;}if(sc !== -1){tokens[sc].sc_last = i - 1;spaces[1] = -1;}}function markBrackets(tokens){var tokensLength=tokens.length;var ps=[], sbs=[], cbs=[], t;for(var i=0; i < tokensLength; i++) {t = tokens[i];var type=t.type;if(type === TokenType.LeftParenthesis){ps.push(i);}else if(type === TokenType.RightParenthesis){if(ps.length){t.left = ps.pop();tokens[t.left].right = i;}}else if(type === TokenType.LeftSquareBracket){sbs.push(i);}else if(type === TokenType.RightSquareBracket){if(sbs.length){t.left = sbs.pop();tokens[t.left].right = i;}}else if(type === TokenType.LeftCurlyBracket){cbs.push(i);}else if(type === TokenType.RightCurlyBracket){if(cbs.length){t.left = cbs.pop();tokens[t.left].right = i;}}}}function markTokens(tokens){markBrackets(tokens);markSpacesAndComments(tokens);}module.exports = markTokens;