Skip to content

Commit

Permalink
feat: add index files to parser and typescript-estree
Browse files Browse the repository at this point in the history
This just creates a clearer entrypoint for contributors, and helps clearly define exactly what is exported from the modules.
  • Loading branch information
bradzacher committed May 21, 2020
1 parent 52b6085 commit 3dfc46d
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 61 deletions.
4 changes: 2 additions & 2 deletions packages/parser/package.json
Expand Up @@ -2,8 +2,8 @@
"name": "@typescript-eslint/parser",
"version": "2.34.0",
"description": "An ESLint custom parser which leverages TypeScript ESTree",
"main": "dist/parser.js",
"types": "dist/parser.d.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist",
"README.md",
Expand Down
6 changes: 6 additions & 0 deletions packages/parser/src/index.ts
@@ -0,0 +1,6 @@
export { parse, parseForESLint, ParserOptions } from './parser';
export { ParserServices } from '@typescript-eslint/typescript-estree';
export { clearCaches } from '@typescript-eslint/typescript-estree';

// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
export const version: string = require('../package.json').version;
19 changes: 3 additions & 16 deletions packages/parser/src/parser.ts
@@ -1,6 +1,5 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import {
AST_NODE_TYPES,
parseAndGenerateServices,
ParserServices,
TSESTreeOptions,
Expand All @@ -11,9 +10,6 @@ import { analyzeScope } from './analyze-scope';

type ParserOptions = TSESLint.ParserOptions;

// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
const packageJSON = require('../package.json');

interface ParseForESLintResult {
ast: TSESTree.Program & {
range?: [number, number];
Expand All @@ -35,22 +31,14 @@ function validateBoolean(
return value;
}

//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------

export const version = packageJSON.version;

export const Syntax = Object.freeze(AST_NODE_TYPES);

export function parse(
function parse(
code: string,
options?: ParserOptions,
): ParseForESLintResult['ast'] {
return parseForESLint(code, options).ast;
}

export function parseForESLint(
function parseForESLint(
code: string,
options?: ParserOptions | null,
): ParseForESLintResult {
Expand Down Expand Up @@ -98,5 +86,4 @@ export function parseForESLint(
return { ast, services, scopeManager, visitorKeys };
}

export { ParserServices, ParserOptions };
export { clearCaches } from '@typescript-eslint/typescript-estree';
export { parse, parseForESLint, ParserOptions };
15 changes: 1 addition & 14 deletions packages/parser/tests/lib/parser.ts
@@ -1,10 +1,8 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import * as typescriptESTree from '@typescript-eslint/typescript-estree';
import { parse, parseForESLint, Syntax } from '../../src/parser';
import { parse, parseForESLint } from '../../src/parser';
import * as scope from '../../src/analyze-scope';

const { AST_NODE_TYPES } = typescriptESTree;

describe('parser', () => {
it('parse() should return just the AST from parseForESLint()', () => {
const code = 'const valid = true;';
Expand Down Expand Up @@ -65,17 +63,6 @@ describe('parser', () => {
});
});

it('Syntax should contain a frozen object of AST_NODE_TYPES', () => {
expect(Syntax).toEqual(AST_NODE_TYPES);
expect(
// intentionally breaking the readonly
// eslint-disable-next-line @typescript-eslint/no-explicit-any
() => ((Syntax as any).ArrayExpression = 'foo'),
).toThrowErrorMatchingInlineSnapshot(
`"Cannot assign to read only property 'ArrayExpression' of object '#<Object>'"`,
);
});

it('`warnOnUnsupportedTypeScriptVersion: false` should set `loggerFn: false` on typescript-estree', () => {
const code = 'const valid = true;';
const spy = jest.spyOn(typescriptESTree, 'parseAndGenerateServices');
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-estree/package.json
Expand Up @@ -2,8 +2,8 @@
"name": "@typescript-eslint/typescript-estree",
"version": "2.34.0",
"description": "A parser that converts TypeScript source code into an ESTree compatible form",
"main": "dist/parser.js",
"types": "dist/parser.d.ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist",
"README.md",
Expand Down
14 changes: 14 additions & 0 deletions packages/typescript-estree/src/index.ts
@@ -0,0 +1,14 @@
export {
AST,
parse,
parseAndGenerateServices,
ParseAndGenerateServicesResult,
} from './parser';
export { ParserServices, TSESTreeOptions } from './parser-options';
export { simpleTraverse } from './simple-traverse';
export { visitorKeys } from './visitor-keys';
export * from './ts-estree';
export { clearCaches } from './create-program/createWatchProgram';

// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
export const version: string = require('../package.json').version;
23 changes: 1 addition & 22 deletions packages/typescript-estree/src/parser.ts
Expand Up @@ -323,10 +323,6 @@ function warnAboutTSVersion(): void {
}
}

//------------------------------------------------------------------------------
// Parser
//------------------------------------------------------------------------------

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface EmptyObject {}
type AST<T extends TSESTreeOptions> = TSESTree.Program &
Expand All @@ -338,12 +334,6 @@ interface ParseAndGenerateServicesResult<T extends TSESTreeOptions> {
services: ParserServices;
}

//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------

const version: string = require('../package.json').version;

function parse<T extends TSESTreeOptions = TSESTreeOptions>(
code: string,
options?: T,
Expand Down Expand Up @@ -472,15 +462,4 @@ function parseAndGenerateServices<T extends TSESTreeOptions = TSESTreeOptions>(
};
}

export {
AST,
parse,
parseAndGenerateServices,
ParseAndGenerateServicesResult,
version,
};
export { ParserServices, TSESTreeOptions } from './parser-options';
export { simpleTraverse } from './simple-traverse';
export { visitorKeys } from './visitor-keys';
export * from './ts-estree';
export { clearCaches } from './create-program/createWatchProgram';
export { AST, parse, parseAndGenerateServices, ParseAndGenerateServicesResult };
2 changes: 1 addition & 1 deletion packages/typescript-estree/tests/lib/parse.ts
@@ -1,6 +1,6 @@
import debug from 'debug';
import { join, resolve } from 'path';
import * as parser from '../../src/parser';
import * as parser from '../../src';
import * as astConverter from '../../src/ast-converter';
import { TSESTreeOptions } from '../../src/parser-options';
import * as sharedParserUtils from '../../src/create-program/shared';
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/tests/lib/persistentParse.ts
@@ -1,7 +1,7 @@
import fs from 'fs';
import path from 'path';
import tmp from 'tmp';
import { clearCaches, parseAndGenerateServices } from '../../src/parser';
import { clearCaches, parseAndGenerateServices } from '../../src';

const CONTENTS = {
foo: 'console.log("foo")',
Expand Down
@@ -1,6 +1,6 @@
import { readFileSync } from 'fs';
import glob from 'glob';
import * as parser from '../../src/parser';
import * as parser from '../../src';
import { extname } from 'path';
import { formatSnapshotName, isJSXFileType } from '../../tools/test-utils';

Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/tests/lib/semanticInfo.ts
Expand Up @@ -12,7 +12,7 @@ import {
clearCaches,
parseAndGenerateServices,
ParseAndGenerateServicesResult,
} from '../../src/parser';
} from '../../src';
import { TSESTree } from '../../src/ts-estree';

const FIXTURES_DIR = './tests/fixtures/semanticInfo';
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/tools/test-utils.ts
@@ -1,4 +1,4 @@
import * as parser from '../src/parser';
import * as parser from '../src';
import { TSESTreeOptions } from '../src/parser-options';

export function parseCodeAndGenerateServices(
Expand Down

0 comments on commit 3dfc46d

Please sign in to comment.