c9-core/plugins/c9.ide.language.javascript..../packager/eslint.js

133 wiersze
4.3 KiB
JavaScript
Czysty Zwykły widok Historia

2017-12-07 12:44:47 +00:00
// build script to bundle eslint and eslint-plugin-react in a format usable by language worker
2017-11-23 19:09:33 +00:00
var path = require("path");
var fs = require("fs");
2017-11-26 18:14:44 +00:00
function changeFile(filepath, handler, ignoreBackup) {
var value;
try {
value = fs.readFileSync(filepath + ".bak", "utf8");
} catch (e) {
value = fs.readFileSync(filepath, "utf8");
2017-12-07 12:44:47 +00:00
// backup the file in case the script is invoked again without clean npm install
2017-11-26 18:14:44 +00:00
if (!ignoreBackup)
fs.writeFileSync(filepath + ".bak", value, "utf8");
}
if (typeof handler == "function") {
value = handler(value);
} else {
handler.forEach(function(v) {
value = safeReplace(value, ...v);
});
}
2017-11-23 19:09:33 +00:00
fs.writeFileSync(filepath, value, "utf8");
}
2017-11-26 18:14:44 +00:00
function safeReplace(src, pattern, value, count) {
2017-12-07 12:44:47 +00:00
var matchCount = 0;
var replacer = value
if (typeof replacer == "string") {
replacer = function(...args) {
// expand placeholders of form $i in the replacement string
return value.replace(/[$](\d)/g, (_, i) => args[i]);
}
}
2017-11-26 18:14:44 +00:00
src = src.replace(pattern, function(...args) {
2017-12-07 12:44:47 +00:00
matchCount++;
return replacer(...args);
2017-11-26 18:14:44 +00:00
});
2017-12-07 12:44:47 +00:00
if (count != undefined && count != matchCount)
throw new Error(`expected "${pattern}" to match ${count} times instead of ${n}`);
2017-11-26 18:14:44 +00:00
return src;
}
var loadRulesPath = require.resolve("eslint/lib/load-rules");
2017-11-23 19:09:33 +00:00
var contents = `module.exports = function() {
var rules = {`;
2017-12-07 12:44:47 +00:00
var ruleNames = fs.readdirSync(path.join(loadRulesPath, "../rules"));
ruleNames.forEach(function(file) {
if (file.endsWith(".js")) {
2017-11-23 19:09:33 +00:00
file = file.slice(0, -3);
contents += ' "' + file + '": ' + 'require("eslint/lib/rules/' + file + '"),\n';
}
});
contents += `}\n
var jsxRules = require("eslint-plugin-react").rules;
Object.keys(jsxRules).forEach(function(key) { rules["react/" + key] = jsxRules[key]; })
return rules
};
2017-11-26 18:14:44 +00:00
`;
2017-11-23 19:09:33 +00:00
changeFile(loadRulesPath, function() { return contents });
2017-11-26 18:14:44 +00:00
changeFile(require.resolve("eslint/lib/linter"), [
[/^/, `//a
require('core-js/es6');
require('core-js/fn/symbol');
require('core-js/fn/set');
require('core-js/fn/array/from');
require('core-js/fn/array/find-index');
require('core-js/fn/string/raw');
require('core-js/fn/map');
require('core-js/fn/weak-map');
require("regenerator-runtime/runtime");
`.replace(/^\s*/gm, ""), 1],
['require(config.parser)', 'require("espree")', 1],
[`require("./config/config-validator")`, `{validateRuleOptions: x=>x}`, 1]
]);
2017-11-23 19:09:33 +00:00
changeFile(require.resolve("espree/espree"), function(src) {
return src.replace(/acornOptions = {[^}\s]*/g, 'acornOptions = {allowImportExportEverywhere:true,')
.replace(/(function isValid(?:Node|Token)\(\w+\) {)(?!ret)/g, "$1return true;");
});
var webpack = require("webpack");
2017-11-26 18:14:44 +00:00
var outputPath = __dirname + "/../worker/eslint_browserified.js";
2017-11-23 19:09:33 +00:00
webpack({
entry: "eslint/lib/linter",
module: {
2017-11-26 18:14:44 +00:00
rules: [{
2017-11-23 19:09:33 +00:00
test: /\.js$/,
include: [
path.resolve(__dirname)
],
enforce: 'pre',
loader: "babel-loader",
options: {
presets: ["es2015"],
2017-11-26 18:14:44 +00:00
compact: false,
plugins: ["babel-plugin-unassert"]
2017-11-23 19:09:33 +00:00
},
}]
},
output: {
path: path.dirname(outputPath),
filename: path.basename(outputPath),
library: "eslint",
// libraryTarget: "window",
libraryTarget: "amd"
},
resolve: {
unsafeCache: true,
}
}, (err, stats) => {
if (err || stats.hasErrors()) {
2017-12-07 12:44:47 +00:00
return console.log(err, stats);
2017-11-23 19:09:33 +00:00
}
var commentRe = /^(;)?(?:\s*(?:\/\/.+\n|\/\*(?:[^*]|\*(?!\/))*\*\/))+(?: *\n)?/gm;
changeFile(outputPath, function(src) {
2017-11-23 19:25:59 +00:00
return "// generated using packager/eslint.js\n"
2017-11-23 19:09:33 +00:00
+ src.replace(commentRe, "$1")
2017-11-23 19:25:59 +00:00
.replace('define("eslint", ', "define(")
2017-11-26 18:14:44 +00:00
.replace("if (severityValue === 0 || severityValue === 1 || severityValue === 2) {", "if (typeof severityValue === 'number') {")
2017-11-23 19:25:59 +00:00
.replace(/^ {4,}/gm, function(indentation) {
2017-11-26 18:14:44 +00:00
return indentation.replace(indentation.length % 4 ? / {2}/g : / {4}/g, "\t");
2017-11-23 19:25:59 +00:00
});
2017-11-26 18:14:44 +00:00
}, true);
2017-11-23 19:09:33 +00:00
});