Use ?. where it represents the intended semantics (#11512)

This commit is contained in:
Nicolò Ribaudo
2020-05-09 23:31:50 +02:00
committed by GitHub
parent aeb51f463c
commit 31b361b736
47 changed files with 99 additions and 118 deletions

View File

@@ -16,7 +16,7 @@ import "./tokenizer/context";
import type { Expression, File } from "./types";
export function parse(input: string, options?: Options): File {
if (options && options.sourceType === "unambiguous") {
if (options?.sourceType === "unambiguous") {
options = {
...options,
};
@@ -71,7 +71,7 @@ export { tokTypes };
function getParser(options: ?Options, input: string): Parser {
let cls = Parser;
if (options && options.plugins) {
if (options?.plugins) {
validatePlugins(options.plugins);
cls = getParserClass(options.plugins);
}

View File

@@ -769,7 +769,7 @@ export default class ExpressionParser extends LValParser {
this.raise(node.start, Errors.ImportCallArity);
} else {
const importArg = node.arguments[0];
if (importArg && importArg.type === "SpreadElement") {
if (importArg?.type === "SpreadElement") {
this.raise(importArg.start, Errors.ImportCallSpreadArgument);
}
}

View File

@@ -160,9 +160,9 @@ export default class LValParser extends NodeUtils {
let end = exprList.length;
if (end) {
const last = exprList[end - 1];
if (last && last.type === "RestElement") {
if (last?.type === "RestElement") {
--end;
} else if (last && last.type === "SpreadElement") {
} else if (last?.type === "SpreadElement") {
last.type = "RestElement";
const arg = last.argument;
this.toAssignable(arg);
@@ -210,7 +210,7 @@ export default class LValParser extends NodeUtils {
this.toReferencedList(exprList, isParenthesizedExpr);
for (const expr of exprList) {
if (expr && expr.type === "ArrayExpression") {
if (expr?.type === "ArrayExpression") {
this.toReferencedListDeep(expr.elements);
}
}

View File

@@ -13,8 +13,8 @@ class Node implements NodeBase {
this.start = pos;
this.end = 0;
this.loc = new SourceLocation(loc);
if (parser && parser.options.ranges) this.range = [pos, 0];
if (parser && parser.filename) this.loc.filename = parser.filename;
if (parser?.options.ranges) this.range = [pos, 0];
if (parser?.filename) this.loc.filename = parser.filename;
}
type: string;

View File

@@ -165,7 +165,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (name === "__proto__" && prop.kind === "init") {
// Store the first redefinition's position
if (protoRef.used) {
if (refExpressionErrors && refExpressionErrors.doubleProto === -1) {
if (refExpressionErrors?.doubleProto === -1) {
refExpressionErrors.doubleProto = key.start;
} else {
this.raise(key.start, Errors.DuplicateProto);
@@ -181,7 +181,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
stmt.type === "ExpressionStatement" &&
stmt.expression.type === "Literal" &&
typeof stmt.expression.value === "string" &&
(!stmt.expression.extra || !stmt.expression.extra.parenthesized)
!stmt.expression.extra?.parenthesized
);
}

View File

@@ -2198,7 +2198,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
): $ReadOnlyArray<N.Pattern> {
for (let i = 0; i < exprList.length; i++) {
const expr = exprList[i];
if (expr && expr.type === "TypeCastExpression") {
if (expr?.type === "TypeCastExpression") {
exprList[i] = this.typeCastToParameter(expr);
}
}
@@ -2216,7 +2216,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (
expr &&
expr.type === "TypeCastExpression" &&
(!expr.extra || !expr.extra.parenthesized) &&
!expr.extra?.parenthesized &&
(exprList.length > 1 || !isParenthesizedExpr)
) {
this.raise(expr.typeAnnotation.start, FlowErrors.TypeCastInPattern);
@@ -2665,7 +2665,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}
if ((jsx && jsx.error) || this.isRelational("<")) {
if (jsx?.error || this.isRelational("<")) {
state = state || this.state.clone();
let typeParameters;
@@ -2690,9 +2690,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}, state);
const arrowExpression: ?N.ArrowFunctionExpression =
arrow.node && arrow.node.type === "ArrowFunctionExpression"
? arrow.node
: null;
arrow.node?.type === "ArrowFunctionExpression" ? arrow.node : null;
if (!arrow.error && arrowExpression) return arrowExpression;
@@ -2702,7 +2700,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// If the error is recoverable, we can only re-report it if there is
// a node we can return.
if (jsx && jsx.node) {
if (jsx?.node) {
/*:: invariant(jsx.failState) */
this.state = jsx.failState;
return jsx.node;
@@ -2714,7 +2712,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return arrowExpression;
}
if (jsx && jsx.thrown) throw jsx.error;
if (jsx?.thrown) throw jsx.error;
if (arrow.thrown) throw arrow.error;
/*:: invariant(typeParameters) */

View File

@@ -261,7 +261,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
checkExport(node: N.ExportNamedDeclaration): void {
const { specifiers } = node;
if (specifiers && specifiers.length) {
if (specifiers?.length) {
node.specifiers = specifiers.filter(
node => node.exported.type === "Placeholder",
);

View File

@@ -2340,7 +2340,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}
if (!(jsx && jsx.error) && !this.isRelational("<")) {
if (!jsx?.error && !this.isRelational("<")) {
return super.parseMaybeAssign(...args);
}
@@ -2362,7 +2362,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
// Correct TypeScript code should have at least 1 type parameter, but don't crash on bad code.
if (typeParameters && typeParameters.params.length !== 0) {
if (typeParameters?.params.length !== 0) {
this.resetStartLocationFromNode(expr, typeParameters);
}
expr.typeParameters = typeParameters;
@@ -2384,7 +2384,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (!typeCast.error) return typeCast.node;
}
if (jsx && jsx.node) {
if (jsx?.node) {
/*:: invariant(jsx.failState) */
this.state = jsx.failState;
return jsx.node;
@@ -2396,17 +2396,17 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return arrow.node;
}
if (typeCast && typeCast.node) {
if (typeCast?.node) {
/*:: invariant(typeCast.failState) */
this.state = typeCast.failState;
return typeCast.node;
}
if (jsx && jsx.thrown) throw jsx.error;
if (jsx?.thrown) throw jsx.error;
if (arrow.thrown) throw arrow.error;
if (typeCast && typeCast.thrown) throw typeCast.error;
if (typeCast?.thrown) throw typeCast.error;
throw (jsx && jsx.error) || arrow.error || (typeCast && typeCast.error);
throw jsx?.error || arrow.error || typeCast?.error;
}
// Handle type assertions
@@ -2616,7 +2616,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
): $ReadOnlyArray<?N.Expression> {
for (let i = 0; i < exprList.length; i++) {
const expr = exprList[i];
if (expr && expr.type === "TSTypeCastExpression") {
if (expr?.type === "TSTypeCastExpression") {
this.raise(expr.start, TSErrors.UnexpectedTypeAnnotation);
}
}

View File

@@ -225,7 +225,7 @@ export default class Tokenizer extends LocationParser {
nextToken(): void {
const curContext = this.curContext();
if (!curContext || !curContext.preserveSpace) this.skipSpace();
if (!curContext?.preserveSpace) this.skipSpace();
this.state.octalPositions = [];
this.state.start = this.state.pos;