Skip to content

Commit 06d7f04

Browse files
ZweZeyaRichDom2185martin-henz
authored
1401 add support for local module importexport to source typed (#1686)
* add typechecking for export statement in typeErrorChecker * merge * update estree and TypedES * Fix broken submodule changes * Fix format --------- Co-authored-by: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Co-authored-by: Martin Henz <henz@comp.nus.edu.sg>
1 parent 8716791 commit 06d7f04

File tree

9 files changed

+41
-14
lines changed

9 files changed

+41
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@commander-js/extra-typings": "^12.0.1",
3636
"@joeychenofficial/alt-ergo-modified": "^2.4.0",
3737
"@ts-morph/bootstrap": "^0.18.0",
38-
"@types/estree": "0.0.52",
38+
"@types/estree": "^1.0.5",
3939
"acorn": "^8.8.2",
4040
"acorn-class-fields": "^1.0.0",
4141
"acorn-loose": "^8.0.0",

src/modules/preprocessor/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type es from 'estree'
2+
// import * as TypedES from '../../typeChecker/tsESTree'
23

34
import type { Context, IOptions } from '../..'
45
import type { RecursivePartial } from '../../types'

src/modules/preprocessor/transformers/removeExports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function removeExports(program: es.Program): void {
2121
// it with the declaration node in its parent node's body.
2222
return node.declaration.type === 'FunctionDeclaration' ||
2323
node.declaration.type === 'ClassDeclaration'
24-
? node.declaration
24+
? (node.declaration as es.FunctionDeclaration)
2525
: undefined
2626
case 'ExportNamedDeclaration':
2727
// If the ExportNamedDeclaration node contains a declaration, replace

src/modules/preprocessor/transformers/transformProgramToFunctionDeclaration.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ const getDefaultExportExpression = (
142142
nodes: es.ModuleDeclaration[],
143143
exportedNameToIdentifierMap: Partial<Record<string, es.Identifier>>
144144
): es.Expression | null => {
145-
let defaultExport: es.Expression | null = null
145+
let defaultExport:
146+
| es.MaybeNamedFunctionDeclaration
147+
| es.MaybeNamedClassDeclaration
148+
| es.Expression
149+
| null = null
146150

147151
// Handle default exports which are parsed as ExportNamedDeclaration AST nodes.
148152
// 'export { name as default };' is equivalent to 'export default name;' but
@@ -164,7 +168,6 @@ const getDefaultExportExpression = (
164168
// This should never occur because multiple default exports should have
165169
// been caught by the Acorn parser when parsing into an AST.
166170
assert(defaultExport === null, 'Encountered multiple default exports!')
167-
168171
if (isDeclaration(node.declaration)) {
169172
const identifier = getIdentifier(node.declaration)
170173
if (identifier === null) {

src/typeChecker/tsESTree.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface BaseNodeWithoutComments {
66
// The type property should be a string literal. For example, Identifier
77
// has: `type: "Identifier"`
88
type: string
9-
loc?: es.SourceLocation | null | undefined
9+
loc?: SourceLocation | null | undefined
1010
range?: [number, number] | undefined
1111
}
1212

@@ -49,6 +49,12 @@ export interface Comment extends BaseNodeWithoutComments {
4949
value: string
5050
}
5151

52+
export interface SourceLocation {
53+
source?: string | null | undefined
54+
start: Position
55+
end: Position
56+
}
57+
5258
export interface Position {
5359
/** >= 1 */
5460
line: number
@@ -217,7 +223,7 @@ export type Declaration = FunctionDeclaration | VariableDeclaration | ClassDecla
217223

218224
type BaseDeclaration = BaseStatement
219225

220-
export interface FunctionDeclaration extends BaseFunction, BaseDeclaration {
226+
export interface MaybeNamedFunctionDeclaration extends BaseFunction, BaseDeclaration {
221227
type: 'FunctionDeclaration'
222228
/** It is null when a function declaration is a part of the `export default function` statement */
223229
id: Identifier | null
@@ -226,6 +232,10 @@ export interface FunctionDeclaration extends BaseFunction, BaseDeclaration {
226232
returnType?: TSTypeAnnotation
227233
}
228234

235+
export interface FunctionDeclaration extends MaybeNamedFunctionDeclaration {
236+
id: Identifier
237+
}
238+
229239
export interface VariableDeclaration extends BaseDeclaration {
230240
type: 'VariableDeclaration'
231241
declarations: Array<VariableDeclarator>
@@ -486,6 +496,9 @@ export type AssignmentOperator =
486496
| '|='
487497
| '^='
488498
| '&='
499+
| '||='
500+
| '&&='
501+
| '??='
489502

490503
export type UpdateOperator = '++' | '--'
491504

@@ -588,12 +601,16 @@ export interface MethodDefinition extends BaseNode {
588601
static: boolean
589602
}
590603

591-
export interface ClassDeclaration extends BaseClass, BaseDeclaration {
604+
export interface MaybeNamedClassDeclaration extends BaseClass, BaseDeclaration {
592605
type: 'ClassDeclaration'
593606
/** It is null when a class declaration is a part of the `export default class` statement */
594607
id: Identifier | null
595608
}
596609

610+
export interface ClassDeclaration extends MaybeNamedClassDeclaration {
611+
id: Identifier
612+
}
613+
597614
export interface ClassExpression extends BaseClass, BaseExpression {
598615
type: 'ClassExpression'
599616
id?: Identifier | null | undefined
@@ -659,7 +676,7 @@ export interface ExportSpecifier extends BaseModuleSpecifier {
659676

660677
export interface ExportDefaultDeclaration extends BaseModuleDeclaration {
661678
type: 'ExportDefaultDeclaration'
662-
declaration: Declaration | Expression
679+
declaration: MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration | Expression
663680
}
664681

665682
export interface ExportAllDeclaration extends BaseModuleDeclaration {

src/typeChecker/typeErrorChecker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ function typeCheckAndReturnType(node: tsEs.Node): Type {
534534
return typeToCastTo
535535
case 'TSInterfaceDeclaration':
536536
throw new TypecheckError(node, 'Interface declarations are not allowed')
537+
case 'ExportNamedDeclaration':
538+
return typeCheckAndReturnType(node.declaration!)
537539
default:
538540
throw new TypecheckError(node, 'Unknown node type')
539541
}

src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,11 @@ export interface StatementSequence extends es.BaseStatement {
320320
/**
321321
* js-slang's custom Node type - this should be used wherever es.Node is used.
322322
*/
323-
export type Node = es.Node | StatementSequence
323+
export type Node =
324+
| es.Node
325+
| StatementSequence
326+
| es.MaybeNamedClassDeclaration
327+
| es.MaybeNamedFunctionDeclaration
324328

325329
/*
326330
Although the ESTree specifications supposedly provide a Directive interface, the index file does not seem to export it.

src/utils/ast/astCreator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ export const functionDeclarationExpression = (
340340
})
341341

342342
export const functionDeclaration = (
343-
id: es.Identifier | null,
343+
id: es.Identifier,
344344
params: es.Pattern[],
345345
body: es.BlockStatement,
346346
loc?: es.SourceLocation | null

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,10 +1459,10 @@
14591459
dependencies:
14601460
"@babel/types" "^7.20.7"
14611461

1462-
"@types/estree@0.0.52":
1463-
version "0.0.52"
1464-
resolved "https://registry.npmjs.org/@types/estree/-/estree-0.0.52.tgz"
1465-
integrity sha512-BZWrtCU0bMVAIliIV+HJO1f1PR41M7NKjfxrFJwwhKI1KwhwOxYw1SXg9ao+CIMt774nFuGiG6eU+udtbEI9oQ==
1462+
"@types/estree@^1.0.5":
1463+
version "1.0.5"
1464+
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.5.tgz#a6ce3e556e00fd9895dd872dd172ad0d4bd687f4"
1465+
integrity sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==
14661466

14671467
"@types/graceful-fs@^4.1.3":
14681468
version "4.1.9"

0 commit comments

Comments
 (0)