-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validate/webidl): add Web IDL validator (#104)
- Loading branch information
Showing
8 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { env, exit, install, yesOrNo } from "./utils.js"; | ||
import { BuildResult } from "./build.js"; | ||
type Input = Pick<BuildResult, "dest" | "file">; | ||
|
||
if (module === require.main) { | ||
if (yesOrNo(env("INPUTS_VALIDATE_WEBIDL")) === false) { | ||
exit("Skipped", 0); | ||
} | ||
|
||
const input: Input = JSON.parse(env("OUTPUTS_BUILD")); | ||
main(input).catch(err => exit(err.message || "Failed", err.code)); | ||
} | ||
|
||
export default async function main({ dest, file }: Input) { | ||
console.log(`Validating Web IDL defined in ${file}...`); | ||
await install("reffy"); | ||
const { crawlList } = require("reffy/src/cli/crawl-specs"); | ||
|
||
const fileurl = new URL(file, `file://${dest}/`).href; | ||
const results = await crawlList( | ||
[{ url: fileurl, nightly: { url: fileurl } }], | ||
{ modules: ["idl"] }, | ||
); | ||
|
||
const idl = results[0]?.idl?.idl; | ||
if (!idl) { | ||
exit("No Web IDL found in spec, skipped validation", 0); | ||
} | ||
|
||
await install("webidl2"); | ||
const { parse, validate } = require("webidl2"); | ||
let errors: { message: string }[] = []; | ||
try { | ||
const tree = parse(idl); | ||
errors = validate(tree); | ||
} catch (error) { | ||
errors = [error]; | ||
} | ||
if (!errors.length) { | ||
exit("✅ Looks good! No Web IDL validation errors!", 0); | ||
} else { | ||
console.group("Invalid Web IDL detected:"); | ||
for (const error of errors) { | ||
console.log(error.message); | ||
console.log(""); | ||
} | ||
console.groupEnd(); | ||
exit("❌ Invalid Web IDL detected... please fix the issues above."); | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import main from "../src/validate-webidl.js"; | ||
import { Outputs } from "./index.test.js"; | ||
|
||
export default async function validateWebIdl(outputs: Outputs) { | ||
const { webidl: shouldValidate = false } = outputs?.prepare?.validate || {}; | ||
if (shouldValidate === false) { | ||
return; | ||
} | ||
|
||
const { dest = process.cwd() + ".common", file = "index.html" } = | ||
outputs?.build?.w3c || {}; | ||
|
||
return await main({ dest, file }); | ||
} |