-
Notifications
You must be signed in to change notification settings - Fork 35
Add rules directory configuration #45
base: main
Are you sure you want to change the base?
Conversation
@ganralf great job! |
@mike-kaufman, would be great if this could be merged some time soon to add support for custom rules. |
1 similar comment
@mike-kaufman, would be great if this could be merged some time soon to add support for custom rules. |
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.
Thanks @ganralf! I added some questions. Forgive me if any are obvious, as I don't look at this code too frequently. :)
htmlhint-server/src/server.ts
Outdated
rulesLoaded = false; | ||
} | ||
|
||
if (rulesLoaded || htmlhint == null) { |
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.
what is htmlhint
here? The param is HTMLHint
.
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.
Correct, that check is pointless
htmlhint-server/src/server.ts
Outdated
loadCustomRules(absoluteDir, HTMLHint); | ||
} | ||
} | ||
catch (e) { } |
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.
why are exceptions getting swallowed here? Is this necessary?
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.
loadCustomRules does error handling, I will remove the try/catch in this method
|
||
// load custom rles | ||
function loadCustomRules(rulesdir:string, HTMLHint:any):any{ | ||
rulesdir = rulesdir.replace(/\\/g, '/'); |
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.
A comment here would help those of us who don't have a built-in regex parser. :)
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.
Similiarly for other regexes used in here.
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 copied both loadCustomRules and loadRule from the htmlhint module's source code as it is not exposed for us to use.
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.
Ideally instead of copy/pasting this, we'd get these methods exposed via htmlhint. This way, we don't break the VS Code exetension when htmlhint changes their implementation. Do you know what a PR would look like to refactor in htmlhint? /cc @thedaviddias.
if(fs.statSync(rulesdir).isDirectory()){ | ||
rulesdir += /\/$/.test(rulesdir)?'':'/'; | ||
rulesdir += '**/*.js'; | ||
var arrFiles = glob.sync(rulesdir, { |
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.
A comment here explaining what files you're going to load would be helpful as well.
htmlhint-server/src/server.ts
Outdated
'silent': true | ||
}); | ||
arrFiles.forEach(function(file){ | ||
loadRule(file, HTMLHint); |
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.
What's the motivation for letting someone specify a directory here? It seems like doing that will result in the extension require-ing every JS file in the directory, which could easily be problematic.
Can you support a single file, or an explicit list of files?
What does basic htmlhint allow here?
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.
The way the plugin works (as I understand it) is you can create multiple rules in as many files and subfolders as you wish.
Each .js file in the rules folder will be loaded into memory.
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 thought they had to be specified in the rules list as well via htmlhintrc or --rules on the command line.
var module = require(filepath); | ||
module(HTMLHint); | ||
} | ||
catch(e){} |
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.
blanket catch seems problematic here. Is this here for a reaason?
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 expect the idea is to skip a rule if it is broken. Given that this code is from htmlhint's source there might not be a way to properly report errors from there.
Can you please suggest a nice way we can report errors to the user as we have access to vscode here?
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 think vs code APIs let you report errors with something like connection.window.showErrorMessage(...message...);
See API docs [here](https://code.visualstudio.com/docs/extensionAPI/vscode-api_
htmlhint-server/src/server.ts
Outdated
@@ -60,6 +65,74 @@ function getRange(error: htmlhint.Error, lines: string[]): any { | |||
}; | |||
} | |||
|
|||
function loadRules(HTMLHint: any, force: boolean): any { |
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.
for consitency, I'd call HTMLHint
linter
or htmlHintLinter
- something to indicate that it is an instance ofa linter.
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.
Or can you just use the module var linter
here?
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.
Yes, that will be better, will make that change
Please @-mention me if I go dark on this. :) |
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.
some more comments... getting closer :)
rulesLoaded = false; | ||
if (linter == null || !settings.htmlhint || !settings.htmlhint.rulesDir) { | ||
return; | ||
} |
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.
nit: looks like indentation is messed up in loadRules()
return; | ||
} | ||
|
||
let rulesDir = settings.htmlhint.rulesDir; |
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 think you should be able to do something like const rulesDir = path.resolve(settings.htmlhint.rulesDir);
, and then drop all the stuff from lines 75-84?
Or alternatively, const rulesDir = path.resolve(rootDir, settings.htmlhint.rulesDir);
|
||
// load custom rles | ||
function loadCustomRules(rulesdir:string, HTMLHint:any):any{ | ||
rulesdir = rulesdir.replace(/\\/g, '/'); |
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.
Ideally instead of copy/pasting this, we'd get these methods exposed via htmlhint. This way, we don't break the VS Code exetension when htmlhint changes their implementation. Do you know what a PR would look like to refactor in htmlhint? /cc @thedaviddias.
var module = require(filepath); | ||
module(HTMLHint); | ||
} | ||
catch(e){} |
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 think vs code APIs let you report errors with something like connection.window.showErrorMessage(...message...);
See API docs [here](https://code.visualstudio.com/docs/extensionAPI/vscode-api_
so is there any way to load a /rules directory in .htmlhintrc? is that a different issue? |
@miramardesign I haven't looked at the HTMLHint source, but the documentation suggests custom rules go in a sub-folder and you use the --rulesdir on the command line to pic them up. To turn them on you can either include the custom rule name in your rc file or you can include it on the command line using the --rules option. |
Nice job here guys. What's the status on this one? |
Do you need help here? This feature would help us tremendously in developing our Angular frontend, by helping users piece together components, that rely on specific contentChildren to work. Such a linter is little help, when it's only evaluated when executing the npm command, but very helpful, when the IDE evaluates it automatically. A linter could look like this:
|
I too would love to see this feature. Do we know when this pull request will be accepted and merge? Thanks in advance! |
there's pending comments on teh PR that haven't been addressed, and conflicts that need resolved. Feel free to open another PR & move this forward. I'm happy to merge things in provided feedback is addressed. :) |
We use custom html hinting rules, this is a small improvement to allow you to specify the folder containing those rules.