| Conditions | 44 |
| Total Lines | 112 |
| Code Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ruby.js ➔ tokenBase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others |
||
| 39 | function tokenBase(stream, state) { |
||
| 40 | curPunc = null; |
||
| 41 | if (stream.sol() && stream.match("=begin") && stream.eol()) { |
||
| 42 | state.tokenize.push(readBlockComment); |
||
| 43 | return "comment"; |
||
| 44 | } |
||
| 45 | if (stream.eatSpace()) return null; |
||
| 46 | var ch = stream.next(), m; |
||
| 47 | if (ch == "`" || ch == "'" || ch == '"') { |
||
| 48 | return chain(readQuoted(ch, "string", ch == '"' || ch == "`"), stream, state); |
||
| 49 | } else if (ch == "/") { |
||
| 50 | var currentIndex = stream.current().length; |
||
| 51 | if (stream.skipTo("/")) { |
||
| 52 | var search_till = stream.current().length; |
||
| 53 | stream.backUp(stream.current().length - currentIndex); |
||
| 54 | var balance = 0; // balance brackets |
||
| 55 | while (stream.current().length < search_till) { |
||
| 56 | var chchr = stream.next(); |
||
| 57 | if (chchr == "(") balance += 1; |
||
| 58 | else if (chchr == ")") balance -= 1; |
||
| 59 | if (balance < 0) break; |
||
| 60 | } |
||
| 61 | stream.backUp(stream.current().length - currentIndex); |
||
| 62 | if (balance == 0) |
||
| 63 | return chain(readQuoted(ch, "string-2", true), stream, state); |
||
| 64 | } |
||
| 65 | return "operator"; |
||
| 66 | } else if (ch == "%") { |
||
| 67 | var style = "string", embed = true; |
||
| 68 | if (stream.eat("s")) style = "atom"; |
||
| 69 | else if (stream.eat(/[WQ]/)) style = "string"; |
||
| 70 | else if (stream.eat(/[r]/)) style = "string-2"; |
||
| 71 | else if (stream.eat(/[wxq]/)) { style = "string"; embed = false; } |
||
| 72 | var delim = stream.eat(/[^\w\s=]/); |
||
| 73 | if (!delim) return "operator"; |
||
| 74 | if (matching.propertyIsEnumerable(delim)) delim = matching[delim]; |
||
| 75 | return chain(readQuoted(delim, style, embed, true), stream, state); |
||
| 76 | } else if (ch == "#") { |
||
| 77 | stream.skipToEnd(); |
||
| 78 | return "comment"; |
||
| 79 | } else if (ch == "<" && (m = stream.match(/^<-?[\`\"\']?([a-zA-Z_?]\w*)[\`\"\']?(?:;|$)/))) { |
||
| 80 | return chain(readHereDoc(m[1]), stream, state); |
||
| 81 | } else if (ch == "0") { |
||
| 82 | if (stream.eat("x")) stream.eatWhile(/[\da-fA-F]/); |
||
| 83 | else if (stream.eat("b")) stream.eatWhile(/[01]/); |
||
| 84 | else stream.eatWhile(/[0-7]/); |
||
| 85 | return "number"; |
||
| 86 | } else if (/\d/.test(ch)) { |
||
| 87 | stream.match(/^[\d_]*(?:\.[\d_]+)?(?:[eE][+\-]?[\d_]+)?/); |
||
| 88 | return "number"; |
||
| 89 | } else if (ch == "?") { |
||
| 90 | while (stream.match(/^\\[CM]-/)) {} |
||
| 91 | if (stream.eat("\\")) stream.eatWhile(/\w/); |
||
| 92 | else stream.next(); |
||
| 93 | return "string"; |
||
| 94 | } else if (ch == ":") { |
||
| 95 | if (stream.eat("'")) return chain(readQuoted("'", "atom", false), stream, state); |
||
| 96 | if (stream.eat('"')) return chain(readQuoted('"', "atom", true), stream, state); |
||
| 97 | |||
| 98 | // :> :>> :< :<< are valid symbols |
||
| 99 | if (stream.eat(/[\<\>]/)) { |
||
| 100 | stream.eat(/[\<\>]/); |
||
| 101 | return "atom"; |
||
| 102 | } |
||
| 103 | |||
| 104 | // :+ :- :/ :* :| :& :! are valid symbols |
||
| 105 | if (stream.eat(/[\+\-\*\/\&\|\:\!]/)) { |
||
| 106 | return "atom"; |
||
| 107 | } |
||
| 108 | |||
| 109 | // Symbols can't start by a digit |
||
| 110 | if (stream.eat(/[a-zA-Z$@_\xa1-\uffff]/)) { |
||
| 111 | stream.eatWhile(/[\w$\xa1-\uffff]/); |
||
| 112 | // Only one ? ! = is allowed and only as the last character |
||
| 113 | stream.eat(/[\?\!\=]/); |
||
| 114 | return "atom"; |
||
| 115 | } |
||
| 116 | return "operator"; |
||
| 117 | } else if (ch == "@" && stream.match(/^@?[a-zA-Z_\xa1-\uffff]/)) { |
||
| 118 | stream.eat("@"); |
||
| 119 | stream.eatWhile(/[\w\xa1-\uffff]/); |
||
| 120 | return "variable-2"; |
||
| 121 | } else if (ch == "$") { |
||
| 122 | if (stream.eat(/[a-zA-Z_]/)) { |
||
| 123 | stream.eatWhile(/[\w]/); |
||
| 124 | } else if (stream.eat(/\d/)) { |
||
| 125 | stream.eat(/\d/); |
||
| 126 | } else { |
||
| 127 | stream.next(); // Must be a special global like $: or $! |
||
| 128 | } |
||
| 129 | return "variable-3"; |
||
| 130 | } else if (/[a-zA-Z_\xa1-\uffff]/.test(ch)) { |
||
| 131 | stream.eatWhile(/[\w\xa1-\uffff]/); |
||
| 132 | stream.eat(/[\?\!]/); |
||
| 133 | if (stream.eat(":")) return "atom"; |
||
| 134 | return "ident"; |
||
| 135 | } else if (ch == "|" && (state.varList || state.lastTok == "{" || state.lastTok == "do")) { |
||
| 136 | curPunc = "|"; |
||
| 137 | return null; |
||
| 138 | } else if (/[\(\)\[\]{}\\;]/.test(ch)) { |
||
| 139 | curPunc = ch; |
||
| 140 | return null; |
||
| 141 | } else if (ch == "-" && stream.eat(">")) { |
||
| 142 | return "arrow"; |
||
| 143 | } else if (/[=+\-\/*:\.^%<>~|]/.test(ch)) { |
||
| 144 | var more = stream.eatWhile(/[=+\-\/*:\.^%<>~|]/); |
||
| 145 | if (ch == "." && !more) curPunc = "."; |
||
| 146 | return "operator"; |
||
| 147 | } else { |
||
| 148 | return null; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 286 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.