Enable no-case-declarations to prevent bugs and remove if (true)

This commit is contained in:
Daniel Tschinder 2017-08-19 10:39:49 +02:00 committed by Henry Zhu
parent dfb279f478
commit aec1bdb359
5 changed files with 36 additions and 27 deletions

View File

@ -5,7 +5,8 @@
"prettier" "prettier"
], ],
"rules": { "rules": {
"prettier/prettier": ["error", { "trailingComma": "all" }] "prettier/prettier": ["error", { "trailingComma": "all" }],
"no-case-declarations": "error"
}, },
"env": { "env": {
"node": true "node": true

View File

@ -676,7 +676,7 @@ export default class ExpressionParser extends LValParser {
case tt._yield: case tt._yield:
if (this.state.inGenerator) this.unexpected(); if (this.state.inGenerator) this.unexpected();
case tt.name: case tt.name: {
node = this.startNode(); node = this.startNode();
const allowAwait = this.state.value === "await" && this.state.inAsync; const allowAwait = this.state.value === "await" && this.state.inAsync;
const allowYield = this.shouldAllowYieldIdentifier(); const allowYield = this.shouldAllowYieldIdentifier();
@ -705,29 +705,29 @@ export default class ExpressionParser extends LValParser {
} }
return id; return id;
}
case tt._do: case tt._do: {
// TODO this.expectPlugin("doExpressions");
if (true) { const node = this.startNode();
this.expectPlugin("doExpressions"); this.next();
const node = this.startNode(); const oldInFunction = this.state.inFunction;
this.next(); const oldLabels = this.state.labels;
const oldInFunction = this.state.inFunction; this.state.labels = [];
const oldLabels = this.state.labels; this.state.inFunction = false;
this.state.labels = []; node.body = this.parseBlock(false);
this.state.inFunction = false; this.state.inFunction = oldInFunction;
node.body = this.parseBlock(false); this.state.labels = oldLabels;
this.state.inFunction = oldInFunction; return this.finishNode(node, "DoExpression");
this.state.labels = oldLabels; }
return this.finishNode(node, "DoExpression");
}
case tt.regexp: case tt.regexp: {
const value = this.state.value; const value = this.state.value;
node = this.parseLiteral(value.value, "RegExpLiteral"); node = this.parseLiteral(value.value, "RegExpLiteral");
node.pattern = value.pattern; node.pattern = value.pattern;
node.flags = value.flags; node.flags = value.flags;
return node; return node;
}
case tt.num: case tt.num:
return this.parseLiteral(this.state.value, "NumericLiteral"); return this.parseLiteral(this.state.value, "NumericLiteral");
@ -781,7 +781,7 @@ export default class ExpressionParser extends LValParser {
case tt.backQuote: case tt.backQuote:
return this.parseTemplate(false); return this.parseTemplate(false);
case tt.doubleColon: case tt.doubleColon: {
node = this.startNode(); node = this.startNode();
this.next(); this.next();
node.object = null; node.object = null;
@ -794,6 +794,7 @@ export default class ExpressionParser extends LValParser {
"Binding should be performed on object property.", "Binding should be performed on object property.",
); );
} }
}
default: default:
throw this.unexpected(); throw this.unexpected();

View File

