|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * Check whether the given token is an arrow. |
| 5 | + * @param {Token|undefined|null} token The token to check. |
| 6 | + * @returns {boolean} `true` if the token is an arrow. |
| 7 | + */ |
| 8 | +function isArrow(token) { |
| 9 | + return token != null && token.type === 'Punctuator' && token.value === '=>' |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Check whether the given token is a left parenthesis. |
| 14 | + * @param {Token|undefined|null} token The token to check. |
| 15 | + * @returns {boolean} `true` if the token is a left parenthesis. |
| 16 | + */ |
| 17 | +function isLeftParen(token) { |
| 18 | + return token != null && token.type === 'Punctuator' && token.value === '(' |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Check whether the given token is a left parenthesis. |
| 23 | + * @param {Token|undefined|null} token The token to check. |
| 24 | + * @returns {boolean} `false` if the token is a left parenthesis. |
| 25 | + */ |
| 26 | +function isNotLeftParen(token) { |
| 27 | + return token != null && (token.type !== 'Punctuator' || token.value !== '(') |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Check whether the given token is a right parenthesis. |
| 32 | + * @param {Token|undefined|null} token The token to check. |
| 33 | + * @returns {boolean} `true` if the token is a right parenthesis. |
| 34 | + */ |
| 35 | +function isRightParen(token) { |
| 36 | + return token != null && token.type === 'Punctuator' && token.value === ')' |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Check whether the given token is a right parenthesis. |
| 41 | + * @param {Token|undefined|null} token The token to check. |
| 42 | + * @returns {boolean} `false` if the token is a right parenthesis. |
| 43 | + */ |
| 44 | +function isNotRightParen(token) { |
| 45 | + return token != null && (token.type !== 'Punctuator' || token.value !== ')') |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Check whether the given token is a left brace. |
| 50 | + * @param {Token|undefined|null} token The token to check. |
| 51 | + * @returns {boolean} `true` if the token is a left brace. |
| 52 | + */ |
| 53 | +function isLeftBrace(token) { |
| 54 | + return token != null && token.type === 'Punctuator' && token.value === '{' |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Check whether the given token is a right brace. |
| 59 | + * @param {Token|undefined|null} token The token to check. |
| 60 | + * @returns {boolean} `true` if the token is a right brace. |
| 61 | + */ |
| 62 | +function isRightBrace(token) { |
| 63 | + return token != null && token.type === 'Punctuator' && token.value === '}' |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Check whether the given token is a left bracket. |
| 68 | + * @param {Token|undefined|null} token The token to check. |
| 69 | + * @returns {boolean} `true` if the token is a left bracket. |
| 70 | + */ |
| 71 | +function isLeftBracket(token) { |
| 72 | + return token != null && token.type === 'Punctuator' && token.value === '[' |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Check whether the given token is a right bracket. |
| 77 | + * @param {Token|undefined|null} token The token to check. |
| 78 | + * @returns {boolean} `true` if the token is a right bracket. |
| 79 | + */ |
| 80 | +function isRightBracket(token) { |
| 81 | + return token != null && token.type === 'Punctuator' && token.value === ']' |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Check whether the given token is a semicolon. |
| 86 | + * @param {Token|undefined|null} token The token to check. |
| 87 | + * @returns {boolean} `true` if the token is a semicolon. |
| 88 | + */ |
| 89 | +function isSemicolon(token) { |
| 90 | + return token != null && token.type === 'Punctuator' && token.value === ';' |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Check whether the given token is a colon. |
| 95 | + * @param {Token|undefined|null} token The token to check. |
| 96 | + * @returns {boolean} `true` if the token is a colon. |
| 97 | + */ |
| 98 | +function isColon(token) { |
| 99 | + return token != null && token.type === 'Punctuator' && token.value === ':' |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Check whether the given token is a comma. |
| 104 | + * @param {Token|undefined|null} token The token to check. |
| 105 | + * @returns {boolean} `true` if the token is a comma. |
| 106 | + */ |
| 107 | +function isComma(token) { |
| 108 | + return token != null && token.type === 'Punctuator' && token.value === ',' |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Check whether the given token is a wildcard. |
| 113 | + * @param {Token|undefined|null} token The token to check. |
| 114 | + * @returns {boolean} `true` if the token is a wildcard. |
| 115 | + */ |
| 116 | +function isWildcard(token) { |
| 117 | + return token != null && token.type === 'Punctuator' && token.value === '*' |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Check whether the given token is a question. |
| 122 | + * @param {Token|undefined|null} token The token to check. |
| 123 | + * @returns {boolean} `true` if the token is a question. |
| 124 | + */ |
| 125 | +function isQuestion(token) { |
| 126 | + return token != null && token.type === 'Punctuator' && token.value === '?' |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Check whether the given token is an extends keyword. |
| 131 | + * @param {Token|undefined|null} token The token to check. |
| 132 | + * @returns {boolean} `true` if the token is an extends keywordn. |
| 133 | + */ |
| 134 | +function isExtendsKeyword(token) { |
| 135 | + return token != null && token.type === 'Keyword' && token.value === 'extends' |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Check whether the given token is a whitespace. |
| 140 | + * @param {Token|undefined|null} token The token to check. |
| 141 | + * @returns {boolean} `true` if the token is a whitespace. |
| 142 | + */ |
| 143 | +function isNotWhitespace(token) { |
| 144 | + return token != null && token.type !== 'HTMLWhitespace' |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * Check whether the given token is a comment. |
| 149 | + * @param {Token|undefined|null} token The token to check. |
| 150 | + * @returns {boolean} `true` if the token is a comment. |
| 151 | + */ |
| 152 | +function isComment(token) { |
| 153 | + return ( |
| 154 | + token != null && |
| 155 | + (token.type === 'Block' || |
| 156 | + token.type === 'Line' || |
| 157 | + token.type === 'Shebang' || |
| 158 | + (typeof token.type === |
| 159 | + 'string' /* Although acorn supports new tokens, espree may not yet support new tokens.*/ && |
| 160 | + token.type.endsWith('Comment'))) |
| 161 | + ) |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Check whether the given token is a comment. |
| 166 | + * @param {Token|undefined|null} token The token to check. |
| 167 | + * @returns {boolean} `false` if the token is a comment. |
| 168 | + */ |
| 169 | +function isNotComment(token) { |
| 170 | + return ( |
| 171 | + token != null && |
| 172 | + token.type !== 'Block' && |
| 173 | + token.type !== 'Line' && |
| 174 | + token.type !== 'Shebang' && |
| 175 | + !( |
| 176 | + typeof token.type === |
| 177 | + 'string' /* Although acorn supports new tokens, espree may not yet support new tokens.*/ && |
| 178 | + token.type.endsWith('Comment') |
| 179 | + ) |
| 180 | + ) |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * Check whether the given node is not an empty text node. |
| 185 | + * @param {ASTNode} node The node to check. |
| 186 | + * @returns {boolean} `false` if the token is empty text node. |
| 187 | + */ |
| 188 | +function isNotEmptyTextNode(node) { |
| 189 | + return !(node.type === 'VText' && node.value.trim() === '') |
| 190 | +} |
| 191 | + |
| 192 | +/** |
| 193 | + * Check whether the given token is a pipe operator. |
| 194 | + * @param {Token|undefined|null} token The token to check. |
| 195 | + * @returns {boolean} `true` if the token is a pipe operator. |
| 196 | + */ |
| 197 | +function isPipeOperator(token) { |
| 198 | + return token != null && token.type === 'Punctuator' && token.value === '|' |
| 199 | +} |
| 200 | + |
| 201 | +/** |
| 202 | + * Get the last element. |
| 203 | + * @template T |
| 204 | + * @param {T[]} xs The array to get the last element. |
| 205 | + * @returns {T | undefined} The last element or undefined. |
| 206 | + */ |
| 207 | +function last(xs) { |
| 208 | + return xs.length === 0 ? undefined : xs[xs.length - 1] |
| 209 | +} |
| 210 | + |
| 211 | +module.exports = { |
| 212 | + isArrow, |
| 213 | + isLeftParen, |
| 214 | + isNotLeftParen, |
| 215 | + isRightParen, |
| 216 | + isNotRightParen, |
| 217 | + isLeftBrace, |
| 218 | + isRightBrace, |
| 219 | + isLeftBracket, |
| 220 | + isRightBracket, |
| 221 | + isSemicolon, |
| 222 | + isColon, |
| 223 | + isComma, |
| 224 | + isWildcard, |
| 225 | + isQuestion, |
| 226 | + isExtendsKeyword, |
| 227 | + isNotWhitespace, |
| 228 | + isComment, |
| 229 | + isNotComment, |
| 230 | + isNotEmptyTextNode, |
| 231 | + isPipeOperator, |
| 232 | + last |
| 233 | +} |
0 commit comments