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

Commit

Permalink
Strip comments from config file before parsing
Browse files Browse the repository at this point in the history
It's nice to allow teams to explain their rules or whatever in the config file.
This allows comments to happily exist in the config file.
  • Loading branch information
Jason Webster committed Apr 26, 2015
1 parent 2d5592c commit b7aa5ab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -28,7 +28,8 @@
"glob": "^4.0.0",
"ignore": "^2.2.15",
"optimist": "^0.6.1",
"resolve": "^0.6.3"
"resolve": "^0.6.3",
"strip-json-comments": "^1.0.2"
},
"devDependencies": {
"vows": ">=0.6.0",
Expand Down
9 changes: 7 additions & 2 deletions src/commandline.coffee
Expand Up @@ -13,6 +13,7 @@ os = require("os")
glob = require("glob")
optimist = require("optimist")
ignore = require('ignore')
stripComments = require('strip-json-comments')
thisdir = path.dirname(fs.realpathSync(__filename))
coffeelint = require(path.join(thisdir, "coffeelint"))
configfinder = require(path.join(thisdir, "configfinder"))
Expand Down Expand Up @@ -59,6 +60,10 @@ lintSource = (source, config, literate = false) ->
errorReport.lint("stdin", source, config, literate)
return errorReport

# Load a config file given a path/filename
loadConfig = (path) ->
JSON.parse(stripComments(read(path)))

# Get fallback configuration. With the -F flag found configs in standard places
# will be used for each file being linted. Standard places are package.json or
# coffeelint.json in a project's root folder or the user's home folder.
Expand Down Expand Up @@ -188,15 +193,15 @@ else
config = null
unless options.argv.noconfig
if options.argv.f
config = JSON.parse read options.argv.f
config = loadConfig(options.argv.f)

# If -f was specifying a package.json, extract the config
if config.coffeelintConfig
config = config.coffeelintConfig

else if (process.env.COFFEELINT_CONFIG and
fs.existsSync(process.env.COFFEELINT_CONFIG))
config = JSON.parse(read(process.env.COFFEELINT_CONFIG))
config = loadConfig(process.env.COFFEELINT_CONFIG)

ruleLoader.loadRule(coffeelint, options.argv.rules) if options.argv.rules

Expand Down
3 changes: 2 additions & 1 deletion src/configfinder.coffee
Expand Up @@ -5,6 +5,7 @@ JSHint does.

fs = require 'fs'
path = require 'path'
stripComments = require 'strip-json-comments'

# Cache for findFile
findFileResults = {}
Expand All @@ -31,7 +32,7 @@ loadNpmConfig = (dir) ->
# Parse a JSON file gracefully.
loadJSON = (filename) ->
try
JSON.parse(fs.readFileSync(filename).toString())
JSON.parse(stripComments(fs.readFileSync(filename).toString()))
catch e
process.stderr.write "Could not load JSON file '#{filename}': #{e}"
null
Expand Down

0 comments on commit b7aa5ab

Please sign in to comment.