| 1 |
// PHP Linter to CodeMirror, copyright (c) by Ioan CHIRIAC |
| 2 |
// Distributed under a BSD 3-Clause License : https://github.com/glayzzle/codemirror-linter/blob/master/LICENSE |
| 3 |
|
| 4 |
// Note: Adding a variable to make it work with WordPress' implementation of CodeMirror |
| 5 |
var CodeMirror = wp.CodeMirror; |
| 6 |
|
| 7 |
(function(mod) { |
| 8 |
if (typeof exports == "object" && typeof module == "object") { |
| 9 |
mod(require("cm/lib/codemirror"), require('php-parser')); |
| 10 |
} else { |
| 11 |
mod(CodeMirror, require('php-parser')); |
| 12 |
} |
| 13 |
})(function(CodeMirror, phpParser) { |
| 14 |
"use strict"; |
| 15 |
|
| 16 |
var messages = []; |
| 17 |
|
| 18 |
/** |
| 19 |
* Adds a new linting message |
| 20 |
*/ |
| 21 |
function addMessage(msg, position, type) { |
| 22 |
var start, end; |
| 23 |
if (position.lineNumber && position.columnNumber) { |
| 24 |
start = CodeMirror.Pos(position.lineNumber - 1, position.columnNumber - 1); |
| 25 |
end = CodeMirror.Pos(position.lineNumber - 1, position.columnNumber); |
| 26 |
} else if (position.start && position.end) { |
| 27 |
if (position.end.offset < position.start.offset) { |
| 28 |
end = CodeMirror.Pos(position.start.line - 1, position.start.column); |
| 29 |
start = CodeMirror.Pos(position.end.line - 1, position.end.column); |
| 30 |
} else { |
| 31 |
start = CodeMirror.Pos(position.start.line - 1, position.start.column); |
| 32 |
end = CodeMirror.Pos(position.end.line - 1, position.end.column); |
| 33 |
} |
| 34 |
} |
| 35 |
messages.push({ |
| 36 |
message: msg, |
| 37 |
severity: type, |
| 38 |
from: start, |
| 39 |
to: end |
| 40 |
}); |
| 41 |
} |
| 42 |
|
| 43 |
function addError(msg, position) { |
| 44 |
addMessage(msg, position, "error"); |
| 45 |
} |
| 46 |
|
| 47 |
function addWarning(msg, position) { |
| 48 |
addMessage(msg, position, "warning"); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Recursive ndoe visitor |
| 53 |
*/ |
| 54 |
function visit(node, opt) { |
| 55 |
if (node.hasOwnProperty('kind')) { |
| 56 |
validate(node, opt); |
| 57 |
} |
| 58 |
for(var k in node) { |
| 59 |
if (node.hasOwnProperty(k)) { |
| 60 |
var child = node[k]; |
| 61 |
if (!child) continue; |
| 62 |
if (child.hasOwnProperty('kind')) { |
| 63 |
visit(child, opt); |
| 64 |
} else if (Array.isArray(child)) { |
| 65 |
child.forEach(function(item) { |
| 66 |
visit(item, opt); |
| 67 |
}); |
| 68 |
} else if (typeof child === 'object') { |
| 69 |
visit(child, opt); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Validation function |
| 77 |
*/ |
| 78 |
function validate(node, opt) { |
| 79 |
if (opt.disableEval) { |
| 80 |
if (node.kind === 'eval') { |
| 81 |
return addWarning('Eval is evil', node.loc); |
| 82 |
} |
| 83 |
if (node.kind === 'call' && node.what.name === 'create_function') { |
| 84 |
return addWarning('Eval is evil', node.loc); |
| 85 |
} |
| 86 |
} |
| 87 |
if (node.kind === 'exit' && opt.disableExit) { |
| 88 |
return addWarning('You should not use exit or die', node.loc); |
| 89 |
} |
| 90 |
if (opt.disablePHP7) { |
| 91 |
if (node.kind === 'array' && node.shortForm) { |
| 92 |
return addWarning('PHP 7 feature disabled', node.loc); |
| 93 |
} |
| 94 |
// ... todo |
| 95 |
} |
| 96 |
if (opt.disabledFunctions) { |
| 97 |
if (node.kind === 'call' && opt.disabledFunctions.indexOf(node.what.name) > -1) { |
| 98 |
return addError('Function "' + node.what.name + '" is not available', node.what.loc); |
| 99 |
} |
| 100 |
} |
| 101 |
if (opt.deprecatedFunctions) { |
| 102 |
if (node.kind === 'call' && opt.deprecatedFunctions.indexOf(node.what.name) > -1) { |
| 103 |
return addWarning('Function "' + node.what.name + '" is deprecated', node.what.loc); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// validate some code |
| 109 |
CodeMirror.registerHelper("lint", "php", function phpLint(text, options, cm) { |
| 110 |
messages = []; |
| 111 |
if (phpParser) { |
| 112 |
try { |
| 113 |
// Check if startOpen is false (code starts directly in PHP mode) |
| 114 |
var mode = cm.getOption('mode'); |
| 115 |
var startOpen = mode && mode.startOpen !== undefined ? mode.startOpen : true; |
| 116 |
|
| 117 |
// If startOpen is false, wrap code with PHP tags for parsing |
| 118 |
var codeToLint = text; |
| 119 |
if (!startOpen) { |
| 120 |
codeToLint = '<?php\n' + text; |
| 121 |
} |
| 122 |
|
| 123 |
var ast = phpParser.parseCode(codeToLint, { |
| 124 |
parser: { |
| 125 |
suppressErrors: true |
| 126 |
}, |
| 127 |
ast: { |
| 128 |
withPositions: true |
| 129 |
} |
| 130 |
}); |
| 131 |
// check errors |
| 132 |
if (ast.errors && ast.errors.length > 0) { |
| 133 |
for(var i = 0; i < ast.errors.length; i++) { |
| 134 |
var error = ast.errors[i]; |
| 135 |
// Adjust line numbers if we added the PHP tag |
| 136 |
if (!startOpen && error.loc && error.loc.start) { |
| 137 |
error.loc.start.line = Math.max(1, error.loc.start.line - 1); |
| 138 |
if (error.loc.end) { |
| 139 |
error.loc.end.line = Math.max(1, error.loc.end.line - 1); |
| 140 |
} |
| 141 |
} |
| 142 |
addError(error.message, error.loc); |
| 143 |
} |
| 144 |
} |
| 145 |
// filter nodes |
| 146 |
visit(ast, options); |
| 147 |
} catch(e) { |
| 148 |
addError(e.message, e); |
| 149 |
} |
| 150 |
} else if (window.console) { |
| 151 |
window.console.error("Error: php-parser not defined, CodeMirror PHP linting cannot run."); |
| 152 |
} |
| 153 |
return messages; |
| 154 |
}); |
| 155 |
|
| 156 |
}); |
| 157 |
|