Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Breaking: Infer parsing as JSX from file extension (fixes #399) #436

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ const SUPPORTED_TYPESCRIPT_VERSIONS = require("./package.json").devDependencies.
const ACTIVE_TYPESCRIPT_VERSION = ts.version;
const isRunningSupportedTypeScriptVersion = semver.satisfies(ACTIVE_TYPESCRIPT_VERSION, SUPPORTED_TYPESCRIPT_VERSIONS);

const WARNING_BORDER = "=============";
const DEFAULT_ESLINT_FILEPATH = "<text>";

let extra;
let warnedAboutTSVersion = false;
let warnedAboutJSXOverride = false;

/**
* Resets the extra config object
Expand Down Expand Up @@ -73,19 +77,32 @@ function generateAST(code, options, additionalParsingContext) {
if (typeof options.tokens === "boolean" && options.tokens) {
extra.tokens = [];
}

if (typeof options.comment === "boolean" && options.comment) {
extra.comment = true;
extra.comments = [];
}

if (typeof options.tolerant === "boolean" && options.tolerant) {
extra.errors = [];
}

if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") {
// pass through jsx option
const hasEcmaFeatures = options.ecmaFeatures && typeof options.ecmaFeatures === "object";

// Allows user to parse a string of text passed on the command line in JSX mode.
if (hasEcmaFeatures) {
extra.ecmaFeatures.jsx = options.ecmaFeatures.jsx;
}

const hasFilePath = additionalParsingContext.isParseForESLint ? options.filePath !== DEFAULT_ESLINT_FILEPATH : options.filePath;
const hasTsxExtension = hasFilePath && /.tsx$/.test(options.filePath);

// Infer whether or not the parser should parse in "JSX mode" or not.
// This will override the parserOptions.ecmaFeatures.jsx config option if a filePath is provided.
if (hasFilePath) {
extra.ecmaFeatures.jsx = hasTsxExtension;
}

/**
* Allow the user to cause the parser to error if it encounters an unknown AST Node Type
* (used in testing).
Expand Down Expand Up @@ -114,18 +131,29 @@ function generateAST(code, options, additionalParsingContext) {
if (additionalParsingContext.isParseForESLint) {
extra.parseForESLint = true;
}

if (!warnedAboutJSXOverride && hasFilePath && hasEcmaFeatures && typeof options.ecmaFeatures.jsx !== "undefined") {
const warning = [
WARNING_BORDER,
"typescript-eslint-parser will automatically detect whether it should be parsing in JSX mode or not based on file extension.",
"Consider removing parserOptions.ecmaFeatures.jsx from your configuration, as it will be overridden by the extension of the file being parsed.",
WARNING_BORDER
];

extra.log(warning.join("\n\n"));
warnedAboutJSXOverride = true;
}
}

if (!isRunningSupportedTypeScriptVersion && !warnedAboutTSVersion) {
const border = "=============";
const versionWarning = [
border,
WARNING_BORDER,
"WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-eslint-parser.",
"You may find that it works just fine, or you may not.",
`SUPPORTED TYPESCRIPT VERSIONS: ${SUPPORTED_TYPESCRIPT_VERSIONS}`,
`YOUR TYPESCRIPT VERSION: ${ACTIVE_TYPESCRIPT_VERSION}`,
"Please only submit bug reports when using the officially supported version.",
border
WARNING_BORDER
];
extra.log(versionWarning.join("\n\n"));
warnedAboutTSVersion = true;
Expand Down