BVB Source Codes

prettier Show parser-typescript.js Source code

Return Download prettier: download parser-typescript.js Source code - Download prettier Source code - Type:.js
  1. "use strict";
  2.  
  3. const createError = require("./parser-create-error");
  4. const includeShebang = require("./parser-include-shebang");
  5.  
  6. function parse(text) {
  7.   const jsx = isProbablyJsx(text);
  8.   let ast;
  9.   try {
  10.     try {
  11.       // Try passing with our best guess first.
  12.       ast = tryParseTypeScript(text, jsx);
  13.     } catch (e) {
  14.       // But if we get it wrong, try the opposite.
  15.       ast = tryParseTypeScript(text, !jsx);
  16.     }
  17.   } catch (e) {
  18.     throw createError(e.message, {
  19.       start: { line: e.lineNumber, column: e.column + 1 }
  20.     });
  21.   }
  22.  
  23.   delete ast.tokens;
  24.   includeShebang(text, ast);
  25.   return ast;
  26. }
  27.  
  28. function tryParseTypeScript(text, jsx) {
  29.   // While we are working on typescript, we are putting it in devDependencies
  30.   // so it shouldn't be picked up by static analysis
  31.   const parser = require("typescript-eslint-parser");
  32.   return parser.parse(text, {
  33.     loc: true,
  34.     range: true,
  35.     tokens: true,
  36.     comment: true,
  37.     useJSXTextNode: true,
  38.     ecmaFeatures: { jsx }
  39.   });
  40. }
  41.  
  42. /**
  43.  * Use a naive regular expression until we address
  44.  * https://github.com/prettier/prettier/issues/1538
  45.  */
  46. function isProbablyJsx(text) {
  47.   return new RegExp(
  48.     [
  49.       "(^[^\"'`]*</)", // Contains "</" when probably not in a string
  50.       "|",
  51.       "(^[^/]{2}.*/>)" // Contains "/>" on line not starting with "//"
  52.     ].join(""),
  53.     "m"
  54.   ).test(text);
  55. }
  56.  
  57. module.exports = parse;
  58.  
downloadparser-typescript.js Source code - Download prettier Source code
Related Source Codes/Software:
storybook - 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
parse-server - Parse-compatible API server module for Node/Expres... 2017-06-07
inferno - An extremely fast, React-like JavaScript library f... 2017-06-08
guetzli - Perceptual JPEG encoder 2017-06-08
cs-video-courses - List of Computer Science courses with video lectur... 2017-06-08
interviews - Everything you need to know to get the job. 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top