This repository was archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.js
139 lines (120 loc) · 3.77 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use babel';
// eslint-disable-next-line import/no-extraneous-dependencies, import/extensions
import { CompositeDisposable } from 'atom';
// Dependencies
let helpers;
let path;
// Local variables
const parseRegex = /^((?:Parse|Fatal) error|Deprecated):\s+(.+) in .+?(?: on line |:)(\d+)/gm;
const phpVersionMatchRegex = /^PHP (\d+)\.(\d+)\.(\d+)/;
const loadDeps = () => {
if (!helpers) {
helpers = require('atom-linter');
}
if (!path) {
path = require('path');
}
};
export default {
activate() {
this.idleCallbacks = new Set();
let depsCallbackID;
const installLinterPhpDeps = () => {
this.idleCallbacks.delete(depsCallbackID);
if (!atom.inSpecMode()) {
require('atom-package-deps').install('linter-php');
}
loadDeps();
};
depsCallbackID = window.requestIdleCallback(installLinterPhpDeps);
this.idleCallbacks.add(depsCallbackID);
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(
atom.config.observe('linter-php.executablePath', (value) => {
this.executablePath = value;
}),
atom.config.observe('linter-php.errorReporting', (value) => {
this.errorReporting = value;
}),
atom.config.observe('linter-php.ignorePhpIni', (value) => {
this.ignorePhpIni = value;
}),
);
},
deactivate() {
this.idleCallbacks.forEach((callbackID) => window.cancelIdleCallback(callbackID));
this.idleCallbacks.clear();
this.subscriptions.dispose();
},
provideLinter() {
return {
name: 'PHP',
grammarScopes: ['text.html.php', 'source.php'],
scope: 'file',
lintsOnChange: true,
lint: async (textEditor) => {
if (!atom.workspace.isTextEditor(textEditor)) {
return null;
}
const filePath = textEditor.getPath();
const fileText = textEditor.getText();
// Ensure that the dependencies are loaded
loadDeps();
const parameters = [
'--syntax-check',
'--define', 'display_errors=On',
'--define', 'log_errors=Off',
];
if (this.errorReporting) {
parameters.push('--define', 'error_reporting=E_ALL');
}
if (this.ignorePhpIni) {
// No configuration (ini) files will be used
parameters.push('-n');
}
const execOptions = {
stdin: fileText,
ignoreExitCode: true,
};
if (filePath) {
// Only specify a CWD if the file has been saved
const [projectPath] = atom.project.relativizePath(filePath);
execOptions.cwd = projectPath !== null ? projectPath : path.dirname(filePath);
}
const output = await helpers.exec(this.executablePath, parameters, execOptions);
if (textEditor.getText() !== fileText) {
// Editor contents have changed, don't update messages
return null;
}
const messages = [];
let match = parseRegex.exec(output);
while (match !== null) {
const line = Number.parseInt(match[3], 10) - 1;
const errorType = match[1];
messages.push({
severity: (/error/i.test(errorType) ? 'error' : 'warning'),
location: {
file: filePath,
position: helpers.generateRange(textEditor, line),
},
excerpt: match[2],
});
match = parseRegex.exec(output);
}
return messages;
},
};
},
async getPhpVersionInfo() {
const execOptions = {
ignoreExitCode: true,
};
const output = await helpers.exec(this.executablePath, ['--version'], execOptions);
const match = phpVersionMatchRegex.exec(output);
return {
major: match[1],
minor: match[2],
patch: match[3],
};
},
};