@typescript-eslint/typescript-estree
Advanced tools
Comparing version 1.1.1-alpha.10 to 1.1.1-alpha.11
@@ -11,6 +11,4 @@ import ts from 'typescript'; | ||
} : {}); | ||
export declare const version: string; | ||
export declare function parse<T extends ParserOptions = ParserOptions>(code: string, options?: T): AST<T>; | ||
export declare function parseAndGenerateServices(code: string, options: ParserOptions): { | ||
ast: Program; | ||
interface ParseAndGenerateServicesResult<T extends ParserOptions> { | ||
ast: AST<T>; | ||
services: { | ||
@@ -21,4 +19,7 @@ program: ts.Program | undefined; | ||
}; | ||
}; | ||
} | ||
export declare const version: string; | ||
export declare function parse<T extends ParserOptions = ParserOptions>(code: string, options?: T): AST<T>; | ||
export declare function parseAndGenerateServices<T extends ParserOptions = ParserOptions>(code: string, options: T): ParseAndGenerateServicesResult<T>; | ||
export { AST_NODE_TYPES } from './ast-node-types'; | ||
export { ParserOptions }; |
@@ -27,4 +27,7 @@ "use strict"; | ||
/** | ||
* Compute the filename based on the parser options | ||
* Compute the filename based on the parser options. | ||
* | ||
* Even if jsx option is set in typescript compiler, filename still has to | ||
* contain .tsx file extension. | ||
* | ||
* @param options Parser options | ||
@@ -85,4 +88,2 @@ */ | ||
function createNewProgram(code) { | ||
// Even if jsx option is set in typescript compiler, filename still has to | ||
// contain .tsx file extension | ||
const FILENAME = getFileName(extra); | ||
@@ -141,72 +142,70 @@ const compilerHost = { | ||
} | ||
/** | ||
* Parses the given source code to produce a valid AST | ||
* @param {string} code TypeScript code | ||
* @param {boolean} shouldGenerateServices Flag determining whether to generate ast maps and program or not | ||
* @param {ParserOptions} options configuration object for the parser | ||
* @returns {Object} the AST | ||
*/ | ||
function generateAST(code, options = {}, shouldGenerateServices = false) { | ||
if (typeof code !== 'string' && !(code instanceof String)) { | ||
code = String(code); | ||
function applyParserOptionsToExtra(options) { | ||
/** | ||
* Track range information in the AST | ||
*/ | ||
extra.range = typeof options.range === 'boolean' && options.range; | ||
extra.loc = typeof options.loc === 'boolean' && options.loc; | ||
/** | ||
* Track tokens in the AST | ||
*/ | ||
if (typeof options.tokens === 'boolean' && options.tokens) { | ||
extra.tokens = []; | ||
} | ||
resetExtra(); | ||
if (typeof options !== 'undefined') { | ||
extra.range = typeof options.range === 'boolean' && options.range; | ||
extra.loc = typeof options.loc === 'boolean' && options.loc; | ||
if (typeof options.tokens === 'boolean' && options.tokens) { | ||
extra.tokens = []; | ||
} | ||
if (typeof options.comment === 'boolean' && options.comment) { | ||
extra.comment = true; | ||
extra.comments = []; | ||
} | ||
if (typeof options.jsx === 'boolean' && options.jsx) { | ||
extra.jsx = true; | ||
} | ||
/** | ||
* Allow the user to cause the parser to error if it encounters an unknown AST Node Type | ||
* (used in testing). | ||
*/ | ||
if (typeof options.errorOnUnknownASTType === 'boolean' && | ||
options.errorOnUnknownASTType) { | ||
extra.errorOnUnknownASTType = true; | ||
} | ||
/** | ||
* Retrieve semantic and syntactic diagnostics from the underlying TypeScript Program | ||
* and turn them into parse errors | ||
*/ | ||
if (shouldGenerateServices && | ||
typeof options.errorOnTypeScriptSyntacticAndSemanticIssues === | ||
'boolean' && | ||
options.errorOnTypeScriptSyntacticAndSemanticIssues) { | ||
extra.errorOnTypeScriptSyntacticAndSemanticIssues = true; | ||
} | ||
if (typeof options.useJSXTextNode === 'boolean' && options.useJSXTextNode) { | ||
extra.useJSXTextNode = true; | ||
} | ||
/** | ||
* Allow the user to override the function used for logging | ||
*/ | ||
if (typeof options.loggerFn === 'function') { | ||
extra.log = options.loggerFn; | ||
} | ||
else if (options.loggerFn === false) { | ||
extra.log = Function.prototype; | ||
} | ||
if (typeof options.project === 'string') { | ||
extra.projects = [options.project]; | ||
} | ||
else if (Array.isArray(options.project) && | ||
options.project.every(projectPath => typeof projectPath === 'string')) { | ||
extra.projects = options.project; | ||
} | ||
if (typeof options.tsconfigRootDir === 'string') { | ||
extra.tsconfigRootDir = options.tsconfigRootDir; | ||
} | ||
if (Array.isArray(options.extraFileExtensions) && | ||
options.extraFileExtensions.every(ext => typeof ext === 'string')) { | ||
extra.extraFileExtensions = options.extraFileExtensions; | ||
} | ||
/** | ||
* Track comments in the AST | ||
*/ | ||
if (typeof options.comment === 'boolean' && options.comment) { | ||
extra.comment = true; | ||
extra.comments = []; | ||
} | ||
/** | ||
* Enable JSX - note the applicable file extension is still required | ||
*/ | ||
if (typeof options.jsx === 'boolean' && options.jsx) { | ||
extra.jsx = true; | ||
} | ||
/** | ||
* The JSX AST changed the node type for string literals | ||
* inside a JSX Element from `Literal` to `JSXText`. | ||
* | ||
* When value is `true`, these nodes will be parsed as type `JSXText`. | ||
* When value is `false`, these nodes will be parsed as type `Literal`. | ||
*/ | ||
if (typeof options.useJSXTextNode === 'boolean' && options.useJSXTextNode) { | ||
extra.useJSXTextNode = true; | ||
} | ||
/** | ||
* Allow the user to cause the parser to error if it encounters an unknown AST Node Type | ||
* (used in testing) | ||
*/ | ||
if (typeof options.errorOnUnknownASTType === 'boolean' && | ||
options.errorOnUnknownASTType) { | ||
extra.errorOnUnknownASTType = true; | ||
} | ||
/** | ||
* Allow the user to override the function used for logging | ||
*/ | ||
if (typeof options.loggerFn === 'function') { | ||
extra.log = options.loggerFn; | ||
} | ||
else if (options.loggerFn === false) { | ||
extra.log = Function.prototype; | ||
} | ||
if (typeof options.project === 'string') { | ||
extra.projects = [options.project]; | ||
} | ||
else if (Array.isArray(options.project) && | ||
options.project.every(projectPath => typeof projectPath === 'string')) { | ||
extra.projects = options.project; | ||
} | ||
if (typeof options.tsconfigRootDir === 'string') { | ||
extra.tsconfigRootDir = options.tsconfigRootDir; | ||
} | ||
if (Array.isArray(options.extraFileExtensions) && | ||
options.extraFileExtensions.every(ext => typeof ext === 'string')) { | ||
extra.extraFileExtensions = options.extraFileExtensions; | ||
} | ||
} | ||
function warnAboutTSVersion() { | ||
if (!isRunningSupportedTypeScriptVersion && !warnedAboutTSVersion) { | ||
@@ -226,8 +225,85 @@ const border = '============='; | ||
} | ||
const shouldProvideParserServices = shouldGenerateServices && extra.projects && extra.projects.length > 0; | ||
const { ast, program } = getProgramAndAST(code, options, shouldProvideParserServices); | ||
} | ||
//------------------------------------------------------------------------------ | ||
// Public | ||
//------------------------------------------------------------------------------ | ||
exports.version = packageJSON.version; | ||
function parse(code, options) { | ||
/** | ||
* Reset the parse configuration | ||
*/ | ||
resetExtra(); | ||
/** | ||
* Ensure users do not attempt to use parse() when they need parseAndGenerateServices() | ||
*/ | ||
if (options && options.errorOnTypeScriptSyntacticAndSemanticIssues) { | ||
throw new Error(`"errorOnTypeScriptSyntacticAndSemanticIssues" is only supported for parseAndGenerateServices()`); | ||
} | ||
/** | ||
* Ensure the source code is a string, and store a reference to it | ||
*/ | ||
if (typeof code !== 'string' && !(code instanceof String)) { | ||
code = String(code); | ||
} | ||
extra.code = code; | ||
/** | ||
* Convert the AST | ||
* Apply the given parser options | ||
*/ | ||
if (typeof options !== 'undefined') { | ||
applyParserOptionsToExtra(options); | ||
} | ||
/** | ||
* Warn if the user is using an unsupported version of TypeScript | ||
*/ | ||
warnAboutTSVersion(); | ||
/** | ||
* Create a ts.SourceFile directly, no ts.Program is needed for a simple | ||
* parse | ||
*/ | ||
const ast = typescript_1.default.createSourceFile(getFileName(extra), code, typescript_1.default.ScriptTarget.Latest, | ||
/* setParentNodes */ true); | ||
/** | ||
* Convert the TypeScript AST to an ESTree-compatible one | ||
*/ | ||
const { estree } = ast_converter_1.default(ast, extra, false); | ||
return estree; | ||
} | ||
exports.parse = parse; | ||
function parseAndGenerateServices(code, options) { | ||
/** | ||
* Reset the parse configuration | ||
*/ | ||
resetExtra(); | ||
/** | ||
* Ensure the source code is a string, and store a reference to it | ||
*/ | ||
if (typeof code !== 'string' && !(code instanceof String)) { | ||
code = String(code); | ||
} | ||
extra.code = code; | ||
/** | ||
* Apply the given parser options | ||
*/ | ||
if (typeof options !== 'undefined') { | ||
applyParserOptionsToExtra(options); | ||
if (typeof options.errorOnTypeScriptSyntacticAndSemanticIssues === | ||
'boolean' && | ||
options.errorOnTypeScriptSyntacticAndSemanticIssues) { | ||
extra.errorOnTypeScriptSyntacticAndSemanticIssues = true; | ||
} | ||
} | ||
/** | ||
* Warn if the user is using an unsupported version of TypeScript | ||
*/ | ||
warnAboutTSVersion(); | ||
/** | ||
* Generate a full ts.Program in order to be able to provide parser | ||
* services, such as type-checking | ||
*/ | ||
const shouldProvideParserServices = extra.projects && extra.projects.length > 0; | ||
const { ast, program } = getProgramAndAST(code, options, shouldProvideParserServices); | ||
/** | ||
* Convert the TypeScript AST to an ESTree-compatible one, and optionally preserve | ||
* mappings between converted and original AST nodes | ||
*/ | ||
const { estree, astMaps } = ast_converter_1.default(ast, extra, shouldProvideParserServices); | ||
@@ -244,29 +320,15 @@ /** | ||
} | ||
/** | ||
* Return the converted AST and additional parser services | ||
*/ | ||
return { | ||
estree, | ||
program: shouldProvideParserServices ? program : undefined, | ||
astMaps: shouldProvideParserServices | ||
? astMaps | ||
: { esTreeNodeToTSNodeMap: undefined, tsNodeToESTreeNodeMap: undefined } | ||
}; | ||
} | ||
//------------------------------------------------------------------------------ | ||
// Public | ||
//------------------------------------------------------------------------------ | ||
exports.version = packageJSON.version; | ||
function parse(code, options) { | ||
if (options && options.errorOnTypeScriptSyntacticAndSemanticIssues) { | ||
throw new Error(`"errorOnTypeScriptSyntacticAndSemanticIssues" is only supported for parseAndGenerateServices()`); | ||
} | ||
return generateAST(code, options).estree; | ||
} | ||
exports.parse = parse; | ||
function parseAndGenerateServices(code, options) { | ||
const result = generateAST(code, options, /*shouldGenerateServices*/ true); | ||
return { | ||
ast: result.estree, | ||
ast: estree, | ||
services: { | ||
program: result.program, | ||
esTreeNodeToTSNodeMap: result.astMaps.esTreeNodeToTSNodeMap, | ||
tsNodeToESTreeNodeMap: result.astMaps.tsNodeToESTreeNodeMap | ||
program: shouldProvideParserServices ? program : undefined, | ||
esTreeNodeToTSNodeMap: shouldProvideParserServices && astMaps | ||
? astMaps.esTreeNodeToTSNodeMap | ||
: undefined, | ||
tsNodeToESTreeNodeMap: shouldProvideParserServices && astMaps | ||
? astMaps.tsNodeToESTreeNodeMap | ||
: undefined | ||
} | ||
@@ -273,0 +335,0 @@ }; |
{ | ||
"name": "@typescript-eslint/typescript-estree", | ||
"version": "1.1.1-alpha.10+5017c11", | ||
"version": "1.1.1-alpha.11+aacf5b0", | ||
"description": "A parser that converts TypeScript source code into an ESTree compatible form", | ||
@@ -45,6 +45,6 @@ "main": "dist/parser.js", | ||
"devDependencies": { | ||
"@typescript-eslint/shared-fixtures": "1.1.1-alpha.10+5017c11", | ||
"@typescript-eslint/shared-fixtures": "1.1.1-alpha.11+aacf5b0", | ||
"typescript": "~3.2.1" | ||
}, | ||
"gitHead": "5017c115f232b42285c61ae53dcf531d66a9c4b5" | ||
"gitHead": "aacf5b05c7f0d802bbc28222d7c95141fa0ff86c" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
217519
5295