@code-to-json/core
Version: 
[](https://travis-ci.org/code-to-json/code-to-json) [](https://d
32 lines (26 loc) • 859 B
text/typescript
import { UnreachableError } from '@code-to-json/utils';
import { SourceFile } from 'typescript';
export interface IWalkerOptionArgs {
  includeDeclarations: 'all' | 'none';
}
const DEFAULT_WALKER_OPTIONS: IWalkerOptionArgs = {
  includeDeclarations: 'none',
};
export interface WalkerOptions {
  shouldIncludeSourceFile(sf: SourceFile): boolean;
}
export default function createWalkerOptions(rawOpts: Partial<IWalkerOptionArgs>): WalkerOptions {
  const opts = Object.assign({}, DEFAULT_WALKER_OPTIONS, rawOpts);
  return {
    shouldIncludeSourceFile(sf: SourceFile): boolean {
      const { includeDeclarations } = opts;
      if (includeDeclarations === 'all') {
        return true;
      }
      if (includeDeclarations === 'none') {
        return !sf.isDeclarationFile;
      }
      throw new UnreachableError(includeDeclarations);
    },
  };
}