[d565449] | 1 | /*
|
---|
| 2 | Copyright (C) 2013 Yusuke Suzuki <utatane.tea@gmail.com>
|
---|
| 3 |
|
---|
| 4 | Redistribution and use in source and binary forms, with or without
|
---|
| 5 | modification, are permitted provided that the following conditions are met:
|
---|
| 6 |
|
---|
| 7 | * Redistributions of source code must retain the above copyright
|
---|
| 8 | notice, this list of conditions and the following disclaimer.
|
---|
| 9 | * Redistributions in binary form must reproduce the above copyright
|
---|
| 10 | notice, this list of conditions and the following disclaimer in the
|
---|
| 11 | documentation and/or other materials provided with the distribution.
|
---|
| 12 |
|
---|
| 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
|
---|
| 14 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 15 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 16 | ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
---|
| 17 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
| 18 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
| 19 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
| 20 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 21 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 22 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 23 | */
|
---|
| 24 |
|
---|
| 25 | (function () {
|
---|
| 26 | 'use strict';
|
---|
| 27 |
|
---|
| 28 | function isExpression(node) {
|
---|
| 29 | if (node == null) { return false; }
|
---|
| 30 | switch (node.type) {
|
---|
| 31 | case 'ArrayExpression':
|
---|
| 32 | case 'AssignmentExpression':
|
---|
| 33 | case 'BinaryExpression':
|
---|
| 34 | case 'CallExpression':
|
---|
| 35 | case 'ConditionalExpression':
|
---|
| 36 | case 'FunctionExpression':
|
---|
| 37 | case 'Identifier':
|
---|
| 38 | case 'Literal':
|
---|
| 39 | case 'LogicalExpression':
|
---|
| 40 | case 'MemberExpression':
|
---|
| 41 | case 'NewExpression':
|
---|
| 42 | case 'ObjectExpression':
|
---|
| 43 | case 'SequenceExpression':
|
---|
| 44 | case 'ThisExpression':
|
---|
| 45 | case 'UnaryExpression':
|
---|
| 46 | case 'UpdateExpression':
|
---|
| 47 | return true;
|
---|
| 48 | }
|
---|
| 49 | return false;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | function isIterationStatement(node) {
|
---|
| 53 | if (node == null) { return false; }
|
---|
| 54 | switch (node.type) {
|
---|
| 55 | case 'DoWhileStatement':
|
---|
| 56 | case 'ForInStatement':
|
---|
| 57 | case 'ForStatement':
|
---|
| 58 | case 'WhileStatement':
|
---|
| 59 | return true;
|
---|
| 60 | }
|
---|
| 61 | return false;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | function isStatement(node) {
|
---|
| 65 | if (node == null) { return false; }
|
---|
| 66 | switch (node.type) {
|
---|
| 67 | case 'BlockStatement':
|
---|
| 68 | case 'BreakStatement':
|
---|
| 69 | case 'ContinueStatement':
|
---|
| 70 | case 'DebuggerStatement':
|
---|
| 71 | case 'DoWhileStatement':
|
---|
| 72 | case 'EmptyStatement':
|
---|
| 73 | case 'ExpressionStatement':
|
---|
| 74 | case 'ForInStatement':
|
---|
| 75 | case 'ForStatement':
|
---|
| 76 | case 'IfStatement':
|
---|
| 77 | case 'LabeledStatement':
|
---|
| 78 | case 'ReturnStatement':
|
---|
| 79 | case 'SwitchStatement':
|
---|
| 80 | case 'ThrowStatement':
|
---|
| 81 | case 'TryStatement':
|
---|
| 82 | case 'VariableDeclaration':
|
---|
| 83 | case 'WhileStatement':
|
---|
| 84 | case 'WithStatement':
|
---|
| 85 | return true;
|
---|
| 86 | }
|
---|
| 87 | return false;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | function isSourceElement(node) {
|
---|
| 91 | return isStatement(node) || node != null && node.type === 'FunctionDeclaration';
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | function trailingStatement(node) {
|
---|
| 95 | switch (node.type) {
|
---|
| 96 | case 'IfStatement':
|
---|
| 97 | if (node.alternate != null) {
|
---|
| 98 | return node.alternate;
|
---|
| 99 | }
|
---|
| 100 | return node.consequent;
|
---|
| 101 |
|
---|
| 102 | case 'LabeledStatement':
|
---|
| 103 | case 'ForStatement':
|
---|
| 104 | case 'ForInStatement':
|
---|
| 105 | case 'WhileStatement':
|
---|
| 106 | case 'WithStatement':
|
---|
| 107 | return node.body;
|
---|
| 108 | }
|
---|
| 109 | return null;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | function isProblematicIfStatement(node) {
|
---|
| 113 | var current;
|
---|
| 114 |
|
---|
| 115 | if (node.type !== 'IfStatement') {
|
---|
| 116 | return false;
|
---|
| 117 | }
|
---|
| 118 | if (node.alternate == null) {
|
---|
| 119 | return false;
|
---|
| 120 | }
|
---|
| 121 | current = node.consequent;
|
---|
| 122 | do {
|
---|
| 123 | if (current.type === 'IfStatement') {
|
---|
| 124 | if (current.alternate == null) {
|
---|
| 125 | return true;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | current = trailingStatement(current);
|
---|
| 129 | } while (current);
|
---|
| 130 |
|
---|
| 131 | return false;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | module.exports = {
|
---|
| 135 | isExpression: isExpression,
|
---|
| 136 | isStatement: isStatement,
|
---|
| 137 | isIterationStatement: isIterationStatement,
|
---|
| 138 | isSourceElement: isSourceElement,
|
---|
| 139 | isProblematicIfStatement: isProblematicIfStatement,
|
---|
| 140 |
|
---|
| 141 | trailingStatement: trailingStatement
|
---|
| 142 | };
|
---|
| 143 | }());
|
---|
| 144 | /* vim: set sw=4 ts=4 et tw=80 : */
|
---|