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

Commit aebf50e

Browse files
authored
Merge pull request #169 from AtomLinter/arcanemagus/only-modify-php
Only modify PHP files
2 parents a97e49d + 9e0fe21 commit aebf50e

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lib/index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ let generateRange;
1212
let tinyPromisify;
1313
let stripJSONComments;
1414

15+
const phpEmbeddedScope = 'text.html.php';
16+
1517
const getConfig = async (filePath) => {
1618
const readFile = tinyPromisify(fsReadFile);
1719
const configPath = await findAsync(dirname(filePath), '.htmlhintrc');
@@ -25,6 +27,17 @@ const getConfig = async (filePath) => {
2527
return null;
2628
};
2729

30+
const phpScopedEditor = editor => editor.getCursors().some(cursor =>
31+
cursor.getScopeDescriptor().getScopesArray().some(scope =>
32+
scope === phpEmbeddedScope));
33+
34+
const removePHP = str => str.replace(/<\?(?:php|=)?(?:[\s\S])+?\?>/gi, (match) => {
35+
const newlines = match.match(/\r?\n|\r/g);
36+
const newlineCount = newlines ? newlines.length : 0;
37+
38+
return '\n'.repeat(newlineCount);
39+
});
40+
2841
const loadDeps = () => {
2942
if (loadDeps.loaded) {
3043
return;
@@ -98,16 +111,11 @@ export default {
98111
return null;
99112
}
100113

101-
const removePHP = str => str.replace(/<\?(?:php|=)?(?:[\s\S])+?\?>/gi, (match) => {
102-
const newlines = match.match(/\r?\n|\r/g);
103-
const newlineCount = newlines ? newlines.length : 0;
104-
105-
return '\n'.repeat(newlineCount);
106-
});
114+
const isPhPEditor = phpScopedEditor(editor);
107115

108116
const fileText = editor.getText();
109-
const fileTextStripped = removePHP(fileText);
110-
if (!fileTextStripped) {
117+
const text = isPhPEditor ? removePHP(fileText) : fileText;
118+
if (!text) {
111119
return [];
112120
}
113121

@@ -116,7 +124,7 @@ export default {
116124

117125
const ruleset = await getConfig(filePath);
118126

119-
const messages = HTMLHint.verify(fileTextStripped, ruleset || undefined);
127+
const messages = HTMLHint.verify(text, ruleset || undefined);
120128

121129
if (editor.getText() !== fileText) {
122130
// Editor contents have changed, tell Linter not to update

0 commit comments

Comments
 (0)