Files
test-repo/src/language-js/parser-typescript.js
T
Sosuke Suzukiandfisker Cheung 4cc149248a Support TypeScript 4.1 via typescript-estree (#9636)
* Update typescript and typescript-estree

* Add AST transformation for TSTemplateLiteralType

* Update tests for template-literal-types

* Fix transformChainExpression

* Update tests for key-remapping-in-mapped-types

* Implement recoverInvalidDecorators

* Update changelog

* Avoid typecheck error

* Add os.platform() shim

* Update config.js

* Update config.js

* Shim os.cpus

* Update `process.versions.node`

* Attempt to fix build

* Update ts parserOptions

* Fix code style

* Remove `errorOnUnknownASTType`, fix code style

* Use `[] ||` instead of `[] //`

* Fix hack

* Add invalid tests

* checkMissingDecorators

* Update tests

* Fix tests

* Add comment

* Revert "Implement recoverInvalidDecorators"

This reverts commit 27e7b46ed41df36fb585791117c85e1b2dab932c.

* Add ref pr link

* Simplify logic

* Don't transform `TSTemplateLiteralType`

* Add `TSTemplateLiteralType` as simple type

* Fix tests

* Update comments

* Add comment

* Fix logic

* Minor tweak

* use multiple parsers

* Update @typescript-eslint/typescript-estree to v4.8.0

* Switch to `parseWithNodeMaps`

* Rename `esNode` -> `esTreeNode`

* Update @typescript-eslint/typescript-estree to 4.8.1-alpha.1

* Use @typescript-eslint/typescript-estree 4.8.1

Co-authored-by: fisker Cheung <lionkay@gmail.com>
2020-11-18 10:59:58 +09:00

78 lines
1.9 KiB
JavaScript

"use strict";
const createError = require("../common/parser-create-error");
const { hasPragma } = require("./pragma");
const { locStart, locEnd } = require("./loc");
const postprocess = require("./parse-postprocess");
function parse(text, parsers, opts) {
const jsx = isProbablyJsx(text);
let result;
try {
// Try passing with our best guess first.
result = tryParseTypeScript(text, jsx);
} catch (firstError) {
try {
// But if we get it wrong, try the opposite.
result = tryParseTypeScript(text, !jsx);
} catch (secondError) {
// Suppose our guess is correct, throw the first error
const { message, lineNumber, column } = firstError;
/* istanbul ignore next */
if (typeof lineNumber !== "number") {
throw firstError;
}
throw createError(message, {
start: { line: lineNumber, column: column + 1 },
});
}
}
return postprocess(result.ast, {
...opts,
originalText: text,
tsParseResult: result,
});
}
function tryParseTypeScript(text, jsx) {
const { parseWithNodeMaps } = require("@typescript-eslint/typescript-estree");
return parseWithNodeMaps(text, {
// `jest@<=26.4.2` rely on `loc`
// https://github.com/facebook/jest/issues/10444
loc: true,
range: true,
comment: true,
useJSXTextNode: true,
jsx,
tokens: true,
loggerFn: false,
project: [],
});
}
/**
* Use a naive regular expression to detect JSX
*/
function isProbablyJsx(text) {
return new RegExp(
[
"(^[^\"'`]*</)", // Contains "</" when probably not in a string
"|",
"(^[^/]{2}.*/>)", // Contains "/>" on line not starting with "//"
].join(""),
"m"
).test(text);
}
const parser = { parse, astFormat: "estree", hasPragma, locStart, locEnd };
// Export as a plugin so we can reuse the same bundle for UMD loading
module.exports = {
parsers: {
typescript: parser,
},
};