Skip to content

Commit

Permalink
lint: Run jshint when jshint config file is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Jan 1, 2025
1 parent e4e518e commit 42a793d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .github/litejs.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@
"*.js",
"lib/*.js"
]
},
"test": {
"tz": "Europe/Tallinn",
"_": [
"test/index.js"
]
}
}
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- run: npm install --no-audit && npm install -g uglify-js
- run: npm audit --audit-level=low
- run: npx c8 -r lcovonly -r text npm test
- run: npm run lint
- run: npx lj lint
- uses: coverallsapp/github-action@v2
name: Upload to coveralls.io
with:
Expand Down
3 changes: 0 additions & 3 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ function getExt(el) {
var ext = getSrc(el).split("?")[0].split(".").pop()
return ext === "tpl" || ext === "view" ? "ui" : ext
}
function resolve(name) {
return fs.existsSync(name) ? name : require.resolve(defMap(name))
}
function drop(el, content) {
var flags = el && el.getAttribute && el.getAttribute("drop")
return flags ? content.replace(
Expand Down
30 changes: 23 additions & 7 deletions lib/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,32 @@

module.exports = function(opts) {
var vm = require("vm")
, exit = 0
, child = require("child_process")
, cli = require("..")
, fileNameRe = /^(?:\/?[a-z0-9](?:[-.]?[A-Za-z0-9])*)+\.[a-z0-9]+$/
, invalidNameRe = /^[-\/]|[^-\/a-z0-9.]|[-\/]$/i
, whitespaceErrRe = /^ +\t|^\t+ |\r|[ \t]$/m
, files = cli.ls(opts._[0] ? opts._ : ["*.json","*.js"], { dir: false })
, files = cli.ls(opts._[0] ? opts._ : ["*.json", "*.js"], { dir: false })
, jsFiles = files.filter(byExt(".js"))
, formatNeeded = [].concat(
validate(files, "js", isValidJs),
validate(files, "json", isValidJson)
)
if (!files[0]) return console.error("No files to lint: " + opts._)
if (opts.jshint && jsFiles[0]) try {
child.execSync("npx jshint --config=" + opts.jshint + " " + jsFiles.join(" "), { stdio: ["ignore", 1, 2]})
} catch(e) {
exit = 1
}

if (formatNeeded.length > 0) {
console.log("lint error: Format needed: " + formatNeeded.join(", "))
process.exit(1)
} else {
console.log("# Lint OK %s files:", files.length, files.join(", "))
exit = 1
}

if (!files[0]) return console.error("No files to lint: " + opts._)
if (exit) process.exit(exit)
else console.log("# Lint OK %s files:", files.length, files.join(", "))

function validate(files, ext, validator) {
return files.filter(function(name) {
return name.split(".").pop() === ext && (
Expand All @@ -30,8 +40,14 @@ module.exports = function(opts) {
})
}

function byExt(ext) {
return function(name) {
return name.slice(-ext.length) === ext
}
}

function isValidName(name) {
if (fileNameRe.test(name)) return true
if (!invalidNameRe.test(name)) return true
console.error("Invalid file name:", JSON.stringify(name))
}

Expand Down
4 changes: 1 addition & 3 deletions opts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

/* litejs.com/MIT-LICENSE.txt */

exports.opts = opts

function opts(defaults, files, argv) {
exports.opts = function opts(defaults, files, argv) {
var expect, i, key, val
, path = require("path")
, short = {}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"lib"
],
"scripts": {
"test": "node ./cli.js test test/index.js --tz='Europe/Tallinn'",
"lint": "npx jshint --config=.github/jshint.json *.js lib/*.js"
"test": "node ./cli.js t"
},
"dependencies": {
"@litejs/dom": "24.12.0"
Expand Down
2 changes: 1 addition & 1 deletion test/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("opts", function() {
],
[
["-abcd","-e", "f", "-g"],
{a_a:false, b_b:false, c_c:"",e_e:"",g_g:"h"}, {a:true, b:true, c:"d", e:"f",g:"", _valid: ["--a", "--b","--c=d", "--e=f","--g="], _unknown:[],_:[]}
{ax_a:false, bx_b:false, cx_c:"",ex_e:"",g_g:"h"}, {ax:true, bx:true, cx:"d", ex:"f",g:"", _valid: ["--ax", "--bx","--cx=d", "--ex=f","--g="], _unknown:[],_:[]}
],
[ [], {print:false, to:"a", x:true}, {print:false, to:"a", x:true, _valid: [], _unknown:[],_:[]} ]
], function(argv, defaults, expected, assert) {
Expand Down
6 changes: 3 additions & 3 deletions test/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ describe("tools", function() {
// Assert options
assert.equal(cli.ls(".github/*", { dir: false }).join(" "), ".github/jshint.json .github/litejs.json")
assert.equal(cli.ls("*", { cwd: ".github", dir: false }).join(" "), "jshint.json litejs.json")
assert.own(cli.ls("*", { cwd: ".github", stat: true }), [
{ size: 225, name: "jshint.json" },
{ size: 21, name: "litejs.json" },
assert.own(cli.ls("*", { cwd: ".github", stat: true }).map(s=>({size: s.size, name: s.name})), [
{ size: 232, name: "jshint.json" },
{ size: 247, name: "litejs.json" },
{ size: 4096, name: "workflows" }
])
assert.equal(cli.ls(".github/*", { file: false }).join(" "), ".github/workflows")
Expand Down

0 comments on commit 42a793d

Please sign in to comment.