Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Report an error when a function is declared in the `$functions` map in a JS file but has no corresponding `extern fn` declaration in TypeSpec. Previously this would silently have no effect.
36 changes: 36 additions & 0 deletions packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4848,6 +4848,7 @@ export function createChecker(program: Program, resolver: NameResolver): Checker
checkSourceFile(file);
}

checkJsImplementations();
internalDecoratorValidation();
assertNoPendingResolutions();
runPostValidators(postCheckValidators);
Expand Down Expand Up @@ -4924,6 +4925,41 @@ export function createChecker(program: Program, resolver: NameResolver): Checker
validateInheritanceDiscriminatedUnions(program);
}

/**
* Validate that every function implementation in a JS `$functions` map has a corresponding
* `extern fn` declaration in TypeSpec. Without a declaration the function implementation is
* silently ignored. Report an error to help the user diagnose the problem.
*/
function checkJsImplementations() {
for (const jsFile of program.jsSourceFiles.values()) {
checkJsSymbolTableForUnboundFunctions(jsFile.symbol.exports!);
}
}

function checkJsSymbolTableForUnboundFunctions(table: SymbolTable) {
for (const sym of table.values()) {
if (sym.flags & SymbolFlags.Namespace) {
if (sym.exports) {
checkJsSymbolTableForUnboundFunctions(sym.exports);
}
} else if (sym.flags & SymbolFlags.Function && sym.flags & SymbolFlags.Implementation) {
const mergedSym = getMergedSymbol(sym);
const hasTypeSpecDeclaration = mergedSym.declarations.some(
(decl) => decl.kind === SyntaxKind.FunctionDeclarationStatement,
);
if (!hasTypeSpecDeclaration) {
reportCheckerDiagnostic(
createDiagnostic({
code: "implementation-without-extern",
format: { name: sym.name },
target: sym.declarations[0],
}),
);
}
}
}
}

function checkSourceFile(file: TypeSpecScriptNode) {
for (const statement of file.statements) {
checkNode(CheckContext.DEFAULT, statement, undefined);
Expand Down
9 changes: 9 additions & 0 deletions packages/compiler/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,15 @@ const diagnostics = {
default: "Extern declaration must have an implementation in JS file.",
},
},
"implementation-without-extern": {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot lets add a documentation url and a markdown document for it like for triple-quote error

severity: "error",
description:
"Report when a function is registered in $functions in a JS file but has no corresponding `extern fn` declaration in TypeSpec.",
url: "https://typespec.io/docs/standard-library/diags/implementation-without-extern",
messages: {
default: paramMessage`Function "${"name"}" is declared in \`$functions\` but does not have a corresponding \`extern fn\` declaration in TypeSpec. Add an \`extern fn\` declaration.`,
},
},
"overload-same-parent": {
severity: "error",
messages: {
Expand Down
Loading