Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
Speedup by caching parsed tokens when checking for syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
aminland committed Feb 23, 2018
1 parent 10dcf41 commit ceff4c8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/coffeelint.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,12 @@ coffeelint.registerRule require './rules/no_this.coffee'
coffeelint.registerRule require './rules/eol_last.coffee'
coffeelint.registerRule require './rules/no_private_function_fat_arrows.coffee'

hasSyntaxError = (source) ->
getTokens = (source) ->
try
# If there are syntax errors this will abort the lexical and line
# linters.
CoffeeScript.tokens(source)
return false
return true
return CoffeeScript.tokens(source)
return null

ErrorReport = require('./error_report.coffee')
coffeelint.getErrorReport = ->
Expand Down Expand Up @@ -310,9 +309,10 @@ coffeelint.lint = (source, userConfig = {}, literate = false) ->

# only do further checks if the syntax is okay, otherwise they just fail
# with syntax error exceptions
unless hasSyntaxError(source)
tokens = getTokens(source)
if tokens
# Do lexical linting.
lexicalLinter = new LexicalLinter(source, config, _rules, CoffeeScript)
lexicalLinter = new LexicalLinter(source, config, _rules, CoffeeScript, tokens)
lexErrors = lexicalLinter.lint()
errors = errors.concat(lexErrors)

Expand Down
8 changes: 4 additions & 4 deletions src/lexical_linter.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class TokenApi

constructor: (CoffeeScript, source, @config, @tokensByLine) ->
@tokens = CoffeeScript.tokens(source)
constructor: (CoffeeScript, source, @config, @tokensByLine, @tokens) ->
@tokens ?= CoffeeScript.tokens(source)
@lines = source.split('\n')
@tokensByLine = {} # A map of tokens by line.

Expand All @@ -18,10 +18,10 @@ BaseLinter = require './base_linter.coffee'
#
module.exports = class LexicalLinter extends BaseLinter

constructor: (source, config, rules, CoffeeScript) ->
constructor: (source, config, rules, CoffeeScript, tokens) ->
super source, config, rules

@tokenApi = new TokenApi CoffeeScript, source, @config, @tokensByLine
@tokenApi = new TokenApi CoffeeScript, source, @config, @tokensByLine, tokens
# This needs to be available on the LexicalLinter so it can be passed
# to the LineLinter when this finishes running.
@tokensByLine = @tokenApi.tokensByLine
Expand Down

0 comments on commit ceff4c8

Please sign in to comment.