Skip to content

Commit 5e90f97

Browse files
author
Orta Therox
authored
Twoslash perf improvements (microsoft#1115)
* Improve the logging system * Better naming * Update twoslash perf * Bug fixes * Adds the PR json to the artifact * Post-CI improvements
1 parent 8042cb0 commit 5e90f97

5 files changed

Lines changed: 41 additions & 11 deletions

File tree

.github/workflows/CI.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ jobs:
5151
- name: "Validates that TypeScript plugins work"
5252
run: npm init typescript-playground-plugin playground-my-plugin
5353

54+
- name: "Copy PR JSON"
55+
run: cp $GITHUB_EVENT_PATH ${{ github.workspace }}/packages/typescriptlang-org/public/pr.json
56+
5457
- uses: actions/upload-artifact@v2
5558
with:
5659
name: built-site

dangerfile.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { readFileSync } from "fs"
1212

1313
export default () => {
1414
// JSON reference: https://github.com/haya14busa/github-actions-playground/runs/987846369
15-
const contextText = readFileSync(process.env.GITHUB_EVENT_PATH, "utf8")
15+
const contextText = readFileSync("built/public/pr.json", "utf8")
16+
console.log(contextText)
1617
const context = JSON.parse(contextText)
1718

1819
const repo = { owner: context.event.repository.owner.login, repo: context.event.repository.name }

packages/sandbox/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,14 @@ export const createTypeScriptSandbox = (
200200
// Don't update a compiler setting if it's the same
201201
// as the current setting
202202
newKeys.forEach(key => {
203-
if (compilerOptions[key] === opts[key]) delete opts[key]
203+
if (compilerOptions[key] == opts[key]) delete opts[key]
204204
})
205205

206206
if (!Object.keys(opts).length) return
207207

208208
config.logger.log("[Compiler] Updating compiler options: ", opts)
209-
compilerOptions = { ...opts, ...compilerOptions }
209+
210+
compilerOptions = { ...compilerOptions, ...opts }
210211
defaults.setCompilerOptions(compilerOptions)
211212
didUpdateCompilerSettings(compilerOptions)
212213
}

packages/sandbox/src/twoslashSupport.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type CompilerOptions = import("typescript").CompilerOptions
1212
*/
1313

1414
export const extractTwoSlashComplierOptions = (ts: TS) => {
15-
const optMap = new Map<string, any>()
15+
let optMap = new Map<string, any>()
1616

1717
// @ts-ignore - optionDeclarations is not public API
1818
for (const opt of ts.optionDeclarations) {
@@ -26,10 +26,15 @@ export const extractTwoSlashComplierOptions = (ts: TS) => {
2626
codeLines.forEach(line => {
2727
let match
2828
if ((match = booleanConfigRegexp.exec(line))) {
29-
options[match[1]] = true
30-
setOption(match[1], "true", options, optMap)
29+
if (optMap.has(match[1].toLowerCase())) {
30+
options[match[1]] = true
31+
setOption(match[1], "true", options, optMap)
32+
}
3133
} else if ((match = valuedConfigRegexp.exec(line))) {
32-
setOption(match[1], match[2], options, optMap)
34+
console.log(match)
35+
if (optMap.has(match[1].toLowerCase())) {
36+
setOption(match[1], match[2], options, optMap)
37+
}
3338
}
3439
})
3540
return options

packages/sandbox/test/twoslashSupport.test.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { extractTwoSlashComplierOptions } from '../src/twoslashSupport'
2-
import typescript from 'typescript'
1+
import { extractTwoSlashComplierOptions } from "../src/twoslashSupport"
2+
import typescript from "typescript"
33

44
const sandboxMock: any = (code: string) => ({
55
getText: () => code,
66
ts: typescript,
77
})
88

9-
describe('gets twoslash compiler error messages', () => {
10-
it('gets the right vars', () => {
9+
describe("gets twoslash compiler error messages", () => {
10+
it.only("gets the right vars", () => {
1111
const sandbox = sandboxMock(`
1212
// @noImplicitAny: false
1313
// @target: ES2015
@@ -27,4 +27,24 @@ fn(42)
2727
}
2828
`)
2929
})
30+
31+
it("ignores unknown vars", () => {
32+
const sandbox = sandboxMock(`
33+
// @noImplicny: false
34+
// @target: ES2015
35+
36+
// This will not throw because of the noImplicitAny
37+
function fn(s) {
38+
console.log(s.subtr(3))
39+
}
40+
41+
fn(42)
42+
`)
43+
const compilerOptions = extractTwoSlashComplierOptions(sandbox.ts)(sandbox.getText())
44+
expect(compilerOptions).toMatchInlineSnapshot(`
45+
Object {
46+
"target": 2,
47+
}
48+
`)
49+
})
3050
})

0 commit comments

Comments
 (0)