Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactored Source and Line classes into separate module

  • Loading branch information...
commit c45a01221255c241c636ba6165e0f4a1028d8fde 1 parent 166847c
Sleepyfox authored
63  loc.coffee
... ...
@@ -0,0 +1,63 @@
  1
+class Line 
  2
+  constructor: (@string) ->
  3
+
  4
+  isAllOneLineComment: ->
  5
+    if @string.match(/^\s*\/\//) is null
  6
+      false
  7
+    else
  8
+      true
  9
+
  10
+  isStartOfBlockComment: ->
  11
+    if @string.match(/\/\*/) is null
  12
+      false
  13
+    else
  14
+      true
  15
+
  16
+  isEndOfBlockComment: ->
  17
+    if @string.match(/\*\//) is null
  18
+      false
  19
+    else
  20
+      true
  21
+
  22
+  hasNonWhitespaceBeforeBlockCommentStart: ->
  23
+    if @string.match(/\S+\s*\/\*/) is null
  24
+      false
  25
+    else
  26
+      true
  27
+
  28
+  hasNonWhitespaceAfterBlockCommentEnd: ->
  29
+    if @string.match(/\*\/\s*\S+/) is null
  30
+      false
  31
+    else
  32
+      true
  33
+  
  34
+class Source   
  35
+  constructor: (string) ->
  36
+    @array = string.split '\n'
  37
+
  38
+  lines: ->
  39
+    @array.length
  40
+
  41
+  linesOfCode: ->
  42
+    lineCounter = 0
  43
+    inBlockComment = false
  44
+    for sourceLine in @array
  45
+      line = new Line sourceLine
  46
+      # start of hideous nested if/else block
  47
+      if inBlockComment
  48
+        if line.isEndOfBlockComment() 
  49
+          inBlockComment = false
  50
+          if line.hasNonWhitespaceAfterBlockCommentEnd() 
  51
+            lineCounter++
  52
+      else # not in block comment
  53
+        if line.isStartOfBlockComment() 
  54
+          inBlockComment = true
  55
+          if line.hasNonWhitespaceBeforeBlockCommentStart() 
  56
+            lineCounter++
  57
+        else # not start of block comment
  58
+          unless line.isAllOneLineComment() 
  59
+            lineCounter++          
  60
+    lineCounter
  61
+
  62
+exports.Source = Source
  63
+exports.Line = Line
64  test-lines-of-code.coffee
... ...
@@ -1,5 +1,8 @@
1 1
 vows = require 'vows'
2 2
 assert = require 'assert'
  3
+loc = require './loc'
  4
+Source = loc.Source
  5
+Line = loc.Line
3 6
 
4 7
 ONELINESOURCE = "public static final void main { }"
5 8
 
@@ -86,64 +89,3 @@ vows
86 89
 
87 90
   .export(module)
88 91
 
89  
-class Line 
90  
-  constructor: (@string) ->
91  
-
92  
-  isAllOneLineComment: ->
93  
-    if @string.match(/^\s*\/\//) is null
94  
-      false
95  
-    else
96  
-      true
97  
-
98  
-  isStartOfBlockComment: ->
99  
-    if @string.match(/\/\*/) is null
100  
-      false
101  
-    else
102  
-      true
103  
-
104  
-  isEndOfBlockComment: ->
105  
-    if @string.match(/\*\//) is null
106  
-      false
107  
-    else
108  
-      true
109  
-
110  
-  hasNonWhitespaceBeforeBlockCommentStart: ->
111  
-    if @string.match(/\S+\s*\/\*/) is null
112  
-      false
113  
-    else
114  
-      true
115  
-
116  
-  hasNonWhitespaceAfterBlockCommentEnd: ->
117  
-    if @string.match(/\*\/\s*\S+/) is null
118  
-      false
119  
-    else
120  
-      true
121  
-  
122  
-class Source   
123  
-  constructor: (string) ->
124  
-    @array = string.split '\n'
125  
-
126  
-  lines: ->
127  
-    @array.length
128  
-
129  
-  linesOfCode: ->
130  
-    lineCounter = 0
131  
-    inBlockComment = false
132  
-    for sourceLine in @array
133  
-      line = new Line sourceLine
134  
-      # start of hideous nested if/else block
135  
-      if inBlockComment
136  
-        if line.isEndOfBlockComment() 
137  
-          inBlockComment = false
138  
-          if line.hasNonWhitespaceAfterBlockCommentEnd() 
139  
-            lineCounter++
140  
-      else # not in block comment
141  
-        if line.isStartOfBlockComment() 
142  
-          inBlockComment = true
143  
-          if line.hasNonWhitespaceBeforeBlockCommentStart() 
144  
-            lineCounter++
145  
-        else # not start of block comment
146  
-          unless line.isAllOneLineComment() 
147  
-            lineCounter++          
148  
-    lineCounter
149  
-

0 notes on commit c45a012

Please sign in to comment.
Something went wrong with that request. Please try again.