Skip to content

Commit

Permalink
basic port to CoffeeScript 2 almost finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Emanuele Tamponi committed Nov 10, 2017
1 parent fdf999e commit 11b3ddc
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -52,7 +52,7 @@
"sinon": "^1.14.1"
},
"scripts": {
"prepublish": "npm run build && mocha",
"prepublish": "npm run build",
"test": "npm run build && mocha && istanbul report",
"coverage-report": "istanbul report text-summary lcov",
"build": "coffee -c -o lib src",
Expand Down
68 changes: 43 additions & 25 deletions src/SkipVisitor.coffee
Expand Up @@ -15,23 +15,30 @@ PRAGMAS = [
regex: /^!pragma\s+coverage-skip-next$/
istanbulRegex: /^istanbul\s+ignore\s+next$/
fn: (self, node, match, options={}) ->
next = self._getNext(node, match)
next.markAll 'skip', true
origNode = node
if node.type is "Value"
if node.parent.type is "Assign"
node = node.parent
else
node = node.parent.parent
else if node.type isnt "If"
node = node.parent

unless node
throw new Error "Pragma '#{match[0]}' at #{self._toLocString origNode} has no next statement"

node.markAll 'skip', true
}

# '!pragma coverage-skip-block'
{
regex: /^!pragma\s+coverage-skip-block$/
fn: (self, node, match, options={}) ->
parent = node.parent
parent.markAll 'skip', true
parent = node.parent.parent.parent

if parent.type isnt 'Block'
### !pragma coverage-skip-block ###
throw new Error "Pragma '#{match[0]}' at #{@_toLocString node} is not " +
"child of a Block (how did you even do this!?)"
parent.markAll 'skip', true

if parent.parent?.type is 'If'
if parent.parent.type is 'If'
ifBody = parent
ifNode = parent.parent
if ifBody.childName is 'body'
Expand All @@ -46,8 +53,14 @@ PRAGMAS = [
{
regex: /^!pragma\s+no-coverage-next$/
fn: (self, node, match, options={}) ->
next = self._getNext(node, match)
next.markAll 'noCoverage', true
if node.type is "Value"
if node.parent.type is "Assign"
node = node.parent
else
node = node.parent.parent
else if node.type isnt "If"
node = node.parent
node.markAll 'noCoverage', true
}

# 'istanbul ignore if'
Expand All @@ -57,7 +70,12 @@ PRAGMAS = [
{
istanbulRegex: /^istanbul\s+ignore\s+if$/
fn: (self, node, match, options={}) ->
ifNode = self._getNext(node, match, 'If')
if node.type is "IdentifierLiteral"
return
if node.type is "Value" and node.node.base.constructor?.name is "PassthroughLiteral"
throw new Error "Pragma '#{match[0]}' at #{self._toLocString node} has no next statement"

ifNode = self.checkType node, match, "If"
ifNode.mark 'skipIf', true
ifNode.child('body')?.markAll 'skip', true
}
Expand All @@ -69,7 +87,10 @@ PRAGMAS = [
{
istanbulRegex: /^istanbul\s+ignore\s+else$/
fn: (self, node, match, options={}) ->
ifNode = self._getNext(node, match, 'If')
if node.type is "IdentifierLiteral"
return

ifNode = self.checkType node, match, "If"
ifNode.mark 'skipElse', true
ifNode.child('elseBody')?.markAll 'skip', true
}
Expand All @@ -80,8 +101,10 @@ module.exports = class SkipVisitor
constructor: (@fileName) ->

visitComment: (node) ->
comment = node.node.comment?.trim().toLowerCase() ? ''
found = false
if node.node.comments.visited
return

comment = node.node.comments[0].content.trim().toLowerCase() ? ''
if _.startsWith(comment, PRAGMA_PREFIX)
PRAGMAS
.filter (pragma) -> pragma.regex?
Expand All @@ -95,19 +118,14 @@ module.exports = class SkipVisitor
if match = comment.match(pragma.istanbulRegex)
pragma.fn this, node, match, @options

node.node.comments.visited = true

_toLocString: (node) ->
return "#{@fileName} (#{node.locationData.first_line + 1}:#{node.locationData.first_column + 1})"

# Get the next non-comment statement.
_getNext: (node, match, type=null) ->
next = node.next()

# Skip over any comments
next = next.next() while next?.type is 'Comment'

if !next?
throw new Error "Pragma '#{match[0]}' at #{@_toLocString node} has no next statement"
if type? and next.type isnt type
checkType: (node, match, type=null) ->
if type? and node.type isnt type
throw new Error "Statement after pragma '#{match[0]}' at #{@_toLocString node} is not of type #{type}"
next
node

3 changes: 3 additions & 0 deletions src/coffeeCoverage.coffee
Expand Up @@ -331,6 +331,9 @@ exports._runInstrumentor = (instrumentor, fileName, source, options={}) ->
indent = (" " for i in [0...nodeWrapper.depth]).join ''
options.log.debug "#{indent}Examining #{nodeWrapper.toString()}"

if nodeWrapper.node.comments
visitor["visitComment"]?(nodeWrapper)

if nodeWrapper.isStatement
visitor["visitStatement"]?(nodeWrapper)

Expand Down
6 changes: 3 additions & 3 deletions test/istanbul/pragmaTests.coffee
Expand Up @@ -253,7 +253,7 @@ module.exports = (run) ->
expect(instrumentor.fnMap[0].skip).to.not.exist
expect(instrumentor.fnMap[1].skip).to.be.true

it "should not instrument a function", ->
it "should not instrument a class", ->
{instrumentor, result} = run """
console.log "hello"
### !pragma no-coverage-next ###
Expand All @@ -273,15 +273,15 @@ module.exports = (run) ->
console.log "foo"
### !pragma coverage-skip-next ###
"""
.to.throw "Pragma '!pragma coverage-skip-next' at #{FILENAME} (3:5) has no next statement"
.to.throw "Pragma '!pragma coverage-skip-next' at #{FILENAME} (3:39) has no next statement"

expect ->
run """
myFunc = ->
console.log "foo"
### istanbul ignore if ###
"""
.to.throw "Pragma 'istanbul ignore if' at #{FILENAME} (3:1) has no next statement"
.to.throw "Pragma 'istanbul ignore if' at #{FILENAME} (3:27) has no next statement"

it "should throw an error when an 'if' pragma isn't before an 'if'", ->
expect ->
Expand Down

0 comments on commit 11b3ddc

Please sign in to comment.