-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide
removeTwoslashNotations
fallback function
- Loading branch information
Showing
9 changed files
with
219 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import fs from 'node:fs/promises' | ||
import ts from 'typescript' | ||
import type { CompilerOptionDeclaration } from '../src/types/options' | ||
import { defaultHandbookOptions } from '../src/defaults' | ||
|
||
async function generateFlagKeys() { | ||
const tsOptionDeclarations = (ts as any).optionDeclarations as CompilerOptionDeclaration[] | ||
|
||
const keys = [ | ||
...tsOptionDeclarations.map(i => i.name), | ||
...Object.keys(defaultHandbookOptions), | ||
].sort() | ||
|
||
await fs.writeFile('src/flag-keys.ts', `// Generated by scripts/flag-keys.ts\nexport const flagKeys = ${JSON.stringify(keys, null, 2)}`, 'utf-8') | ||
} | ||
|
||
generateFlagKeys() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { removeCodeRanges } from 'twoslash-protocol' | ||
import { reAnnonateMarkers, reConfigBoolean, reConfigValue } from './regexp' | ||
import type { TwoslashReturnMeta } from './types' | ||
import { findCutNotations } from './utils' | ||
import { flagKeys } from './flag-keys' | ||
|
||
/** | ||
* A fallback function to strip out twoslash annotations from a string and does nothing else. | ||
* | ||
* This function does not returns the meta information about the removals. | ||
* It's designed to be used as a fallback when Twoslash fails. | ||
*/ | ||
export function removeTwoslashNotations(code: string, customTags?: string[]): string { | ||
const meta: Pick<TwoslashReturnMeta, 'removals'> = { | ||
removals: [], | ||
} | ||
const tags = [ | ||
...customTags ?? [], | ||
...flagKeys, | ||
] | ||
|
||
Array.from(code.matchAll(reConfigBoolean)).forEach((match) => { | ||
if (!tags.includes(match[1])) | ||
return | ||
meta.removals.push([match.index!, match.index! + match[0].length + 1]) | ||
}) | ||
Array.from(code.matchAll(reConfigValue)).forEach((match) => { | ||
if (!tags.includes(match[1])) | ||
return | ||
meta.removals.push([match.index!, match.index! + match[0].length + 1]) | ||
}) | ||
|
||
findCutNotations(code, meta) | ||
Array.from(code.matchAll(reAnnonateMarkers)).forEach((match) => { | ||
const index = match.index! | ||
meta.removals.push([index, index + match[0].length + 1]) | ||
}) | ||
|
||
return removeCodeRanges(code, meta.removals).code | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Generated by scripts/flag-keys.ts | ||
export const flagKeys = [ | ||
'all', | ||
'allowArbitraryExtensions', | ||
'allowImportingTsExtensions', | ||
'allowJs', | ||
'allowSyntheticDefaultImports', | ||
'allowUmdGlobalAccess', | ||
'allowUnreachableCode', | ||
'allowUnusedLabels', | ||
'alwaysStrict', | ||
'assumeChangesOnlyAffectDirectDependencies', | ||
'baseUrl', | ||
'build', | ||
'charset', | ||
'checkJs', | ||
'composite', | ||
'customConditions', | ||
'declaration', | ||
'declarationDir', | ||
'declarationMap', | ||
'diagnostics', | ||
'disableReferencedProjectLoad', | ||
'disableSizeLimit', | ||
'disableSolutionSearching', | ||
'disableSourceOfProjectReferenceRedirect', | ||
'downlevelIteration', | ||
'emitBOM', | ||
'emitDeclarationOnly', | ||
'emitDecoratorMetadata', | ||
'errors', | ||
'esModuleInterop', | ||
'exactOptionalPropertyTypes', | ||
'experimentalDecorators', | ||
'explainFiles', | ||
'extendedDiagnostics', | ||
'forceConsistentCasingInFileNames', | ||
'generateCpuProfile', | ||
'generateTrace', | ||
'help', | ||
'help', | ||
'ignoreDeprecations', | ||
'importHelpers', | ||
'importsNotUsedAsValues', | ||
'incremental', | ||
'init', | ||
'inlineSourceMap', | ||
'inlineSources', | ||
'isolatedModules', | ||
'jsx', | ||
'jsxFactory', | ||
'jsxFragmentFactory', | ||
'jsxImportSource', | ||
'keepNotations', | ||
'keyofStringsOnly', | ||
'lib', | ||
'listEmittedFiles', | ||
'listFiles', | ||
'listFilesOnly', | ||
'locale', | ||
'mapRoot', | ||
'maxNodeModuleJsDepth', | ||
'module', | ||
'moduleDetection', | ||
'moduleResolution', | ||
'moduleSuffixes', | ||
'newLine', | ||
'noEmit', | ||
'noEmitHelpers', | ||
'noEmitOnError', | ||
'noErrorTruncation', | ||
'noErrorValidation', | ||
'noErrors', | ||
'noErrorsCutted', | ||
'noFallthroughCasesInSwitch', | ||
'noImplicitAny', | ||
'noImplicitOverride', | ||
'noImplicitReturns', | ||
'noImplicitThis', | ||
'noImplicitUseStrict', | ||
'noLib', | ||
'noPropertyAccessFromIndexSignature', | ||
'noResolve', | ||
'noStaticSemanticInfo', | ||
'noStrictGenericChecks', | ||
'noUncheckedIndexedAccess', | ||
'noUnusedLocals', | ||
'noUnusedParameters', | ||
'out', | ||
'outDir', | ||
'outFile', | ||
'paths', | ||
'plugins', | ||
'preserveConstEnums', | ||
'preserveSymlinks', | ||
'preserveValueImports', | ||
'preserveWatchOutput', | ||
'pretty', | ||
'project', | ||
'reactNamespace', | ||
'removeComments', | ||
'resolveJsonModule', | ||
'resolvePackageJsonExports', | ||
'resolvePackageJsonImports', | ||
'rootDir', | ||
'rootDirs', | ||
'showConfig', | ||
'showEmit', | ||
'showEmittedFile', | ||
'skipDefaultLibCheck', | ||
'skipLibCheck', | ||
'sourceMap', | ||
'sourceRoot', | ||
'strict', | ||
'strictBindCallApply', | ||
'strictFunctionTypes', | ||
'strictNullChecks', | ||
'strictPropertyInitialization', | ||
'stripInternal', | ||
'suppressExcessPropertyErrors', | ||
'suppressImplicitAnyIndexErrors', | ||
'target', | ||
'traceResolution', | ||
'tsBuildInfoFile', | ||
'typeRoots', | ||
'types', | ||
'useDefineForClassFields', | ||
'useUnknownInCatchVariables', | ||
'verbatimModuleSyntax', | ||
'version', | ||
'watch', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters