Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Source modules/functions to Full TypeScript chapter #1400

Open
zhaojj2209 opened this issue Apr 4, 2023 · 0 comments
Open
Labels
Enhancement New feature or request Proposal Proposing a feature, please discuss

Comments

@zhaojj2209
Copy link
Contributor

Currently, the Full TypeScript chapter uses the below workaround for pre-declared functions and prelude functions:

// Add builtins to code
// Each declaration is replaced with a single constant declaration with type `any`
// to reduce evaluation time
for (const builtin of context.nativeStorage.builtins) {
code += `const ${builtin[0]}: any = 1\n`
}
// Add prelude functions to code
// Each declaration is replaced with a single constant declaration with type `any`
// to reduce evaluation time
if (context.prelude) {
const preludeFns = context.prelude.split('\nfunction ').slice(1)
preludeFns.forEach(fnString => {
const fnName = fnString.split('(')[0]
// Functions in prelude that start with $ are not added
if (fnName.startsWith('$')) {
return
}
code += `const ${fnName}: any = 1\n`
})
}

Any errors related to modules are also ignored:

diagnostics.forEach(diagnostic => {
const message = diagnostic.messageText.toString()
// Ignore errors regarding imports
// as TS does not have information about Source modules
if (message === IMPORT_TOP_LEVEL_ERROR || message.startsWith(START_OF_MODULE_ERROR)) {
return
}
const lineNumRegExpArr = lineNumRegex.exec(formattedString.split(message)[1])
const lineNum = (lineNumRegExpArr === null ? 0 : parseInt(lineNumRegExpArr[0])) - lineOffset
// Ignore any errors that occur in builtins/prelude (line number <= 0)
if (lineNum <= 0) {
return
}
const position = { line: lineNum, column: 0, offset: 0 }
context.errors.push(new FatalSyntaxError(positionToSourceLocation(position), message))
})

This means that modules and pre-declared functions are not properly typed in the Full TypeScript chapter, and it would be good to add support for them down the line.

To add support for pre-declared functions (without exposing the implementation of the functions to the user), one possible way is to create a lightweight file containing the types of each function, then replace the any type in the constant declaration with the correct type for the function.

In terms of module support, we can perhaps wait until #1399 is solved, and see if the same approach can be used here.

@zhaojj2209 zhaojj2209 added Enhancement New feature or request Proposal Proposing a feature, please discuss labels Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request Proposal Proposing a feature, please discuss
Projects
Development

No branches or pull requests

1 participant