Skip to content

Commit

Permalink
(chore) apply lint to our tooling scripts also (#3380)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hirse committed Nov 1, 2021
1 parent 2ccec7e commit cd1c9dc
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 143 deletions.
7 changes: 4 additions & 3 deletions .eslintrc.js
Expand Up @@ -28,9 +28,10 @@ module.exports = {
// for now ignore diff between types of quoting
quotes: "off",
// this is the style we are already using
"operator-linebreak": ["error", "before", { overrides: {
"=": "after"
}
"operator-linebreak": ["error", "before", {
overrides: {
"=": "after"
}
}],
// sometimes we declare variables with extra spacing
indent: ["error", 2, { VariableDeclarator: 2 }],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -37,7 +37,7 @@
"types": "./types/index.d.ts",
"scripts": {
"mocha": "mocha",
"lint": "eslint src/*.js src/lib/*.js demo/*.js",
"lint": "eslint src/*.js src/lib/*.js demo/*.js tools/**/*.js --ignore-pattern vendor",
"lint-languages": "eslint --no-eslintrc -c .eslintrc.lang.js src/languages/**/*.js",
"build_and_test": "npm run build && npm run test",
"build_and_test_browser": "npm run build-browser && npm run test-browser",
Expand Down
6 changes: 3 additions & 3 deletions tools/build.js
Expand Up @@ -62,7 +62,7 @@

const commander = require('commander');
const path = require('path');
const { clean } = require("./lib/makestuff");
const { clean } = require("./lib/makestuff.js");
const log = (...args) => console.log(...args);

const TARGETS = ["cdn", "browser", "node"];
Expand All @@ -73,8 +73,8 @@ commander
.option('-n, --no-minify', 'Disable minification')
.option('-ne, --no-esm', 'Disable building ESM')
.option('-t, --target <name>',
'Build for target ' +
'[all, browser, cdn, node]',
'Build for target '
+ '[all, browser, cdn, node]',
'browser')
.parse(process.argv);

Expand Down
32 changes: 16 additions & 16 deletions tools/build_browser.js
Expand Up @@ -6,27 +6,27 @@ const path = require("path");
const zlib = require('zlib');
const Terser = require("terser");
const child_process = require('child_process');
const { getLanguages } = require("./lib/language");
const { filter } = require("./lib/dependencies");
const config = require("./build_config");
const { install, installCleanCSS, mkdir, renderTemplate } = require("./lib/makestuff");
const { getLanguages } = require("./lib/language.js");
const { filter } = require("./lib/dependencies.js");
const config = require("./build_config.js");
const { install, installCleanCSS, mkdir, renderTemplate } = require("./lib/makestuff.js");
const log = (...args) => console.log(...args);
const { rollupCode } = require("./lib/bundling.js");
const bundling = require('./lib/bundling.js');
const Table = require('cli-table');

const getDefaultHeader = () => ({
...require('../package.json'),
git_sha : child_process
git_sha: child_process
.execSync("git rev-parse --short=10 HEAD")
.toString().trim(),
.toString().trim()
});
function buildHeader(args = getDefaultHeader()) {
return "/*!\n" +
` Highlight.js v${args.version} (git: ${args.git_sha})\n` +
` (c) ${config.copyrightYears} ${args.author.name} and other contributors\n` +
` License: ${args.license}\n` +
` */`;
return "/*!\n"
+ ` Highlight.js v${args.version} (git: ${args.git_sha})\n`
+ ` (c) ${config.copyrightYears} ${args.author.name} and other contributors\n`
+ ` License: ${args.license}\n`
+ ` */`;
}

function sortByKey(array, key) {
Expand All @@ -41,9 +41,9 @@ function detailedGrammarSizes(languages) {
if (languages.length > 180) return;

const resultTable = new Table({
head: ['lang','minified'],
head: ['lang', 'minified'],
// colWidths: [20,20,10,20,10,20],
chars: {'mid': '', 'left-mid': '', 'mid-mid': '', 'right-mid': ''},
chars: { mid: '', 'left-mid': '', 'mid-mid': '', 'right-mid': '' },
style: {
head: ['grey']
}
Expand Down Expand Up @@ -230,19 +230,19 @@ async function buildCore(name, languages, options) {
const sizeInfo = { shas: [] };
const writePromises = [];
if (options.minify) {
const { code } = await Terser.minify(index, {...config.terser, module: (options.format === "es") });
const { code } = await Terser.minify(index, { ...config.terser, module: (options.format === "es") });
const src = `${header}\n${code}`;
writePromises.push(fs.writeFile(output.file.replace(/js$/, "min.js"), src));
sizeInfo.minified = src.length;
sizeInfo.minifiedSrc = src;
sizeInfo.shas[`${relativePath}${name}.min.js`] = bundling.sha384(src)
sizeInfo.shas[`${relativePath}${name}.min.js`] = bundling.sha384(src);
}
{
const src = `${header}\n${index}`;
writePromises.push(fs.writeFile(output.file, src));
sizeInfo.fullSize = src.length;
sizeInfo.fullSrc = src;
sizeInfo.shas[`${relativePath}${name}.js`] = bundling.sha384(src)
sizeInfo.shas[`${relativePath}${name}.js`] = bundling.sha384(src);
}
await Promise.all(writePromises);
return sizeInfo;
Expand Down
4 changes: 2 additions & 2 deletions tools/build_cdn.js
Expand Up @@ -57,7 +57,7 @@ async function buildCDN(options) {
if (options.esm) {
mkdir("es");
await fs.writeFile(`${process.env.BUILD_DIR}/es/package.json`, `{ "type": "module" }`);
esmCoreSize = await buildCore("core", [], {minify: options.minify, format: "es"});
esmCoreSize = await buildCore("core", [], { minify: options.minify, format: "es" });
esmCommonSize = await buildCore("highlight", embedLanguages, { minify: options.minify, format: "es" });
}
shas = {
Expand Down Expand Up @@ -157,7 +157,7 @@ async function buildDistributable(language, options) {
await fs.mkdir(distDir, { recursive: true });
await fs.writeFile(path.join(language.moduleDir, "dist", filename), language.minified);
if (options.esm) {
await fs.writeFile(path.join(language.moduleDir, "dist", filename.replace(".min.js",".es.min.js")), language.minifiedESM);
await fs.writeFile(path.join(language.moduleDir, "dist", filename.replace(".min.js", ".es.min.js")), language.minifiedESM);
}
}

Expand Down
27 changes: 14 additions & 13 deletions tools/build_node.js
@@ -1,29 +1,29 @@
const fs = require("fs").promises;
const fss = require("fs");
const config = require("./build_config");
const config = require("./build_config.js");
const glob = require("glob-promise");
const { getLanguages } = require("./lib/language");
const { install, mkdir, installCleanCSS } = require("./lib/makestuff");
const { filter } = require("./lib/dependencies");
const { getLanguages } = require("./lib/language.js");
const { install, mkdir, installCleanCSS } = require("./lib/makestuff.js");
const { filter } = require("./lib/dependencies.js");
const { rollupWrite } = require("./lib/bundling.js");
const log = (...args) => console.log(...args);

// https://nodejs.org/api/packages.html#packages_writing_dual_packages_while_avoiding_or_minimizing_hazards
async function buildESMStub(name) {
const code =
`// https://nodejs.org/api/packages.html#packages_writing_dual_packages_while_avoiding_or_minimizing_hazards\n` +
`import HighlightJS from '../lib/${name}.js';\n` +
`export { HighlightJS };\n` +
`export default HighlightJS;\n`;
`// https://nodejs.org/api/packages.html#packages_writing_dual_packages_while_avoiding_or_minimizing_hazards\n`
+ `import HighlightJS from '../lib/${name}.js';\n`
+ `export { HighlightJS };\n`
+ `export default HighlightJS;\n`;
await fs.writeFile(`${process.env.BUILD_DIR}/es/${name}.js`, code);
}

async function buildCJSIndex(name, languages) {
const header = "var hljs = require('./core');";
const footer =
`hljs.HighlightJS = hljs\n` +
`hljs.default = hljs\n` +
`module.exports = hljs;`;
`hljs.HighlightJS = hljs\n`
+ `hljs.default = hljs\n`
+ `module.exports = hljs;`;

const registration = languages.map((lang) => {
const require = `require('./languages/${lang.name}')`;
Expand Down Expand Up @@ -57,7 +57,8 @@ async function buildNodeLanguage(language, options) {
if (options.esm) {
await fs.writeFile(`${process.env.BUILD_DIR}/es/languages/${language.name}.js.js`,
ES_STUB.replace(/%%%%/g, language.name));
await rollupWrite(input, {...output,
await rollupWrite(input, {
...output,
format: "es",
file: output.file.replace("/lib/", "/es/")
});
Expand Down Expand Up @@ -108,7 +109,7 @@ const generatePackageExports = () => ({
"./lib/languages/*": dual("./lib/languages/*.js"),
"./scss/*": "./scss/*",
"./styles/*": "./styles/*",
"./types/*": "./types/*",
"./types/*": "./types/*"
});
function buildPackageJSON(options) {
const packageJson = require("../package.json");
Expand Down
38 changes: 18 additions & 20 deletions tools/checkAutoDetect.js
@@ -1,16 +1,16 @@
#!/usr/bin/env node
'use strict';

let fs = require('fs')
let hljs = require('../build');
let path = require('path');
let utility = require('../test/utility');
let Table = require('cli-table');
let colors = require('colors/safe');
const fs = require('fs');
const hljs = require('../build.js');
const path = require('path');
const utility = require('../test/utility.js');
const Table = require('cli-table');
const colors = require('colors/safe.js');

let resultTable = new Table({
head: ['expected', 'actual', 'score', '2nd best', 'score','info'],
colWidths: [20,20,10,20,10,20],
const resultTable = new Table({
head: ['expected', 'actual', 'score', '2nd best', 'score', 'info'],
colWidths: [20, 20, 10, 20, 10, 20],
style: {
head: ['grey']
}
Expand All @@ -24,8 +24,8 @@ function testAutoDetection(language, index, languages) {
return fs.readFileSync(filename, 'utf-8');
})
.forEach(function(content) {
const expected = language,
actual = hljs.highlightAuto(content);
const expected = language;
const actual = hljs.highlightAuto(content);
if (actual.language !== expected && actual.secondBest.language !== expected) {
return resultTable.push([
expected,
Expand All @@ -45,7 +45,7 @@ function testAutoDetection(language, index, languages) {
]);
}
// equal relevance is flagged
if (actual.relevance == actual.secondBest.relevance) {
if (actual.relevance === actual.secondBest.relevance) {
return resultTable.push([
expected,
actual.language,
Expand All @@ -68,18 +68,16 @@ if (process.env.ONLY_LANGUAGES) {
console.log('Checking auto-highlighting for ' + colors.grey(languages.length) + ' languages...');
languages.forEach((lang, index) => {
if (index % 60 === 0) { console.log(""); }
testAutoDetection(lang)
testAutoDetection(lang);
process.stdout.write(".");
});
console.log("\n")
console.log("\n");

if (resultTable.length === 0) {
console.log(colors.green('SUCCESS') + ' - ' + colors.green(languages.length) + ' of ' + colors.gray(languages.length) + ' languages passed auto-highlight check!')
console.log(colors.green('SUCCESS') + ' - ' + colors.green(languages.length) + ' of ' + colors.gray(languages.length) + ' languages passed auto-highlight check!');
} else {
console.log(
colors.red('ISSUES') + ' - ' + colors.red(resultTable.length) + ' of ' + colors.gray(languages.length) + ' languages have potential issues.' +
'\n' +
resultTable.toString());
colors.red('ISSUES') + ' - ' + colors.red(resultTable.length) + ' of ' + colors.gray(languages.length) + ' languages have potential issues.'
+ '\n'
+ resultTable.toString());
}


9 changes: 4 additions & 5 deletions tools/checkTheme.js
Expand Up @@ -2,7 +2,6 @@

const fs = require("fs");
const css = require("css");
const { match } = require("assert");
require("colors");

const CODE = {
Expand Down Expand Up @@ -166,19 +165,19 @@ function check_group(group, rules) {
});


let doesNotSupport = has_rules.map(x => x[1]).includes(false);
let skipped = has_rules.find(x => x[2]);
const doesNotSupport = has_rules.map(x => x[1]).includes(false);
const skipped = has_rules.find(x => x[2]);
if (doesNotSupport || skipped) {
console.log(group.name.yellow);
if (doesNotSupport) {
console.log(`- Theme does not fully support.`.brightMagenta);
}

has_rules.filter(x => !x[1]).forEach(([scope,_]) => {
has_rules.filter(x => !x[1]).forEach(([scope, _]) => {
const selector = scopeToSelector(scope);
console.log(`- scope ${scope.cyan} is not highlighted\n (css: ${selector.green})`);
});
has_rules.filter(x => x[2]).forEach(([scope,_]) => {
has_rules.filter(x => x[2]).forEach(([scope, _]) => {
console.log(` - scope ${scope.cyan} [purposely] un-highlighted.`.cyan);
});
console.log();
Expand Down
4 changes: 2 additions & 2 deletions tools/lib/bundling.js
@@ -1,8 +1,8 @@
const rollup = require('rollup')
const rollup = require('rollup');
const crypto = require("crypto");

async function rollupCode(inputOptions, outputOptions) {
const output = await generate(inputOptions, outputOptions)
const output = await generate(inputOptions, outputOptions);
return output[0].code;
}

Expand Down

0 comments on commit cd1c9dc

Please sign in to comment.