Skip to content

Commit

Permalink
fix polluting globals
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Apr 29, 2024
1 parent 6695159 commit 885a6cc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-fireants-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'mdxts': patch
---

Fixes polluting `CodeBlock` globals by always adding a `export { }` declaration to the AST and only removing it from the rendered tokens.
42 changes: 18 additions & 24 deletions packages/mdxts/src/components/CodeBlock/get-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,33 @@ export async function getTokens(
allowedErrorCodes.length === 0 && allowErrors
? []
: sourceFile
? getDiagnostics(sourceFile).filter(
(diagnostic) => !allowedErrorCodes.includes(diagnostic.getCode())
)
? sourceFile
.getPreEmitDiagnostics()
.filter(
(diagnostic) => !allowedErrorCodes.includes(diagnostic.getCode())
)
: []
const theme = getThemeColors()
const finalLanguage = getLanguage(language)
const { tokens } = highlighter.codeToTokens(
let { tokens } = highlighter.codeToTokens(
sourceFile ? sourceFile.getFullText() : value,
{
theme: 'mdxts',
lang: finalLanguage as any,
}
)
// If tokens contain an "export { }" statement, remove it
const exportStatementIndex = tokens.findIndex((line) =>
line
.map((token) => token.content)
.join('')
.includes('export { }')
)
if (exportStatementIndex > -1) {
// trim the export statement and the following line break
tokens = tokens.slice(0, exportStatementIndex - 1)
}

const importSpecifiers =
sourceFile && !jsxOnly
? sourceFile
Expand Down Expand Up @@ -323,26 +337,6 @@ function getQuickInfo(
}
}

/** Get the diagnostics for a source file, coerced into a module if necessary. */
function getDiagnostics(sourceFile: SourceFile) {
// if no imports/exports are found, add an empty export to coerce the file into a module and not pollute the global scope
const hasImports = sourceFile.getImportDeclarations().length > 0
const hasExports = sourceFile.getExportDeclarations().length > 0

if (!hasImports && !hasExports) {
sourceFile.addExportDeclaration({})
}

const diagnostics = sourceFile.getPreEmitDiagnostics()

// remove the empty export
if (!hasImports && !hasExports) {
sourceFile.getExportDeclarations().at(0)!.remove()
}

return diagnostics
}

const FontStyle = {
Italic: 1,
Bold: 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,26 @@ export async function parseSourceTextMetadata({
// attempt to fix the removed imports and any other missing imports
sourceFile.fixMissingImports()

const importDeclarations = sourceFile.getImportDeclarations()

if (shouldEmitDiagnostics) {
// remap relative module specifiers to package imports if possible
// e.g. `import { getTheme } from '../../mdxts/src/components'` -> `import { getTheme } from 'mdxts/components'`
sourceFile.getImportDeclarations().forEach((importDeclaration) => {
importDeclarations.forEach((importDeclaration) => {
if (importDeclaration.isModuleSpecifierRelative()) {
const importSpecifier = getPathRelativeToPackage(importDeclaration)
importDeclaration.setModuleSpecifier(importSpecifier)
}
})
}

// If no imports or exports add an empty export declaration to coerce TypeScript to treat the file as a module
const hasImports = importDeclarations.length > 0
const hasExports = sourceFile.getExportDeclarations().length > 0

if (!hasImports && !hasExports) {
sourceFile.addExportDeclaration({})
}
} catch (error) {
if (error instanceof Error) {
const workingDirectory = process.cwd()
Expand Down

0 comments on commit 885a6cc

Please sign in to comment.