@ -69,13 +69,14 @@ export default class LValParser extends NodeUtils {
this.toAssignable(node.value, isBinding, contextDescription); this.toAssignable(node.value, isBinding, contextDescription);
break; break;
case "SpreadElement": case "SpreadElement": {
this.checkToRestConversion(node); this.checkToRestConversion(node);
node.type = "RestElement"; node.type = "RestElement";
const arg = node.argument; const arg = node.argument;
this.toAssignable(arg, isBinding, contextDescription); this.toAssignable(arg, isBinding, contextDescription);
break; break;
}
case "ArrayExpression": case "ArrayExpression":
node.type = "ArrayPattern"; node.type = "ArrayPattern";
@ -211,11 +212,12 @@ export default class LValParser extends NodeUtils {
case tt.name: case tt.name:
return this.parseBindingIdentifier(); return this.parseBindingIdentifier();
case tt.bracketL: case tt.bracketL: {
const node = this.startNode(); const node = this.startNode();
this.next(); this.next();
node.elements = this.parseBindingList(tt.bracketR, true); node.elements = this.parseBindingList(tt.bracketR, true);
return this.finishNode(node, "ArrayPattern"); return this.finishNode(node, "ArrayPattern");
}
case tt.braceL: case tt.braceL:
return this.parseObj(true); return this.parseObj(true);

View File

@ -540,7 +540,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
switch (this.state.type) { switch (this.state.type) {
case tt.name: case tt.name:
case tt._void: case tt._void:
case tt._null: case tt._null: {
const type = this.match(tt._void) const type = this.match(tt._void)
? "TSVoidKeyword" ? "TSVoidKeyword"
: this.match(tt._null) : this.match(tt._null)
@ -552,6 +552,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, type); return this.finishNode(node, type);
} }
return this.tsParseTypeReference(); return this.tsParseTypeReference();
}
case tt.string: case tt.string:
case tt.num: case tt.num:
case tt._true: case tt._true:
@ -573,13 +574,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "TSLiteralType"); return this.finishNode(node, "TSLiteralType");
} }
break; break;
case tt._this: case tt._this: {
const thisKeyword = this.tsParseThisTypeNode(); const thisKeyword = this.tsParseThisTypeNode();
if (this.isContextual("is") && !this.hasPrecedingLineBreak()) { if (this.isContextual("is") && !this.hasPrecedingLineBreak()) {
return this.tsParseThisTypePredicate(thisKeyword); return this.tsParseThisTypePredicate(thisKeyword);
} else { } else {
return thisKeyword; return thisKeyword;
} }
}
case tt._typeof: case tt._typeof:
return this.tsParseTypeQuery(); return this.tsParseTypeQuery();
case tt.braceL: case tt.braceL:
@ -1038,13 +1040,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
case tt._var: case tt._var:
case tt._let: case tt._let:
return this.parseVarStatement(nany, this.state.type); return this.parseVarStatement(nany, this.state.type);
case tt.name: case tt.name: {
const value = this.state.value; const value = this.state.value;
if (value === "global") { if (value === "global") {
return this.tsParseAmbientExternalModuleDeclaration(nany); return this.tsParseAmbientExternalModuleDeclaration(nany);
} else { } else {
return this.tsParseDeclaration(nany, value, /* next */ true); return this.tsParseDeclaration(nany, value, /* next */ true);
} }
}
} }
} }
@ -1064,14 +1067,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
tsParseExpressionStatement(node: any, expr: N.Identifier): ?N.Declaration { tsParseExpressionStatement(node: any, expr: N.Identifier): ?N.Declaration {
switch (expr.name) { switch (expr.name) {
case "declare": case "declare": {
const declaration = this.tsTryParseDeclare(node); const declaration = this.tsTryParseDeclare(node);
if (declaration) { if (declaration) {
declaration.declare = true; declaration.declare = true;
return declaration; return declaration;
} }
break; break;
}
case "global": case "global":
// `global { }` (with no `declare`) may appear inside an ambient module declaration. // `global { }` (with no `declare`) may appear inside an ambient module declaration.
// Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global". // Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global".

View File

@ -595,11 +595,13 @@ export default class Tokenizer extends LocationParser {
++this.state.pos; ++this.state.pos;
return this.finishToken(tt.backQuote); return this.finishToken(tt.backQuote);
case 48: // '0' case 48: {
// '0'
const next = this.input.charCodeAt(this.state.pos + 1); const next = this.input.charCodeAt(this.state.pos + 1);
if (next === 120 || next === 88) return this.readRadixNumber(16); // '0x', '0X' - hex number if (next === 120 || next === 88) return this.readRadixNumber(16); // '0x', '0X' - hex number
if (next === 111 || next === 79) return this.readRadixNumber(8); // '0o', '0O' - octal number if (next === 111 || next === 79) return this.readRadixNumber(8); // '0o', '0O' - octal number
if (next === 98 || next === 66) return this.readRadixNumber(2); // '0b', '0B' - binary number if (next === 98 || next === 66) return this.readRadixNumber(2); // '0b', '0B' - binary number
}
// Anything else beginning with a digit is an integer, octal // Anything else beginning with a digit is an integer, octal
// number, or float. // number, or float.
case 49: case 49: