-
Notifications
You must be signed in to change notification settings - Fork 16
No subprocess #112
No subprocess #112
Changes from 4 commits
aa36a3e
903919b
3989522
f84d299
f652f2b
0755fa9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| 'use babel'; | ||
|
|
||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import { execNode, find, rangeFromLineNumber } from 'atom-linter'; | ||
| import { findAsync, rangeFromLineNumber } from 'atom-linter'; | ||
| import stripJsonComments from 'strip-json-comments'; | ||
| import { HTMLHint } from 'htmlhint'; | ||
| import promisify from 'tiny-promisify'; | ||
|
|
||
| const readFile = promisify(fs.readFile); | ||
| const GRAMMAR_SCOPES = [ | ||
| 'text.html.angular', | ||
| 'text.html.basic', | ||
|
|
@@ -14,33 +19,34 @@ const GRAMMAR_SCOPES = [ | |
| 'text.html.ruby' | ||
| ]; | ||
|
|
||
| export const config = { | ||
| executablePath: { | ||
| title: 'Executable Path', | ||
| description: 'HTMLHint Node Script Path', | ||
| type: 'string', | ||
| default: path.join(__dirname, '..', 'node_modules', 'htmlhint', 'bin', 'htmlhint') | ||
| } | ||
| }; | ||
|
|
||
| let executablePath = ''; | ||
| export const config = {}; | ||
|
||
|
|
||
| export function activate() { | ||
| require('atom-package-deps').install('linter-htmlhint'); | ||
| } | ||
|
|
||
| executablePath = atom.config.get('linter-htmlhint.executablePath'); | ||
|
|
||
| atom.config.observe('linter-htmlhint.executablePath', newValue => { | ||
| executablePath = newValue; | ||
| }); | ||
| function getConfig(filePath) { | ||
| return findAsync(path.dirname(filePath), '.htmlhintrc') | ||
| .then(configPath => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might not be a good idea, since it has extra functionality compared to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand what you mean. Are you concerned about replicating existing htmlhint functionality inside this package? Since htmlhint does not provide an exported cosmicconfig could fill this role, but I have two concerns about it. First, it's another module to load which will likely add a few milliseconds to the startup time. Second, it looks for other possible versions of config files as well ( As for the current method, file loading and JSON parsing seem pretty tame as far as adding functionality goes. I'd say we should move it out into another module (or at least file) if there was much more of it, but it's only 18 lines and there's an overhead to using If there's some other package that can replicate the behavior of the original Thoughts?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was merely suggesting I'm perfectly fine with how this is implemented now, so let's just go with that 😉 |
||
| if (configPath) { | ||
| return readFile(configPath, 'utf8'); | ||
| } | ||
| return null; | ||
| }) | ||
| .then(conf => { | ||
| if (conf) { | ||
| return JSON.parse(stripJsonComments(conf)); | ||
| } | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| export function provideLinter() { | ||
| return { | ||
| name: 'htmlhint', | ||
| grammarScopes: GRAMMAR_SCOPES, | ||
| scope: 'file', | ||
| lintOnFly: false, | ||
| lintOnFly: true, | ||
| lint: editor => { | ||
| const text = editor.getText(); | ||
| const filePath = editor.getPath(); | ||
|
|
@@ -49,30 +55,14 @@ export function provideLinter() { | |
| return Promise.resolve([]); | ||
| } | ||
|
|
||
| const parameters = [filePath, '--format', 'json']; | ||
| const htmlhintrc = find(path.dirname(filePath), '.htmlhintrc'); | ||
|
|
||
| if (htmlhintrc) { | ||
| parameters.push('-c'); | ||
| parameters.push(htmlhintrc); | ||
| } | ||
|
|
||
| return execNode(executablePath, parameters, {}).then(output => { | ||
| const results = JSON.parse(output); | ||
|
|
||
| if (!results.length) { | ||
| return []; | ||
| } | ||
|
|
||
| const messages = results[0].messages; | ||
|
|
||
| return messages.map(message => ({ | ||
| return getConfig(filePath) | ||
| .then(ruleset => HTMLHint.verify(text, ruleset || undefined)) | ||
| .then(messages => messages.map(message => ({ | ||
| range: rangeFromLineNumber(editor, message.line - 1, message.col - 1), | ||
| type: message.type, | ||
| text: message.message, | ||
| filePath | ||
| })); | ||
| }); | ||
| }))); | ||
| } | ||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
If possible these modules should be lazily loaded, I've used lazy-req in the past, but if you have a better method go for it 😉.
Right now this will increase the package loading time as these will be required on Atom launch.
(This comment goes for all the
imports.)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.
I changed out all the imports for require calls in the places where they are needed. It looks a little odd, but it shaved about 20-30ms off the load time.