/// <summary> /// Parses and validates code into an ISourceFile. /// </summary> public static bool TryParseTestFile(Logger logger, string filePath, string code, out ISourceFile sourceFile) { var parser = new TypeScript.Net.Parsing.Parser(); sourceFile = parser.ParseSourceFileContent(filePath, code, ParsingOptions.DefaultParsingOptions); if (sourceFile.ParseDiagnostics.Count > 0) { foreach (var diagnostic in sourceFile.ParseDiagnostics) { var lineAndColumn = diagnostic.GetLineAndColumn(sourceFile); logger.LogError(filePath, lineAndColumn.Character, lineAndColumn.Character, diagnostic.MessageText.ToString()); } return(false); } var binder = new Binder(); binder.BindSourceFile(sourceFile, CompilerOptions.Empty); if (sourceFile.BindDiagnostics.Count > 0) { foreach (var diagnostic in sourceFile.BindDiagnostics) { var lineAndColumn = diagnostic.GetLineAndColumn(sourceFile); logger.LogError(filePath, lineAndColumn.Character, lineAndColumn.Character, diagnostic.MessageText.ToString()); } return(false); } return(true); }
/// <nodoc/> public Expression ParseExpression(RuntimeModelContext runtimeModelContext, AbsolutePath path, string spec, FunctionScope localScope, bool useSemanticNameResolution) { Contract.Requires(runtimeModelContext != null); Contract.Requires(spec != null); var parser = new TypeScript.Net.Parsing.Parser(); // Wrap expression in a function call to make the parser happy. // Currently an object literal '{...};' is not a valid statement. var sourceFile = parser.ParseSourceFileContent( path.ToString(runtimeModelContext.PathTable), @"function createExpression(a: any) {} createExpression(" + spec + ");"); if (sourceFile.ParseDiagnostics.Count != 0) { ReportParseDiagnosticsIfNeeded(runtimeModelContext, sourceFile, path); return(null); } var workspaceConfiguration = WorkspaceConfiguration.CreateForTesting(); var workspace = new Workspace( provider: null, workspaceConfiguration: workspaceConfiguration, modules: new[] { CreateModuleFor(runtimeModelContext, sourceFile) }, failures: Enumerable.Empty <Failure>(), preludeModule: null, configurationModule: null); workspace = SemanticWorkspaceProvider.ComputeSemanticWorkspace(runtimeModelContext.PathTable, workspace, workspaceConfiguration).GetAwaiter().GetResult(); // Because we just created source file to parse, we know exactly what the AST is there. // This information helped to simplify analysis logic and migrate to semantic-base name resolution. var invocation = sourceFile.Statements[1].Cast <IExpressionStatement>().Expression.Cast <ICallExpression>(); // Only for expressions, full names should be preserved. // Only for expressions, no checks for definition before use to avoid contract assertion in location computation. m_conversionConfiguration.UnsafeOptions.DisableDeclarationBeforeUseCheck = true; var converter = CreateAstConverter(sourceFile, runtimeModelContext, path, m_conversionConfiguration, workspace: workspace); return(converter.ConvertExpression(invocation, localScope, useSemanticNameResolution)); }