Skip to content

Commit 272f206

Browse files
author
Orta Therox
authored
Merge pull request microsoft#1048 from microsoft/better-json-resolves
JSON in twoslash
2 parents cc7d024 + 80c6719 commit 272f206

12 files changed

Lines changed: 108 additions & 40 deletions

File tree

.github/workflows/CI-post.yml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
restore-keys: |
2828
${{ runner.os }}-yarn-
2929
30-
# Install, should be absically instant if cached
30+
# Install, should be basically be instant if cached
3131
- run: yarn install
3232
env:
3333
YARN_CHECKSUM_BEHAVIOR: ignore
@@ -67,17 +67,6 @@ jobs:
6767
app_artifact_location: "public" # Subfolder in built
6868
###### End of Repository/Build Configurations #####
6969

70-
# - if: github.event_name == 'pull_request' && github.event.action == 'closed'
71-
# runs-on: ubuntu-latest
72-
# name: Close Pull Request Job
73-
# steps:
74-
# - name: Close Pull Request
75-
# id: closepullrequest
76-
# uses: Azure/[email protected]
77-
# with:
78-
# azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_NICE_MEADOW_0C9756810 }}
79-
# action: "close"
80-
8170
- run: "yarn workspace typescriptlang-org create-lighthouse-json"
8271
env:
8372
PR_DEPLOY_URL_ROOT: ${{ steps.deploy.outputs.static_web_app_url }}

