-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Use parser to parse tsconfig json instead of using Json.parse #12336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cca98c3
b3f816b
0b3074f
25be912
005123f
6419de9
0dd944a
c7744a8
f83e754
236e15a
0bbbc51
d3ce95f
911511e
98bd310
cb1b164
6f568b3
d7e9609
b60de87
c97b389
ec2f967
ea60e99
7cf93f9
20570b0
f1ea38d
5501892
16fd947
7bd9e09
7db76ed
8d771ca
8f4f6c5
d680720
c4ad151
48189c8
09f0b34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
/// <reference path="utilities.ts"/> | ||
/// <reference path="scanner.ts"/> | ||
/// <reference path="factory.ts"/> | ||
|
||
namespace ts { | ||
let NodeConstructor: new (kind: SyntaxKind, pos: number, end: number) => Node; | ||
|
@@ -466,6 +465,15 @@ namespace ts { | |
return Parser.parseIsolatedEntityName(text, languageVersion); | ||
} | ||
|
||
/** | ||
* Parse json text into SyntaxTree and return node and parse errors if any | ||
* @param fileName | ||
* @param sourceText | ||
*/ | ||
export function parseJsonText(fileName: string, sourceText: string): JsonSourceFile { | ||
return Parser.parseJsonText(fileName, sourceText); | ||
} | ||
|
||
// See also `isExternalOrCommonJsModule` in utilities.ts | ||
export function isExternalModule(file: SourceFile): boolean { | ||
return file.externalModuleIndicator !== undefined; | ||
|
@@ -632,9 +640,34 @@ namespace ts { | |
return isInvalid ? entityName : undefined; | ||
} | ||
|
||
export function parseJsonText(fileName: string, sourceText: string): JsonSourceFile { | ||
initializeState(sourceText, ScriptTarget.ES2015, /*syntaxCursor*/ undefined, ScriptKind.JSON); | ||
// Set source file so that errors will be reported with this file name | ||
sourceFile = createSourceFile(fileName, ScriptTarget.ES2015, ScriptKind.JSON); | ||
const result = <JsonSourceFile>sourceFile; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Be sure to use |
||
|
||
// Prime the scanner. | ||
nextToken(); | ||
if (token() === SyntaxKind.EndOfFileToken) { | ||
sourceFile.endOfFileToken = <EndOfFileToken>parseTokenNode(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need an EndOfFileToken? |
||
} | ||
else if (token() === SyntaxKind.OpenBraceToken || | ||
lookAhead(() => token() === SyntaxKind.StringLiteral)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this happen often? i am assuming this is to handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to handle one of the failing test case which was used to sanitize the tsconfig.json There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you give a comment why we need a look ahead to be string literal? also would the the property is allow to be identifier since we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to ECMA-404, the key for the property of a JSON object must be a string literal. |
||
result.jsonObject = parseObjectLiteralExpression(); | ||
sourceFile.endOfFileToken = parseExpectedToken(SyntaxKind.EndOfFileToken, /*reportAtCurrentPosition*/ false, Diagnostics.Unexpected_token); | ||
} | ||
else { | ||
parseExpected(SyntaxKind.OpenBraceToken); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are only planning on using this to parse tsconfig.json, then this is fine. If we are planning on using this to parse other JSON files, there are other legal root objects for a JSON file (e.g. Array Literals, string literals, numeric literals, |
||
} | ||
|
||
sourceFile.parseDiagnostics = parseDiagnostics; | ||
clearState(); | ||
return result; | ||
} | ||
|
||
function getLanguageVariant(scriptKind: ScriptKind) { | ||
// .tsx and .jsx files are treated as jsx language variant. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment probably should be removed |
||
return scriptKind === ScriptKind.TSX || scriptKind === ScriptKind.JSX || scriptKind === ScriptKind.JS ? LanguageVariant.JSX : LanguageVariant.Standard; | ||
return scriptKind === ScriptKind.TSX || scriptKind === ScriptKind.JSX || scriptKind === ScriptKind.JS || scriptKind === ScriptKind.JSON ? LanguageVariant.JSX : LanguageVariant.Standard; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSON files can't contain JSX anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we |
||
} | ||
|
||
function initializeState(_sourceText: string, languageVersion: ScriptTarget, _syntaxCursor: IncrementalParser.SyntaxCursor, scriptKind: ScriptKind) { | ||
|
@@ -652,7 +685,7 @@ namespace ts { | |
identifierCount = 0; | ||
nodeCount = 0; | ||
|
||
contextFlags = scriptKind === ScriptKind.JS || scriptKind === ScriptKind.JSX ? NodeFlags.JavaScriptFile : NodeFlags.None; | ||
contextFlags = scriptKind === ScriptKind.JS || scriptKind === ScriptKind.JSX || scriptKind === ScriptKind.JSON ? NodeFlags.JavaScriptFile : NodeFlags.None; | ||
parseErrorBeforeNextFinishedNode = false; | ||
|
||
// Initialize and prime the scanner before parsing the source elements. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably shouldn't use
createSourceFile
, since that initializes a lot of variables these will never use.