Skip to content

Commit 716c3b9

Browse files
authored
Fix for JSON imports not working (#1752)
* Revert "Remove unneeded compatibility code" * Revert tests partially Keep changes for removing commented tests and prettier formatting
1 parent 12bfe70 commit 716c3b9

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

src/__tests__/mode.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ const CATEGORY = {
2222
types: /\bstorage.type\b/,
2323
forbidden: /\bvariable.language\b/,
2424
keywords: /\bkeyword\b/,
25+
consts: /\bbuiltinconsts\b/,
2526
number: /\bconstant.numeric\b/,
26-
bool: /\bconstant.language.boolean\b/,
27-
identifier: /\bidentifier\b/
27+
bool: /\bconstant.language.boolean\b/
2828
}
2929

3030
const setSession = (chapter: Chapter, variant: Variant, external: string, code: string): void => {
@@ -49,15 +49,15 @@ test('function token type error', () => {
4949
const token2 = session.getTokenAt(1, 3)
5050

5151
// at source 2, pair is function but set_tail is not
52-
expect(expectedBool(token1, CATEGORY.identifier)).toBe(true)
52+
expect(expectedBool(token1, CATEGORY.functions)).toBe(true)
5353
expect(expectedBool(token2, CATEGORY.functions)).toBe(false)
5454

5555
// at source 4, set_tail is function as well
5656
setSession(Chapter.SOURCE_4, defaultVariant, defaultExternal, code)
5757
const newToken1 = session.getTokenAt(0, 11)
5858
const newToken2 = session.getTokenAt(1, 3)
59-
expect(expectedBool(newToken1, CATEGORY.identifier)).toBe(true)
60-
expect(expectedBool(newToken2, CATEGORY.identifier)).toBe(true)
59+
expect(expectedBool(newToken1, CATEGORY.functions)).toBe(true)
60+
expect(expectedBool(newToken2, CATEGORY.functions)).toBe(true)
6161
})
6262

6363
test('constants are not correctly loaded', () => {
@@ -72,7 +72,7 @@ test('constants are not correctly loaded', () => {
7272
expect(expectedBool(token2, CATEGORY.number)).toBe(true)
7373

7474
const token3 = session.getTokenAt(2, 1)
75-
expect(expectedBool(token3, CATEGORY.identifier)).toBe(true)
75+
expect(expectedBool(token3, CATEGORY.consts)).toBe(true)
7676
})
7777

7878
test('operator syntax type error', () => {

src/editors/ace/docTooltip/index.ts

+50-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,55 @@
1-
import ext_lib from './External libraries.json'
2-
import source_1 from './source_1.json'
3-
import source_1_typed from './source_1_typed.json'
4-
import source_2 from './source_2.json'
5-
import source_2_typed from './source_2_typed.json'
6-
import source_3 from './source_3.json'
7-
import source_3_concurrent from './source_3_concurrent.json'
8-
import source_3_typed from './source_3_typed.json'
9-
import source_4 from './source_4.json'
10-
import source_4_explicit_control from './source_4_explicit-control.json'
11-
import source_4_typed from './source_4_typed.json'
1+
import * as ext_lib from './External libraries.json'
2+
import * as source_1 from './source_1.json'
3+
import * as source_1_typed from './source_1_typed.json'
4+
import * as source_2 from './source_2.json'
5+
import * as source_2_typed from './source_2_typed.json'
6+
import * as source_3 from './source_3.json'
7+
import * as source_3_concurrent from './source_3_concurrent.json'
8+
import * as source_3_typed from './source_3_typed.json'
9+
import * as source_4 from './source_4.json'
10+
import * as source_4_typed from './source_4_typed.json'
11+
import * as source_4_explicit_control from './source_4_explicit-control.json'
12+
13+
// (18 March 2022)
14+
// Problem to be fixed in the future:
15+
//
16+
// There seems to be an inconsistency between how jest and how typescript
17+
// behaves when encountering imports of the form `import * as x from 'x.json'`
18+
// jest will set x = jsonobject,
19+
// but typescript will instead set x = { default: jsonobject }
20+
//
21+
// This means that under typescript, we want `import x from 'x.json'`,
22+
// while under jest, we want `import * as x from 'x.json'`
23+
//
24+
// This problem was hidden when transpiling to CommonJS modules before, which
25+
// behaves similarly to jest. But now that we are transpiling to es6,
26+
// typescript projects that depend on js-slang may now be exposed to this
27+
// inconsistency.
28+
//
29+
// For now, we use brute force until the landscape changes or someone thinks of
30+
// a proper solution.
31+
function resolveImportInconsistency(json: any) {
32+
// `json` doesn't inherit from `Object`?
33+
// Can't use hasOwnProperty for some reason.
34+
if ('default' in json) {
35+
return json.default
36+
} else {
37+
return json
38+
}
39+
}
1240

1341
export const SourceDocumentation = {
1442
builtins: {
15-
'1': source_1,
16-
'1_typed': source_1_typed,
17-
'2': source_2,
18-
'2_typed': source_2_typed,
19-
'3': source_3,
20-
'3_concurrent': source_3_concurrent,
21-
'3_typed': source_3_typed,
22-
'4': source_4,
23-
'4_typed': source_4_typed,
24-
'4_explicit-control': source_4_explicit_control
43+
'1': resolveImportInconsistency(source_1),
44+
'1_typed': resolveImportInconsistency(source_1_typed),
45+
'2': resolveImportInconsistency(source_2),
46+
'2_typed': resolveImportInconsistency(source_2_typed),
47+
'3': resolveImportInconsistency(source_3),
48+
'3_concurrent': resolveImportInconsistency(source_3_concurrent),
49+
'3_typed': resolveImportInconsistency(source_3_typed),
50+
'4': resolveImportInconsistency(source_4),
51+
'4_typed': resolveImportInconsistency(source_4_typed),
52+
'4_explicit-control': resolveImportInconsistency(source_4_explicit_control)
2553
},
2654
ext_lib
27-
} as const
55+
}

0 commit comments

Comments
 (0)