packages/gatsby-remark-shiki-twoslash/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"lint": "tsdx lint"
2020
},
2121
"dependencies": {
22-
"@typescript/twoslash": "1.1.0",
22+
"@typescript/twoslash": "1.1.1",
2323
"@typescript/vfs": "1.3.0",
2424
"shiki": "^0.1.6",
2525
"shiki-languages": "^0.1.6",

packages/shiki-twoslash/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"lint": "tsdx lint"
2121
},
2222
"dependencies": {
23-
"@typescript/twoslash": "1.1.0",
23+
"@typescript/twoslash": "1.1.1",
2424
"@typescript/vfs": "1.3.0",
2525
"shiki": "^0.1.6",
2626
"shiki-languages": "^0.1.6",

packages/ts-twoslasher/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.1
2+
3+
- Better handling of JSON files
4+
15
## 1.1.0
26

37
- Adds a JS file into the npm tarball for using with a vanilla script tag, which sets `global.twoslash` with the main twoslash function. You need to include a copy of tsvfs beforehand.

packages/ts-twoslasher/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@typescript/twoslash",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"license": "MIT",
55
"author": "TypeScript team",
66
"main": "dist/index.js",

packages/ts-twoslasher/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ export function twoslasher(code: string, extension: string, options: TwoSlashOpt
418418
const filetype = filename.split(".").pop() || ""
419419

420420
// Only run the LSP-y things on source files
421-
if (!sourceFiles.includes(filetype)) {
421+
const allowJSON = compilerOptions.resolveJsonModule && filetype === "json"
422+
if (!sourceFiles.includes(filetype) && !allowJSON) {
422423
continue
423424
}
424425

@@ -496,6 +497,9 @@ export function twoslasher(code: string, extension: string, options: TwoSlashOpt
496497
// Lets fs changes propagate back up to the fsMap
497498
if (handbookOptions.emit) {
498499
filenames.forEach(f => {
500+
const filetype = f.split(".").pop() || ""
501+
if (!sourceFiles.includes(filetype)) return
502+
499503
const output = ls.getEmitOutput(f)
500504
output.outputFiles.forEach(output => {
501505
system.writeFile(output.name, output.text)
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
// @errors: 2367
2+
// @resolveJsonModule
3+
// @module: commonjs
14
// @moduleResolution: node
2-
// @filename: package.json
3-
{ "name": "thing" }
5+
// @filename: settings.json
6+
{
7+
"repo": "TypeScript",
8+
"dry": false,
9+
"debug": false
10+
}
411
// @filename: index.ts
5-
// ---cut---
6-
const i = 123
12+
import settings from "./settings.json";
13+
14+
settings.debug === true;
15+
settings.dry === 2;
Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,66 @@
11
{
2-
"code": "const i = 123\n",
2+
"code": "// @filename: settings.json\n{\n \"repo\": \"TypeScript\",\n \"dry\": false,\n \"debug\": false\n}\n// @filename: index.ts\nimport settings from \"./settings.json\";\n\nsettings.debug === true;\nsettings.dry === 2;\n",
33
"extension": "ts",
44
"highlights": [],
55
"queries": [],
66
"staticQuickInfos": [
77
{
8-
"text": "const i: 123",
8+
"text": "import settings",
99
"docs": "",
10-
"start": 6,
11-
"length": 1,
12-
"line": 0,
13-
"character": 6,
14-
"targetString": "i"
10+
"start": 125,
11+
"length": 8,
12+
"line": 7,
13+
"character": 7,
14+
"targetString": "settings"
15+
},
16+
{
17+
"text": "import settings",
18+
"docs": "",
19+
"start": 159,
20+
"length": 8,
21+
"line": 9,
22+
"character": 0,
23+
"targetString": "settings"
24+
},
25+
{
26+
"text": "(property) \"debug\": boolean",
27+
"docs": "",
28+
"start": 168,
29+
"length": 5,
30+
"line": 9,
31+
"character": 9,
32+
"targetString": "debug"
33+
},
34+
{
35+
"text": "import settings",
36+
"docs": "",
37+
"start": 184,
38+
"length": 8,
39+
"line": 10,
40+
"character": 0,
41+
"targetString": "settings"
42+
},
43+
{
44+
"text": "(property) \"dry\": boolean",
45+
"docs": "",
46+
"start": 193,
47+
"length": 3,
48+
"line": 10,
49+
"character": 9,
50+
"targetString": "dry"
51+
}
52+
],
53+
"errors": [
54+
{
55+
"category": 1,
56+
"code": 2367,
57+
"length": 18,
58+
"start": 184,
59+
"line": 3,
60+
"character": 0,
61+
"renderedMessage": "This condition will always return 'false' since the types 'boolean' and 'number' have no overlap.",
62+
"id": "err-2367-66-18"
1563
}
1664
],
17-
"errors": [],
18-
"playgroundURL": "https://www.typescriptlang.org/play/#code/PTAEAEFsHsBMFcA2BTASsgztR8AuBLaAOwC5Qi5kAoECAM3xSIENJkyAHZgYwGtmA5sgB0AKyxEqAb1AAiFm1llZuABb4iA2aAC+NMOAZNW7UBtjIAHsNwZ9oALRPueJw6rdiGXGdABeUABGACYAZiogA"
65+
"playgroundURL": "https://www.typescriptlang.org/play/#code/PTAEAEFMCdoe2gZwFygEwGYBsB2AUCBNJInADYBukAUqQHYCycAJgK5mQFjgC2L7kVAGM4PPnQBWiLhD5sOAJRLlWAFwCWcOqjotOhcADN1HOgEMeg0IkiqNdAOaIAdFK14A3nlA-QAImIABzg-VD8AFQBPQMgAZSFodUDVPwAab18-ZmhI0NBDMzIbdN9-ZkgAI1YHPIKizgBfGSMTSHNLVHU6coAPZ1VpdR5g6FVrW3snfPgef2dgGzsup1d6PwBuPDxFyZdyqodQAF4T0FVoVkhNneW9nOPTtE2gA"
1966
}

packages/tsconfig-reference/copy/en/options/resolveJsonModule.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ generating a type for the `import` based on the static JSON shape.
88

99
TypeScript does not support resolving JSON files by default:
1010

11-
```ts
11+
```ts twoslash
12+
// @errors: 2732
1213
// @filename: settings.json
1314
{
1415
"repo": "TypeScript",
@@ -24,7 +25,11 @@ settings.dry === 2;
2425

2526
Enabling the option allows importing JSON, and validating the types in that JSON file.
2627

27-
```ts
28+
```ts twoslash
29+
// @errors: 2367
30+
// @resolveJsonModule
31+
// @module: commonjs
32+
// @moduleResolution: node
2833
// @filename: settings.json
2934
{
3035
"repo": "TypeScript",

packages/tsconfig-reference/copy/ja/options/resolveJsonModule.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ display: "Resolve JSON Module"
33
oneline: "Allow importing .json files"
44
---
55

6-
'.json'拡張子のファイルをモジュールとしてインポートできるようにします。Nodeのプロジェクトで一般的に利用されている手法です
7-
このオプションは、`import`時に静的なJSONの構造から型を生成します
6+
'.json'拡張子のファイルをモジュールとしてインポートできるようにします。Node のプロジェクトで一般的に利用されている手法です
7+
このオプションは、`import`時に静的な JSON の構造から型を生成します
88

9-
デフォルトでは、TypeScriptはJSONファイルの解決をサポートしていません:
9+
デフォルトでは、TypeScript は JSON ファイルの解決をサポートしていません:
1010

11-
```ts
11+
```ts twoslash
12+
// @errors: 2732
1213
// @filename: settings.json
1314
{
1415
"repo": "TypeScript",
@@ -22,9 +23,13 @@ settings.debug === true;
2223
settings.dry === 2;
2324
```
2425

25-
このオプションを有効にするとJSONのインポートが可能となり、JSONファイルの型を検査できるようになります
26+
このオプションを有効にすると JSON のインポートが可能となり、JSON ファイルの型を検査できるようになります
2627

27-
```ts
28+
```ts twoslash
29+
// @errors: 2367
30+
// @resolveJsonModule
31+
// @module: commonjs
32+
// @moduleResolution: node
2833
// @filename: settings.json
2934
{
3035
"repo": "TypeScript",

0 commit comments

Comments
 (0)