Permalink
Browse files

refactored Source and Line classes into separate module

  • Loading branch information...
sleepyfox committed Feb 19, 2012
1 parent 166847c commit c45a01221255c241c636ba6165e0f4a1028d8fde
Showing with 66 additions and 61 deletions.
  1. +63 −0 loc.coffee
  2. +3 −61 test-lines-of-code.coffee
View
@@ -0,0 +1,63 @@
+class Line
+ constructor: (@string) ->
+
+ isAllOneLineComment: ->
+ if @string.match(/^\s*\/\//) is null
+ false
+ else
+ true
+
+ isStartOfBlockComment: ->
+ if @string.match(/\/\*/) is null
+ false
+ else
+ true
+
+ isEndOfBlockComment: ->
+ if @string.match(/\*\//) is null
+ false
+ else
+ true
+
+ hasNonWhitespaceBeforeBlockCommentStart: ->
+ if @string.match(/\S+\s*\/\*/) is null
+ false
+ else
+ true
+
+ hasNonWhitespaceAfterBlockCommentEnd: ->
+ if @string.match(/\*\/\s*\S+/) is null
+ false
+ else
+ true
+
+class Source
+ constructor: (string) ->
+ @array = string.split '\n'
+
+ lines: ->
+ @array.length
+
+ linesOfCode: ->
+ lineCounter = 0
+ inBlockComment = false
+ for sourceLine in @array
+ line = new Line sourceLine
+ # start of hideous nested if/else block
+ if inBlockComment
+ if line.isEndOfBlockComment()
+ inBlockComment = false
+ if line.hasNonWhitespaceAfterBlockCommentEnd()
+ lineCounter++
+ else # not in block comment
+ if line.isStartOfBlockComment()
+ inBlockComment = true
+ if line.hasNonWhitespaceBeforeBlockCommentStart()
+ lineCounter++
+ else # not start of block comment
+ unless line.isAllOneLineComment()
+ lineCounter++
+ lineCounter
+
+exports.Source = Source
+exports.Line = Line
View
@@ -1,5 +1,8 @@
vows = require 'vows'
assert = require 'assert'
+loc = require './loc'
+Source = loc.Source
+Line = loc.Line
ONELINESOURCE = "public static final void main { }"
@@ -86,64 +89,3 @@ vows
.export(module)
-class Line
- constructor: (@string) ->
-
- isAllOneLineComment: ->
- if @string.match(/^\s*\/\//) is null
- false
- else
- true
-
- isStartOfBlockComment: ->
- if @string.match(/\/\*/) is null
- false
- else
- true
-
- isEndOfBlockComment: ->
- if @string.match(/\*\//) is null
- false
- else
- true
-
- hasNonWhitespaceBeforeBlockCommentStart: ->
- if @string.match(/\S+\s*\/\*/) is null
- false
- else
- true
-
- hasNonWhitespaceAfterBlockCommentEnd: ->
- if @string.match(/\*\/\s*\S+/) is null
- false
- else
- true
-
-class Source
- constructor: (string) ->
- @array = string.split '\n'
-
- lines: ->
- @array.length
-
- linesOfCode: ->
- lineCounter = 0
- inBlockComment = false
- for sourceLine in @array
- line = new Line sourceLine
- # start of hideous nested if/else block
- if inBlockComment
- if line.isEndOfBlockComment()
- inBlockComment = false
- if line.hasNonWhitespaceAfterBlockCommentEnd()
- lineCounter++
- else # not in block comment
- if line.isStartOfBlockComment()
- inBlockComment = true
- if line.hasNonWhitespaceBeforeBlockCommentStart()
- lineCounter++
- else # not start of block comment
- unless line.isAllOneLineComment()
- lineCounter++
- lineCounter
-

0 comments on commit c45a012

Please sign in to comment.