diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml index 3751196f..4ce68bd8 100644 --- a/.github/workflows/sync.yml +++ b/.github/workflows/sync.yml @@ -20,7 +20,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.TS_BOT_TOKEN }} # Update search links from the origin repo before pushing to the upstream - - run: git clone --depth 1 https://github.com/microsoft/TypeScript.git + - run: git clone --depth 1 https://github.com/microsoft/TypeScript.git TypeScript - run: npm i - run: node scripts/convertRelativeLinksToHardcoded.js "**/*.md" --write - run: rm -rf TypeScript diff --git a/learn/systems/systems-fourslash.md b/learn/systems/systems-fourslash.md index 081a4337..35afa8ce 100644 --- a/learn/systems/systems-fourslash.md +++ b/learn/systems/systems-fourslash.md @@ -15,7 +15,7 @@ Fourslash automatically generates mocha tests based on files you put inside [`/t for this lives in [`/src/testRunner/fourslashRunner.ts`][1]. This class is instantiated in [`/src/testRunner/runner.ts`][2]. -Fom here the main work all lives in [`/src/harness/foudslash.ts`][3] where we'll be spending the rest of this +Fom here the main work all lives in [`/src/harness/fourslash.ts`][3] where we'll be spending the rest of this section. The initial entry point is [`runFourSlashTest`][4] but the work is in [`runFourSlashTestContent`][5]. This function first creates a virtual fs, uses [`parseTestData`][6] to fill up the virtual fs. `parseTestData`: @@ -56,8 +56,8 @@ moment;` [0]: https://github.com/microsoft/TypeScript/blob/master/src/testRunner/fourslashRunner.ts [1]: https://github.com/microsoft/TypeScript/blob/master/tests/cases/fourslash [2]: https://github.com/microsoft/TypeScript/blob/master/src/testRunner/runner.ts -[3]: https://github.com/microsoft/TypeScript/blob/master/src/harness/fourslash.ts -[4]: -[5]: -[5]: +[3]: https://github.com/microsoft/TypeScript/blob/master/src/harness/fourslashImpl.ts +[4]: +[5]: +[5]: diff --git a/scripts/convertRelativeLinksToHardcoded.js b/scripts/convertRelativeLinksToHardcoded.js index 16e5d8f9..fc0816d4 100644 --- a/scripts/convertRelativeLinksToHardcoded.js +++ b/scripts/convertRelativeLinksToHardcoded.js @@ -12,53 +12,64 @@ // Write: // node scripts/convertRelativeLinksToHardcoded.js scripts/fixtures/input.md --write -const glob = require("glob") -const {readFileSync, writeFileSync, existsSync} = require("fs") -const {join} = require("path") +const glob = require("glob"); +const { readFileSync, writeFileSync, existsSync } = require("fs"); +const { join } = require("path"); const { execSync } = require("child_process"); const escapeRegex = require("escape-regex-string"); -if (!process.argv[2]) throw new Error("Did not include a glob for markdown files to change") +if (!process.argv[2]) throw new Error("Did not include a glob for markdown files to change"); // This can be anything -const write = process.argv[3] !== undefined +const write = process.argv[3] !== undefined; -const possibleTSRepo = ["../typescript-compiler", "../TypeScript", "TypeScript"] -let repoPath = possibleTSRepo.find(existsSync) +const possibleTSRepo = ["../typescript-compiler", "../TypeScript", "TypeScript"]; +let repoPath = possibleTSRepo.find(f => existsSync(join(f, "package.json"))); if (!repoPath) throw new Error("Could not find a TypeScript repo"); const repoHead = execSync(`git rev-parse HEAD | cut -c 1-8`, { cwd: repoPath, encoding: "utf8" }); -if (!repoHead) throw new Error("Could not get the git info from the sibling TypeScript repo") +if (!repoHead) throw new Error("Could not get the git info from the sibling TypeScript repo"); -const files = glob.sync(process.argv[2]) -if (!files.length) throw new Error("Did not get any files with that glob") +const files = glob.sync(process.argv[2]); +if (!files.length) throw new Error("Did not get any files with that glob"); + +let failed = []; files.forEach(file => { - let content = readFileSync(file, "utf8") + if (file === "README.md") return; + + let content = readFileSync(file, "utf8"); // https://regex101.com/r/w1dEG1/1 - const regex = new RegExp(/\[.*]: <(.*) - (.*)>/g) - - let result - while (result = regex.exec(content)) { - const file = result[1]; + const regex = new RegExp(/\[.*]: <(.*) - (.*)>/g); + + let result; + while ((result = regex.exec(content))) { + const fileRef = result[1]; const searchTerm = result[2]; - const original = `: <${file} - ${searchTerm}>` - const originalFile = readFileSync(join(repoPath, file), "utf8") - if (!originalFile) throw new Error(`Could not find a file at '${join(repoPath, file)}'`) - - const line = getLineNo(originalFile, new RegExp(escapeRegex(searchTerm))) - const lineRef = line && line[0] && line[0].number ? `#L${line[0].number}`: "" - const replacement = `: https://github.com/microsoft/TypeScript/blob/${repoHead.trim()}/${file}${lineRef}` - content = content.replace(original, replacement) + const original = `: <${fileRef} - ${searchTerm}>`; + try { + const originalFile = readFileSync(join(repoPath, fileRef), "utf8"); + const line = getLineNo(originalFile, new RegExp(escapeRegex(searchTerm))); + const lineRef = line && line[0] && line[0].number ? `#L${line[0].number}` : ""; + const replacement = `: https://github.com/microsoft/TypeScript/blob/${repoHead.trim()}/${fileRef}${lineRef}`; + content = content.replace(original, replacement); + } catch (e) { + failed.push([file, fileRef]); + } } if (write) { - writeFileSync(file, content) + writeFileSync(file, content); } else { - console.log(content) + console.log(content); } }); +if (failed.length) { + console.error("Could not find:"); + console.error(failed); + process.exit(1); +} /*! * line-number @@ -67,13 +78,16 @@ files.forEach(file => { * Licensed under the MIT License */ function getLineNo(str, re) { - return str.split(/\r?\n/).map(function (line, i) { - if (re.test(line)) { - return { - line: line, - number: i + 1, - match: line.match(re)[0] - }; - } - }).filter(Boolean); -}; + return str + .split(/\r?\n/) + .map(function(line, i) { + if (re.test(line)) { + return { + line: line, + number: i + 1, + match: line.match(re)[0] + }; + } + }) + .filter(Boolean); +}