Last active
December 20, 2020 06:33
-
-
Save mervick/ca6cdbc618928d81588d430f3ab1101a to your computer and use it in GitHub Desktop.
Modified JetBrains eslint-plugin for PhpStorm 2017 with support Eslint 6 and Eslint 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var __assign = (this && this.__assign) || function () { | |
__assign = Object.assign || function(t) { | |
for (var s, i = 1, n = arguments.length; i < n; i++) { | |
s = arguments[i]; | |
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | |
t[p] = s[p]; | |
} | |
return t; | |
}; | |
return __assign.apply(this, arguments); | |
}; | |
exports.__esModule = true; | |
exports.ESLintPlugin = void 0; | |
var ESLintResponse = /** @class */ (function () { | |
function ESLintResponse(request_seq, command) { | |
this.request_seq = request_seq; | |
this.command = command; | |
} | |
return ESLintResponse; | |
}()); | |
var FileKind; | |
(function (FileKind) { | |
FileKind["js"] = "js"; | |
FileKind["ts"] = "ts"; | |
FileKind["html"] = "html"; | |
FileKind["vue"] = "vue"; | |
})(FileKind || (FileKind = {})); | |
var ESLintPlugin = (function () { | |
function ESLintPlugin(state) { | |
this.includeSourceText = state.includeSourceText; | |
this.additionalRulesDirectory = state.additionalRootDirectory; | |
var eslintPackagePath; | |
if (state.standardPackagePath != null) { | |
var standardPackagePath = state.standardPackagePath; | |
this.standardLinter = requireInContext(standardPackagePath, state.packageJsonPath); | |
// Standard doesn't provide API to check if file is ignored (https://github.com/standard/standard/issues/1448). | |
// The only way is to use ESLint for that. | |
eslintPackagePath = findESLintPackagePath(standardPackagePath, state.packageJsonPath); | |
} | |
else { | |
eslintPackagePath = state.eslintPackagePath; | |
} | |
eslintPackagePath = normalizePath(eslintPackagePath); | |
this.options = requireInContext(eslintPackagePath + "lib/options", state.packageJsonPath); | |
this.cliEngineCtor = requireInContext(eslintPackagePath + "lib/api", state.packageJsonPath).CLIEngine; | |
} | |
ESLintPlugin.prototype.onMessage = function (p, writer) { | |
var body; | |
var request = JSON.parse(p); | |
var response = new ESLintResponse(request.seq, request.command); | |
try { | |
if (request.command === "GetErrors") { | |
body = this.getErrors(request.arguments); | |
} | |
else if (request.command === "FixErrors") { | |
body = this.fixErrors(request.arguments); | |
} | |
if (this.filterSource == null || this.filterSource) { | |
ESLintPlugin.filterSourceOut(body); | |
} | |
response.body = body; | |
} | |
catch (e) { | |
response.error = e.toString() + "\n\n" + e.stack; | |
} | |
finally { | |
writer.write(JSON.stringify(response)); | |
} | |
}; | |
ESLintPlugin.filterSourceOut = function (body) { | |
for (var i = 0; i < body.length; i++) { | |
var elem = body[i]; | |
if (elem != null) { | |
if (elem.source != null) | |
elem.source = ""; | |
if (elem.messages != null) { | |
for (var j = 0; j < elem.messages.length; j++) { | |
var message = elem.messages[j]; | |
if (message.source != null) | |
message.source = ""; | |
} | |
} | |
} | |
} | |
}; | |
ESLintPlugin.prototype.getErrors = function (requestArguments) { | |
var args = this.createArguments(requestArguments); | |
var parsedOptions = this.options.parse(args); | |
parsedOptions.ext = ".ts,.tsx,.js,.jsx".split(","); | |
parsedOptions.ignorePath = requestArguments.ignoreFilePath; | |
var cliEngine = new this.cliEngineCtor(ESLintPlugin.translateOptions(parsedOptions)); | |
if (cliEngine.isPathIgnored(requestArguments.fileName)) | |
return []; | |
if (this.standardLinter != null) { | |
var standardOptions = { filename: requestArguments.fileName }; | |
return this.standardLinter.lintTextSync(requestArguments.content, standardOptions); | |
} | |
return cliEngine.executeOnText(requestArguments.content, requestArguments.fileName).results; | |
}; | |
ESLintPlugin.formatResults = function (report, cliEngine, parsedOptions) { | |
var output = cliEngine.getFormatter(parsedOptions.format)(report.results); | |
// todo: too many warnings count | |
return JSON.parse(output); | |
}; | |
ESLintPlugin.prototype.fixErrors = function (requestArguments) { | |
var args = this.createArguments(requestArguments); | |
var parsedOptions = this.options.parse(args); | |
parsedOptions.ext = ".ts,.tsx,.js,.jsx".split(","); | |
parsedOptions.ignorePath = requestArguments.ignoreFilePath; | |
var cliEngine = new this.cliEngineCtor(ESLintPlugin.translateOptions(parsedOptions)); | |
if (cliEngine.isPathIgnored(requestArguments.fileName)) | |
return []; | |
if (this.standardLinter != null) { | |
var standardOptions = { filename: requestArguments.fileName, fix: true }; | |
return this.standardLinter.lintTextSync(requestArguments.content, standardOptions); | |
} | |
return cliEngine.executeOnText(requestArguments.content, requestArguments.fileName).results; | |
}; | |
ESLintPlugin.prototype.createArguments = function (getErrorsArguments) { | |
var args = ""; | |
if (getErrorsArguments.configPath != null) { | |
args += "-c \"" + getErrorsArguments.configPath + "\""; | |
} | |
args += " --format=json --ext .ts,.tsx,.js,.jsx"; | |
if (getErrorsArguments.extraOptions != null && getErrorsArguments.extraOptions.length > 0) { | |
args += " " + getErrorsArguments.extraOptions; | |
} | |
if (this.additionalRulesDirectory != null && this.additionalRulesDirectory.length > 0) { | |
args += " --rulesdir=\"" + this.additionalRulesDirectory + "\""; | |
} | |
return args; | |
}; | |
ESLintPlugin.prototype.calcBasicPath = function (eslintPackagePath) { | |
if (eslintPackagePath.charAt(eslintPackagePath.length - 1) !== '/' && | |
eslintPackagePath.charAt(eslintPackagePath.length - 1) !== '\\') { | |
eslintPackagePath = eslintPackagePath + '/'; | |
} | |
eslintPackagePath = eslintPackagePath.split("\\").join("/"); | |
this.basicPath = eslintPackagePath; | |
}; | |
ESLintPlugin.prototype.readVersion = function () { | |
var fs = require("fs"); | |
var packageJsonPath = this.basicPath + "/package.json"; | |
if (!fs.existsSync(packageJsonPath)) { | |
this.initError = "Can not find package.json under '" + this.basicPath + "'"; | |
return null; | |
} | |
var contents = fs.readFileSync(packageJsonPath); | |
try { | |
var json = JSON.parse(contents); | |
return json["version"]; | |
} | |
catch (e) { | |
this.initError = "Can not parse '" + packageJsonPath + "':\n" + e.toString() + "\n\n" + e.stack; | |
} | |
return null; | |
}; | |
// taken from privtae part of eslint, we need it here | |
/** | |
* Translates the CLI options into the options expected by the CLIEngine. | |
* @param {Object} cliOptions The CLI options to translate. | |
* @returns {CLIEngineOptions} The options object for the CLIEngine. | |
* @private | |
*/ | |
ESLintPlugin.translateOptions = function (cliOptions) { | |
return { | |
envs: cliOptions.env, | |
extensions: cliOptions.ext, | |
rules: cliOptions.rule, | |
plugins: cliOptions.plugin, | |
globals: cliOptions.global, | |
ignore: cliOptions.true, | |
ignorePath: cliOptions.ignorePath, | |
ignorePattern: cliOptions.ignorePattern, | |
configFile: cliOptions.config, | |
rulePaths: cliOptions.rulesdir, | |
useEslintrc: cliOptions.eslintrc, | |
parser: cliOptions.parser, | |
parserOptions: cliOptions.parserOptions, | |
cache: cliOptions.cache, | |
cacheFile: cliOptions.cacheFile, | |
cacheLocation: cliOptions.cacheLocation, | |
fix: cliOptions.fix, | |
allowInlineConfig: cliOptions.inlineConfig | |
}; | |
}; | |
return ESLintPlugin; | |
}()); | |
exports.ESLintPlugin = ESLintPlugin; | |
function containsString(src, toFind) { | |
return src != null && src.indexOf(toFind) >= 0; | |
} | |
function normalizePath(eslintPackagePath) { | |
if (eslintPackagePath === undefined) | |
return undefined; | |
if (eslintPackagePath.charAt(eslintPackagePath.length - 1) !== '/' && | |
eslintPackagePath.charAt(eslintPackagePath.length - 1) !== '\\') { | |
eslintPackagePath = eslintPackagePath + '/'; | |
} | |
return toUnixPathSeparators(eslintPackagePath); | |
} | |
function toUnixPathSeparators(path) { | |
return path.split("\\").join("/"); | |
} | |
function findESLintPackagePath(standardPackagePath, contextPath) { | |
var resolvedStandardPackagePath = requireResolveInContext(standardPackagePath, contextPath); | |
var requirePath = require.resolve("eslint", { paths: [resolvedStandardPackagePath] }); | |
requirePath = toUnixPathSeparators(requirePath); | |
var eslintPackageStr = "/eslint/"; | |
var ind = requirePath.lastIndexOf(eslintPackageStr); | |
if (ind < 0) { | |
throw Error("Cannot find eslint package for " + requirePath); | |
} | |
return requirePath.substring(0, ind + eslintPackageStr.length); | |
} | |
function requireInContext(modulePathToRequire, contextPath) { | |
var contextRequire = getContextRequire(contextPath); | |
return contextRequire(modulePathToRequire); | |
} | |
function requireResolveInContext(modulePathToRequire, contextPath) { | |
var contextRequire = getContextRequire(contextPath); | |
return contextRequire.resolve(modulePathToRequire); | |
} | |
function getContextRequire(contextPath) { | |
if (contextPath != null) { | |
var module_1 = require('module'); | |
if (typeof module_1.createRequire === 'function') { | |
// https://nodejs.org/api/module.html#module_module_createrequire_filename | |
// Implemented in Yarn PnP: https://next.yarnpkg.com/advanced/pnpapi/#requiremodule | |
return module_1.createRequire(contextPath); | |
} | |
// noinspection JSDeprecatedSymbols | |
if (typeof module_1.createRequireFromPath === 'function') { | |
// Use createRequireFromPath (a deprecated version of createRequire) to support Node.js 10.x | |
// noinspection JSDeprecatedSymbols | |
return module_1.createRequireFromPath(contextPath); | |
} | |
throw Error('Function module.createRequire is unavailable in Node.js ' + process.version + | |
', Node.js >= 12.2.0 is required'); | |
} | |
return require; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment