This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Rewrite #11
Merged
Merged
Rewrite #11
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
{BufferedProcess, CompositeDisposable} = require 'atom' | ||
helpers = require 'atom-linter' | ||
fs = require 'fs' | ||
fse = require 'fs-extra' | ||
path = require 'path' | ||
temp = require 'temp' | ||
XRegExp = require('xregexp').XRegExp | ||
|
||
class Linter | ||
constructor: -> | ||
@subscriptions = new CompositeDisposable | ||
@subscriptions.add atom.config.observe 'linter-haml.copyRubocopYml', (copyRubocopYml) => | ||
@copyRubocopYml = copyRubocopYml | ||
@subscriptions.add atom.config.observe 'linter-haml.hamlLintExecutablePath', (executablePath) => | ||
@executablePath = executablePath | ||
|
||
copyFile: (sourcePath, destinationPath) -> | ||
new Promise (resolve, reject) -> | ||
fse.copy sourcePath, destinationPath, (error) -> | ||
return reject Error(error) if error | ||
resolve() | ||
|
||
exists: (filePath) -> | ||
new Promise (resolve, reject) -> | ||
fs.exists filePath, (exists) -> | ||
resolve exists | ||
|
||
findFile: (filePath, fileName) => | ||
new Promise (resolve, reject) => | ||
foundPath = helpers.findFile filePath, fileName | ||
return resolve foundPath if foundPath | ||
|
||
homeDir = process.env.HOME || process.env.USERPROFILE | ||
homePath = path.join homeDir, fileName | ||
@exists homePath | ||
.then (exists) -> | ||
resolve if exists then homePath else undefined | ||
|
||
findHamlLintYmlFile: (filePath) => | ||
new Promise (resolve, reject) => | ||
@findFile filePath, '.haml-lint.yml' | ||
.then (hamlLintYmlPath) -> | ||
resolve hamlLintYmlPath | ||
|
||
findRubocopYmlFile: (filePath) => | ||
new Promise (resolve, reject) => | ||
@findFile filePath, '.rubocop.yml' | ||
.then (rubocopYmlPath) -> | ||
resolve rubocopYmlPath | ||
|
||
grammarScopes: ['text.haml'] | ||
|
||
lint: (textEditor) => | ||
new Promise (resolve, reject) => | ||
fileContent = textEditor.getText() | ||
filePath = textEditor.getPath() | ||
fileName = path.basename filePath | ||
|
||
results = [] | ||
rubocopYmlPath = undefined | ||
tempDir = undefined | ||
tempFile = undefined | ||
|
||
@makeTempDir().then (dir) => | ||
tempDir = dir | ||
@writeTempFile(tempDir, fileName, fileContent) | ||
.then (file) => | ||
tempFile = file | ||
@findRubocopYmlFile(filePath) if @copyRubocopYml | ||
.then (rubocopYmlPath) => | ||
if rubocopYmlPath | ||
@copyFile rubocopYmlPath, path.join(tempDir, '.rubocop.yml') | ||
.then => | ||
@findHamlLintYmlFile filePath | ||
.then (hamlLintYmlPath) => | ||
@lintFile textEditor, tempFile, hamlLintYmlPath | ||
.then (messages) -> | ||
results = messages | ||
.then => | ||
@removeTempDir tempDir | ||
.then -> | ||
resolve results | ||
.catch (error) -> | ||
console.error 'linter-haml error', error | ||
resolve results | ||
|
||
lintFile: (textEditor, tempFile, hamlLintYmlPath) -> | ||
new Promise (resolve, reject) => | ||
filePath = textEditor.getPath() | ||
tabLength = textEditor.getTabLength() | ||
textBuffer = textEditor.getBuffer() | ||
|
||
args = [] | ||
if hamlLintYmlPath? | ||
args.push '--config' | ||
args.push hamlLintYmlPath | ||
args.push tempFile | ||
|
||
output = [] | ||
process = new BufferedProcess | ||
command: @executablePath | ||
args: args | ||
options: | ||
cwd: path.dirname tempFile | ||
stdout: (data) -> | ||
output.push data | ||
exit: (code) -> | ||
regex = XRegExp '.+?:(?<line>\\d+) ' + | ||
'\\[((?<warning>W)|(?<error>E))\\] ' + | ||
'(?<message>.+)' | ||
messages = [] | ||
XRegExp.forEach output, regex, (match, i) -> | ||
indentLevel = textEditor.indentationForBufferRow(match.line - 1) | ||
messages.push | ||
type: if match.warning? then 'warning' else 'error' | ||
text: match.message | ||
filePath: filePath | ||
range: [ | ||
[match.line - 1, indentLevel * tabLength], | ||
[match.line - 1, textBuffer.lineLengthForRow(match.line - 1)] | ||
] | ||
resolve messages | ||
|
||
lintOnFly: true | ||
|
||
makeTempDir: -> | ||
new Promise (resolve, reject) -> | ||
temp.mkdir 'AtomLinter', (error, directory) -> | ||
return reject Error(error) if error | ||
resolve directory | ||
|
||
removeTempDir: (tempDir) -> | ||
new Promise (resolve, reject) -> | ||
fse.remove tempDir, (error) -> | ||
return reject Error(error) if error | ||
resolve() | ||
|
||
scope: 'file' | ||
|
||
writeTempFile: (tempDir, fileName, fileContent) -> | ||
new Promise (resolve, reject) -> | ||
tempFile = path.join tempDir, fileName | ||
fse.writeFile tempFile, fileContent, (error) -> | ||
return reject Error(error) if error | ||
resolve tempFile | ||
|
||
module.exports = Linter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Linter = require './linter' | ||
|
||
module.exports = | ||
activate: => | ||
@linter = new Linter | ||
|
||
config: | ||
copyRubocopYml: | ||
default: true | ||
description: 'Copy .rubocop.yml to temporary directory while linting' | ||
type: 'boolean' | ||
|
||
hamlLintExecutablePath: | ||
default: 'haml-lint' | ||
description: 'Path to haml-lint executable' | ||
type: 'string' | ||
|
||
deactivate: => | ||
@linter.subscriptions.dispose() | ||
|
||
provideLinter: => | ||
@linter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
{ | ||
"name": "linter-haml", | ||
"linter-package": true, | ||
"activationCommands": {}, | ||
"main": "./lib/init", | ||
"main": "./lib/main", | ||
"version": "0.4.0", | ||
"description": "Atom linter plugin for HAML, using haml-lint", | ||
"repository": "https://github.com/AtomLinter/linter-haml", | ||
"license": "MIT", | ||
"engines": { | ||
"atom": ">=0.151.0" | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"atom-linter": "1.0.2", | ||
"fs-extra": "0.22.1", | ||
"temp": "0.8.3", | ||
"xregexp": "2.0.0" | ||
}, | ||
"providedServices": { | ||
"linter": { | ||
"versions": { | ||
"1.0.0": "provideLinter" | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the helper module for parsing instead of using
xregexp
directlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, you're fast. :)
My biggest reason for not using the helper module for parsing is that is not able to take into account indenting and ends up underlining all the leading whitespace. My parsing uses the textEditor object to get the indentation level so it can be taken into account (a trick I picked up from linter-coffeelint). Is there some way to have the best of both worlds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also have a
rangeFromLineNumber
method here https://github.com/AtomLinter/atom-linter/blob/master/lib/helpers.coffee#L46There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like that was added after I wrote this (and will require a version bump in the dependencies). I will work on refactoring this to use the helper module.