Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 16, 2023
1 parent f4641e5 commit cfac775
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
9 changes: 0 additions & 9 deletions src/language-js/parse/postprocess/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,9 @@ const isTsKeywordType = require("../../utils/is-ts-keyword-type.js");
const isTypeCastComment = require("../../utils/is-type-cast-comment.js");
const getLast = require("../../../utils/get-last.js");
const visitNode = require("./visit-node.js");
const { throwErrorForInvalidNodes } = require("./typescript.js");
const throwSyntaxError = require("./throw-syntax-error.js");

function postprocess(ast, options) {
if (
options.parser === "typescript" &&
// decorators or abstract properties
/@|abstract/.test(options.originalText)
) {
throwErrorForInvalidNodes(ast, options);
}

// Keep Babel's non-standard ParenthesizedExpression nodes only if they have Closure-style type cast comments.
if (
options.parser !== "typescript" &&
Expand Down
19 changes: 12 additions & 7 deletions src/language-js/parse/postprocess/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ function getSourceFileOfNode(node) {
return node;
}


function throwErrorOnTsNode(node) {
function throwErrorOnTsNode(node, message) {
const sourceFile = getSourceFileOfNode(node);
const [start, end] = [node.getStart(), node.end].map((position) => {
const { line, character: column } =
sourceFile.getLineAndCharacterOfPosition(position);
return { line: line + 1, column };
});

throwSyntaxError({ loc: { start, end } }, "Decorators are not valid here.");
throwSyntaxError({ loc: { start, end } }, message);
}

// Invalid decorators are removed since `@typescript-eslint/typescript-estree` v4
Expand Down Expand Up @@ -112,11 +111,17 @@ function throwErrorForInvalidAbstractProperty(tsNode, esTreeNode) {
}
}

function throwErrorForInvalidNodes(ast, options) {
const { esTreeNodeToTSNodeMap, tsNodeToESTreeNodeMap } =
options.tsParseResult;
function throwErrorForInvalidNodes(result, options) {
if (
// decorators or abstract properties
!/@|abstract/.test(options.originalText)
) {
return;
}

const { esTreeNodeToTSNodeMap, tsNodeToESTreeNodeMap } = result;

visitNode(ast, (node) => {
visitNode(result.ast, (node) => {
const tsNode = esTreeNodeToTSNodeMap.get(node);
if (!tsNode) {
return;
Expand Down
4 changes: 3 additions & 1 deletion src/language-js/parse/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const tryCombinations = require("../../utils/try-combinations.js");
const createParser = require("./utils/create-parser.js");
const replaceHashbang = require("./utils/replace-hashbang.js");
const postprocess = require("./postprocess/index.js");
const { throwErrorForInvalidNodes } = require("./postprocess/typescript.js");

/** @type {import("@typescript-eslint/typescript-estree").TSESTreeOptions} */
const parseOptions = {
Expand Down Expand Up @@ -50,7 +51,8 @@ function parse(text, parsers, options = {}) {
}

options.originalText = text;
options.tsParseResult = result;
throwErrorForInvalidNodes(result, options);

return postprocess(result.ast, options);
}

Expand Down

0 comments on commit cfac775

Please sign in to comment.