Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #16 from nicolasmn/fix
Browse files Browse the repository at this point in the history
Make `atom-linter-pug` work again
  • Loading branch information
Arcanemagus authored Jan 3, 2018
2 parents 69b36fd + 8c3dddd commit 3e6355b
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 44 deletions.
80 changes: 55 additions & 25 deletions lib/init.coffee
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
{CompositeDisposable} = require('atom')
bundledPugLint = require('pug-lint')
path = require('path')
objectAssign = require('object-assign')
configFile = require('pug-lint/lib/config-file')
reqResolve = require('resolve')

pugLints = new Map()

resolvePath = (name, baseDir) ->
return new Promise (resolve, reject) ->
reqResolve name, { basedir: baseDir }, (err, res) ->
reject(err) if err?
resolve(res)

getPugLint = (baseDir) ->
if pugLints.has(baseDir)
return Promise.resolve pugLints.get(baseDir)

resolvePath('pug-lint', baseDir)
.then (pugLintPath) ->
pugLints.set(baseDir, require(pugLintPath))
return Promise.resolve pugLints.get(baseDir)
.catch () ->
pugLints.set(baseDir, bundledPugLint)
return Promise.resolve pugLints.get(baseDir)

module.exports =
config:
executablePath:
type: 'string'
default: path.join __dirname, '..', 'node_modules', 'pug-lint', 'bin', 'pug-lint'
description: 'Full path to the `pug-lint` executable node script file (e.g. /usr/local/bin/pug-lint)'

projectConfigFile:
type: 'string'
default: ''
Expand All @@ -23,6 +40,15 @@ module.exports =

activate: ->
require('atom-package-deps').install('linter-pug')

if atom.config.get('linter-pug.executablePath')?
atom.notifications.addWarning('Removing custom pug-lint path', {
detail: "linter-pug has moved to the Node.js API for pug-lint and " +
"will now use a project's local instance where possible, falling " +
"back to a bundled version of pug-lint where none is found."
})
atom.config.unset('linter-pug.executablePath')

@subscriptions = new CompositeDisposable
@subscriptions.add atom.config.observe 'linter-pug.executablePath',
(executablePath) =>
Expand Down Expand Up @@ -59,34 +85,38 @@ module.exports =
lintOnFly: true

lint: (textEditor) =>
rules = []
filePath = textEditor.getPath()
fileText = textEditor.getText()
projectConfigPath = @getConfig(filePath)
projectConfig = @getConfig(filePath)

# Use Atom's project root folder
projectDir = atom.project.relativizePath(filePath)[0]
if !projectDir?
# Fall back to the file directory
projectDir = path.dirname(filePath)

if !fileText
return Promise.resolve([])

parameters = [filePath]

if !projectConfigPath || !projectConfigPath.configPath
if !@onlyRunWhenConfig
if !projectConfig || !projectConfig.configPath
if @onlyRunWhenConfig
atom.notifications.addError 'Pug-lint config not found'
return Promise.resolve([])
return Promise.resolve([])

if(@onlyRunWhenConfig || projectConfigPath)
parameters.push('-c', projectConfigPath.configPath)
if(@onlyRunWhenConfig || projectConfig)
rules = projectConfig

parameters.push('-r', 'inline')
return new Promise (resolve) ->
getPugLint(projectDir).then (pugLint) ->
linter = new pugLint()
linter.configure rules

return helpers.execNode(@executablePath, parameters, stdin: fileText, allowEmptyStderr: true, stream: 'stderr')
.then (result) ->
regex = /(Warning|Error)?(.*)\:(\d*)\:(\d*)\s(.*)/g
messages = []
results = linter.checkString fileText

while (match = regex.exec(result)) != null
messages.push
type: if match[1] then match[1] else 'Error'
text: match[5]
filePath: match[2]
range: helpers.generateRange(textEditor, match[3] - 1, match[4] - 1)
return messages
resolve results.map (res) -> {
type: res.name
filePath: filePath
range: helpers.generateRange textEditor, res.line - 1, res.column - 1
text: res.msg
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"atom-linter": "^10.0.0",
"atom-package-deps": "^4.0.1",
"object-assign": "^4.1.0",
"pug-lint": "^2.1.9"
"pug-lint": "^2.1.9",
"resolve": "^1.5.0"
},
"package-deps": [
"linter"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions spec/fixtures/noConfig/badRule.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a(href='text #{title}') Link
1 change: 1 addition & 0 deletions spec/fixtures/noConfig/badSyntax.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a(href='text #{title}' Link
57 changes: 39 additions & 18 deletions spec/linter-stylint-spec.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
path = require 'path'

goodPug = path.join(__dirname, 'fixtures', 'good.pug')
badPug = path.join(__dirname, 'fixtures', 'bad.pug')
goodPug = path.join(__dirname, 'fixtures', 'config', 'good.pug')
badPug = path.join(__dirname, 'fixtures', 'config', 'bad.pug')
noConfigRule = path.join(__dirname, 'fixtures', 'noConfig', 'badRule.pug')
noConfigSyntax = path.join(__dirname, 'fixtures', 'noConfig', 'badSyntax.pug')

describe 'The pug-lint provider for Linter', ->
lint = require('../lib/init.coffee').provideLinter().lint
Expand All @@ -18,19 +20,38 @@ describe 'The pug-lint provider for Linter', ->
it 'should be an active package', ->
expect(atom.packages.isPackageActive('linter-pug')).toBe true

it 'finds nothing wrong with valid file', ->
waitsForPromise ->
atom.workspace.open(goodPug).then (editor) ->
lint(editor).then (messages) ->
expect(messages.length).toBe 0

it 'finds something wrong with invalid file', ->
errMsg = 'Attribute interpolation operators must not be used'
waitsForPromise ->
atom.workspace.open(badPug).then (editor) ->
lint(editor).then (messages) ->
expect(messages.length).toEqual 1
expect(messages[0].html).not.toBeDefined()
expect(messages[0].text).toBe errMsg
expect(messages[0].filePath).toBe badPug
expect(messages[0].range).toEqual [[0, 13], [0, 20]]
describe 'works with a configuration', ->
it 'finds nothing wrong with valid file', ->
waitsForPromise ->
atom.workspace.open(goodPug).then (editor) ->
lint(editor).then (messages) ->
expect(messages.length).toBe 0

it 'finds something wrong with invalid file', ->
errMsg = 'Attribute interpolation operators must not be used'
waitsForPromise ->
atom.workspace.open(badPug).then (editor) ->
lint(editor).then (messages) ->
expect(messages.length).toEqual 1
expect(messages[0].html).not.toBeDefined()
expect(messages[0].text).toBe errMsg
expect(messages[0].filePath).toBe badPug
expect(messages[0].range).toEqual [[0, 13], [0, 20]]

describe 'works without a configuration', ->
it 'finds nothing wrong with a "bad" file', ->
waitsForPromise ->
atom.workspace.open(noConfigRule).then (editor) ->
lint(editor).then (messages) ->
expect(messages.length).toBe 0

it 'finds syntax errors without a configuration', ->
errMsg = 'The end of the string reached with no closing bracket ) found.'
waitsForPromise ->
atom.workspace.open(noConfigSyntax).then (editor) ->
lint(editor).then (messages) ->
expect(messages.length).toEqual 1
expect(messages[0].html).not.toBeDefined()
expect(messages[0].text).toBe errMsg
expect(messages[0].filePath).toBe noConfigSyntax
expect(messages[0].range).toEqual [[1, 0], [1, 0]]

0 comments on commit 3e6355b

Please sign in to comment.