-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
364 lines (322 loc) · 10.6 KB
/
index.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
'use strict'
const path = require('path')
const getSettings = require('./settings').getSettings
const BOM = "\uFEFF"
const GET_SCOPE_RULE_NAME = "__eslint-plugin-php-markup-get-scope"
const DECLARE_VARIABLES_RULE_NAME = "__eslint-plugin-php-markup-declare-variables"
const LINTER_ISPATCHED_PROPERTY_NAME =
"__eslint-plugin-php-markup-verify-function-is-patched"
function remapMessages(ctx, messages, code, settings) {
var ms = []
var startPosition = ctx.filtered[0].start.position
var start
for (var i = 0; i < messages[0].length; i++) {
var message = messages[0][i]
if (!start) {
var position = getPosition(message, code.lineStartIndices)
if (position > startPosition) {
start = true
}
}
if (start) {
if (settings.removePHPLint) {
var pos = getOriginalPosition(ctx, position)
if (inPHPCode(ctx, pos)) {
continue
}
if (message.fix && message.fix.range) {
pos = getOriginalPosition(ctx, message.fix.range[0])
if (inPHPCode(ctx, pos)) {
continue
}
}
}
var loc = getOriginalLocation(ctx, message)
message.line = loc.line
message.column = loc.column
// Map fix range
if (message.fix && message.fix.range) {
message.fix.range = [
getOriginalPosition(ctx, message.fix.range[0]),
// The range end is exclusive, meaning it should replace all characters with indexes from
// start to end - 1. We have to get the original index of the last targeted character.
getOriginalPosition(ctx, message.fix.range[1] - 1) + 1,
]
}
// Map end location
if (message.endLine && message.endColumn) {
loc = getOriginalLocation(ctx, {
line: message.endLine,
column: message.endColumn
}, code.lineStartIndices)
message.endLine = loc.line
message.endColumn = loc.column
}
}
ms.push(message)
}
if (settings.removePHPLint) {
messages[0] = ms
}
}
function getLineStartIndices(text) {
return text.split('\n').map(s => s.length).reduce((prev, current) => {
prev.push(prev[prev.length - 1] + current + 1)
return prev
}, [0])
}
function getLocation(position, lineStartIndices) {
var i
for (i = 1; i < lineStartIndices.length; i++) {
if (position >= lineStartIndices[i - 1] && position < lineStartIndices[i]) {
break
}
}
return {
line: i,
column: position - lineStartIndices[i - 1] + 1
}
}
function getPosition(loc, lineStartIndices) {
return lineStartIndices[loc.line - 1] + loc.column - 1
}
function getOriginalPosition(ctx, position) {
for (var i = 0; i < ctx.filtered.length; i++) {
var f = ctx.filtered[i]
if (position > f.start.position) {
// remove the length of replaced text
position = position + (f.end.position - f.start.position) - f.replacement.length
}
}
return position
}
function getOriginalLocation(ctx, loc) {
var position = getPosition(loc, ctx.code.lineStartIndices)
for (var i = 0; i < ctx.filtered.length; i++) {
var f = ctx.filtered[i]
if (position > f.start.position) {
// remove the length of replaced text
position = position + (f.end.position - f.start.position) - f.replacement.length
}
}
return getLocation(position, ctx.originalLineStartIndices)
}
function inPHPCode(ctx, position) {
for (var i = 0; i < ctx.filtered.length; i++) {
var f = ctx.filtered[i]
if (position >= f.start.position && position < f.end.position) {
return true
}
}
return false
}
function isWhitespace(chr) {
return chr === ' ' || chr === '\t'
}
function isNewline(text, pos) {
return text[pos] === '\n' || text.substr(pos, 2) === '\r\n'
}
var _messages = []
var PHP_MARKUP = /<\?[\s\S]*?\?>/g
var PHP_MARKUP_EOL = /<\?[\s\S]*?\?>(\r?\n)?/g
var ctxIndex = -1
var processor = {
preprocess: (text, filename, settings) => {
if (typeof text === 'string') {
var m, found = false, ms = []
var filteredText = ''
var originalLineStartIndices
var regex = settings.keepEOL ? PHP_MARKUP : PHP_MARKUP_EOL
// reset match position
regex.lastIndex = 0
do {
var lastIndex = regex.lastIndex
m = regex.exec(text)
if (m) {
if (!found) {
found = true
originalLineStartIndices = getLineStartIndices(text)
}
var startPosition = m.index
var markupText = text.substr(startPosition + 2, 1)
var isEmptyLine = false
if (settings.removeWhitespace || settings.removeEmptyLine) {
while (startPosition > 0 && isWhitespace(text[startPosition - 1])) {
startPosition--
}
if (startPosition === 0 || text[startPosition - 1] === '\n') {
// empty whitespaces before php markup
if (isNewline(text, settings.keepEOL ? regex.lastIndex : regex.lastIndex - 1)) {
isEmptyLine = true
m.index = startPosition
if (settings.keepEOL && settings.removeEmptyLine) {
while (text[regex.lastIndex] !== '\n') {
regex.lastIndex++
}
}
}
}
}
var startLoc = getLocation(m.index, originalLineStartIndices)
startLoc.position = m.index
var endLoc = getLocation(regex.lastIndex, originalLineStartIndices)
endLoc.position = regex.lastIndex
var replacement
if (isEmptyLine && settings.removeEmptyLine) {
replacement = ''
} else {
replacement = markupText === '='
? settings.markupReplacement['='] : settings.markupReplacement['php']
}
ms.push({
start: startLoc,
end: endLoc,
replacement,
})
filteredText += text.substr(lastIndex, m.index - lastIndex) + replacement
} else {
filteredText += text.substr(lastIndex)
}
} while (m)
ctxIndex++
_messages.push({
source: text,
filtered: ms,
code: { lineStartIndices: getLineStartIndices(filteredText) },
originalLineStartIndices: originalLineStartIndices
})
return [filteredText]
} else {
_messages[ctxIndex].code = text
return [text]
}
},
postprocess: (messages, filename, settings, hasBOM) => {
if (ctxIndex >= 0 && _messages[ctxIndex].filtered.length > 0) {
var m = _messages[ctxIndex]
remapMessages(m, messages, m.code, settings)
}
return messages[0]
},
supportsAutofix: true
}
// Monkey patch for `Linter.prototype.verify`
// Inspired by [eslint-plugin-html](https://github.com/BenoitZugmeyer/eslint-plugin-html)
const needles = [
path.join("lib", "linter", "linter.js"), // ESLint 6+
path.join("lib", "linter.js"), // ESLint 5-
]
iterateESLintModules(patch)
function getLinterFromModule(moduleExports) {
return moduleExports.Linter
? moduleExports.Linter // ESLint 6+
: moduleExports // ESLint 5-
}
function getModuleFromRequire() {
return getLinterFromModule(require("eslint/lib/linter"))
}
function getModuleFromCache(key) {
if (!needles.some(needle => key.endsWith(needle))) return
const module = require.cache[key]
if (!module || !module.exports) return
const Linter = getLinterFromModule(module.exports)
if (
typeof Linter === "function" &&
typeof Linter.prototype.verify === "function"
) {
return Linter
}
}
function iterateESLintModules(fn) {
if (!require.cache || Object.keys(require.cache).length === 0) {
// Jest is replacing the node "require" function, and "require.cache" isn't available here.
fn(getModuleFromRequire())
return
}
let found = false
for (const key in require.cache) {
const Linter = getModuleFromCache(key)
if (Linter) {
fn(Linter)
found = true
}
}
if (!found) {
let eslintPath, eslintVersion
try {
eslintPath = require.resolve("eslint")
} catch (e) {
eslintPath = "(not found)"
}
try {
eslintVersion = require("eslint/package.json").version
} catch (e) {
eslintVersion = "n/a"
}
const parentPaths = module =>
module ? [module.filename].concat(parentPaths(module.parent)) : []
throw new Error(
`eslint-plugin-php-markup error: It seems that eslint is not loaded.
If you think this is a bug, please file a report at https://github.com/tengattack/eslint-plugin-php-markup/issues
In the report, please include *all* those informations:
* ESLint version: ${eslintVersion}
* ESLint path: ${eslintPath}
* Plugin version: ${require("../package.json").version}
* Plugin inclusion paths: ${parentPaths(module).join(", ")}
* NodeJS version: ${process.version}
* CLI arguments: ${JSON.stringify(process.argv)}
* Content of your lock file (package-lock.json or yarn.lock) or the output of \`npm list\`
* How did you run ESLint (via the command line? an editor plugin?)
* The following stack trace:
${new Error().stack.slice(10)}
`
)
}
}
function patch(Linter) {
const verifyMethodName = Linter.prototype._verifyWithoutProcessors
? "_verifyWithoutProcessors" // ESLint 6+
: "verify" // ESLint 5-
const verify = Linter.prototype[verifyMethodName]
// ignore if verify function is already been patched sometime before
if (Linter[LINTER_ISPATCHED_PROPERTY_NAME] === true) {
return
}
Linter[LINTER_ISPATCHED_PROPERTY_NAME] = true
Linter.prototype[verifyMethodName] = function(
textOrSourceCode,
config,
filenameOrOptions,
saveState
) {
const localVerify = code =>
verify.call(this, code, config, filenameOrOptions, saveState)
let messages
const filename =
typeof filenameOrOptions === "object"
? filenameOrOptions.filename
: filenameOrOptions
const extension = path.extname(filename || "")
const pluginSettings = getSettings(config.settings || {})
const isPHP = pluginSettings.phpExtensions.indexOf(extension) >= 0
if (isPHP) {
messages = []
const pushMessages = (localMessages, code) => {
messages.push.apply(
messages,
processor.postprocess([localMessages], code, pluginSettings, false) //textOrSourceCode.startsWith(BOM))
)
}
const currentCodes = processor.preprocess(textOrSourceCode, filename, pluginSettings)
for (const code of currentCodes) {
pushMessages(localVerify(code), code)
}
messages.sort((ma, mb) => {
return ma.line - mb.line || ma.column - mb.column
})
} else {
messages = localVerify(textOrSourceCode)
}
return messages
}
}