From f4ced99ca9d48ebb35958c34dd7566fdc3c5f420 Mon Sep 17 00:00:00 2001 From: Simon Kotwicz Date: Thu, 25 Jun 2020 16:02:44 -0400 Subject: [PATCH 1/2] Fix babel/semi for private properties --- .gitignore | 1 + rules/semi.js | 49 ++++++++++++++++++++++++++------------------- tests/rules/semi.js | 4 ++++ 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 3c3629e..d5f19d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +package-lock.json diff --git a/rules/semi.js b/rules/semi.js index 2d2fcc4..7d6c472 100644 --- a/rules/semi.js +++ b/rules/semi.js @@ -67,31 +67,38 @@ const report = (context, node, missing) => { }); }; +const checkForSemicolon = (node, context) => { + const options = context.options[1]; + const exceptOneLine = options && options.omitLastInOneLineBlock === true; + + const sourceCode = context.getSourceCode(); + const lastToken = sourceCode.getLastToken(node); + + if (context.options[0] === "never") { + if (isUnnecessarySemicolon(context, lastToken)) { + report(context, node, true); + } + } else { + if (!isSemicolon(lastToken)) { + if (!exceptOneLine || !isOneLinerBlock(context, node)) { + report(context, node); + } + } else { + if (exceptOneLine && isOneLinerBlock(context, node)) { + report(context, node, true); + } + } + } +} + const semiRuleWithClassProperty = ruleComposer.joinReports([ semiRule, context => ({ ClassProperty(node) { - const options = context.options[1]; - const exceptOneLine = options && options.omitLastInOneLineBlock === true; - - const sourceCode = context.getSourceCode(); - const lastToken = sourceCode.getLastToken(node); - - if (context.options[0] === "never") { - if (isUnnecessarySemicolon(context, lastToken)) { - report(context, node, true); - } - } else { - if (!isSemicolon(lastToken)) { - if (!exceptOneLine || !isOneLinerBlock(context, node)) { - report(context, node); - } - } else { - if (exceptOneLine && isOneLinerBlock(context, node)) { - report(context, node, true); - } - } - } + checkForSemicolon(node, context); + }, + ClassPrivateProperty(node) { + checkForSemicolon(node, context); }, }), ]); diff --git a/tests/rules/semi.js b/tests/rules/semi.js index 15ebea0..0aed490 100644 --- a/tests/rules/semi.js +++ b/tests/rules/semi.js @@ -101,12 +101,14 @@ ruleTester.run("semi", rule, { // babel "class Foo { bar = 'example'; }", + "class Foo { #bar = 'example'; }", "class Foo { static bar = 'example'; }", { code: "async function foo() { for await (let thing of {}) { console.log(thing); } }", parserOptions: { ecmaVersion: 6 } }, { code: "class Foo { bar = () => {}; }", options: ["always", { omitLastInOneLineBlock: true }] }, // babel, "never" { code: "class Foo { bar = 'example' }", options: ["never"] }, + { code: "class Foo { #bar = 'example' }", options: ["never"] }, { code: "class Foo { static bar = 'example' }", options: ["never"] }, { code: "class Foo { bar = () => {} }", options: ["never"] }, ], @@ -185,6 +187,7 @@ ruleTester.run("semi", rule, { // babel { code: "class Foo { bar = 'example' }", errors: [{ message: "Missing semicolon." }] }, + { code: "class Foo { #bar = 'example' }", errors: [{ message: "Missing semicolon." }] }, { code: "class Foo { static bar = 'example' }", errors: [{ message: "Missing semicolon." }] }, { code: "class Foo { bar = () => {} }", @@ -194,6 +197,7 @@ ruleTester.run("semi", rule, { // babel, "never" { code: "class Foo { bar = 'example'; }", options: ["never"], errors: [{ message: "Extra semicolon." }] }, + { code: "class Foo { #bar = 'example'; }", options: ["never"], errors: [{ message: "Extra semicolon." }] }, { code: "class Foo { static bar = 'example'; }", options: ["never"], errors: [{ message: "Extra semicolon." }] }, { code: "class Foo { bar = () => {}; }", From 7c96021279f6f7ae85d0283e6b531eed6abd00c2 Mon Sep 17 00:00:00 2001 From: Simon Kotwicz Date: Sat, 27 Jun 2020 09:29:16 -0400 Subject: [PATCH 2/2] Combine selectors --- .gitignore | 1 - rules/semi.js | 51 ++++++++++++++++++++++----------------------------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index d5f19d8..3c3629e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ node_modules -package-lock.json diff --git a/rules/semi.js b/rules/semi.js index 7d6c472..4813dfa 100644 --- a/rules/semi.js +++ b/rules/semi.js @@ -67,38 +67,31 @@ const report = (context, node, missing) => { }); }; -const checkForSemicolon = (node, context) => { - const options = context.options[1]; - const exceptOneLine = options && options.omitLastInOneLineBlock === true; - - const sourceCode = context.getSourceCode(); - const lastToken = sourceCode.getLastToken(node); - - if (context.options[0] === "never") { - if (isUnnecessarySemicolon(context, lastToken)) { - report(context, node, true); - } - } else { - if (!isSemicolon(lastToken)) { - if (!exceptOneLine || !isOneLinerBlock(context, node)) { - report(context, node); - } - } else { - if (exceptOneLine && isOneLinerBlock(context, node)) { - report(context, node, true); - } - } - } -} - const semiRuleWithClassProperty = ruleComposer.joinReports([ semiRule, context => ({ - ClassProperty(node) { - checkForSemicolon(node, context); - }, - ClassPrivateProperty(node) { - checkForSemicolon(node, context); + "ClassProperty, ClassPrivateProperty"(node) { + const options = context.options[1]; + const exceptOneLine = options && options.omitLastInOneLineBlock === true; + + const sourceCode = context.getSourceCode(); + const lastToken = sourceCode.getLastToken(node); + + if (context.options[0] === "never") { + if (isUnnecessarySemicolon(context, lastToken)) { + report(context, node, true); + } + } else { + if (!isSemicolon(lastToken)) { + if (!exceptOneLine || !isOneLinerBlock(context, node)) { + report(context, node); + } + } else { + if (exceptOneLine && isOneLinerBlock(context, node)) { + report(context, node, true); + } + } + } }, }), ]);