Skip to content

Commit

Permalink
Revert changes in quotes rule
Browse files Browse the repository at this point in the history
  • Loading branch information
fasttime committed May 19, 2023
1 parent aa16e4c commit 9d94ba4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 72 deletions.
28 changes: 14 additions & 14 deletions lib/rules/quotes.js
Expand Up @@ -157,8 +157,7 @@ module.exports = {

/**
* Checks whether or not a given node is a directive.
* The directive is a `ExpressionStatement` which has only a string literal not surrounded by
* parentheses.
* The directive is a `ExpressionStatement` which has only a string literal.
* @param {ASTNode} node A node to check.
* @returns {boolean} Whether or not the node is a directive.
* @private
Expand All @@ -167,23 +166,23 @@ module.exports = {
return (
node.type === "ExpressionStatement" &&
node.expression.type === "Literal" &&
typeof node.expression.value === "string" &&
!astUtils.isParenthesised(sourceCode, node.expression)
typeof node.expression.value === "string"
);
}

/**
* Checks whether a specified node is either part of, or immediately follows a (possibly empty) directive prologue.
* @see {@link http://www.ecma-international.org/ecma-262/6.0/#sec-directive-prologues-and-the-use-strict-directive}
* Checks whether or not a given node is a part of directive prologues.
* See also: http://www.ecma-international.org/ecma-262/6.0/#sec-directive-prologues-and-the-use-strict-directive
* @param {ASTNode} node A node to check.
* @returns {boolean} Whether a specified node is either part of, or immediately follows a (possibly empty) directive prologue.
* @returns {boolean} Whether or not the node is a part of directive prologues.
* @private
*/
function isExpressionInOrJustAfterDirectivePrologue(node) {
if (!astUtils.isTopLevelExpressionStatement(node.parent)) {
function isPartOfDirectivePrologue(node) {
const block = node.parent.parent;

if (block.type !== "Program" && (block.type !== "BlockStatement" || !astUtils.isFunction(block.parent))) {
return false;
}
const block = node.parent.parent;

// Check the node is at a prologue.
for (let i = 0; i < block.body.length; ++i) {
Expand Down Expand Up @@ -213,7 +212,7 @@ module.exports = {

// Directive Prologues.
case "ExpressionStatement":
return !astUtils.isParenthesised(sourceCode, node) && isExpressionInOrJustAfterDirectivePrologue(node);
return isPartOfDirectivePrologue(node);

// LiteralPropertyName.
case "Property":
Expand Down Expand Up @@ -329,11 +328,12 @@ module.exports = {
description: settings.description
},
fix(fixer) {
if (astUtils.isTopLevelExpressionStatement(node.parent) && !astUtils.isParenthesised(sourceCode, node)) {
if (isPartOfDirectivePrologue(node)) {

/*
* TemplateLiterals aren't actually directives, but fixing them might turn
* them into directives and change the behavior of the code.
* TemplateLiterals in a directive prologue aren't actually directives, but if they're
* in the directive prologue, then fixing them might turn them into directives and change
* the behavior of the code.
*/
return null;
}
Expand Down
60 changes: 2 additions & 58 deletions tests/lib/rules/quotes.js
Expand Up @@ -440,7 +440,7 @@ ruleTester.run("quotes", rule, {
},
{
code: "() => { foo(); `use strict`; }",
output: null, // no autofix
output: "() => { foo(); \"use strict\"; }",
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "wrongQuotes",
Expand All @@ -450,7 +450,7 @@ ruleTester.run("quotes", rule, {
},
{
code: "foo(); `use strict`;",
output: null, // no autofix
output: "foo(); \"use strict\";",
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "wrongQuotes",
Expand Down Expand Up @@ -725,62 +725,6 @@ ruleTester.run("quotes", rule, {
type: "Literal"
}
]
},

// https://github.com/eslint/eslint/pull/17022
{
code: "() => { foo(); (`use strict`); }",
output: "() => { foo(); (\"use strict\"); }",
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "wrongQuotes",
data: { description: "doublequote" },
type: "TemplateLiteral"
}]
},
{
code: "('foo'); \"bar\";",
output: "(`foo`); `bar`;",
options: ["backtick"],
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "wrongQuotes",
data: { description: "backtick" },
type: "Literal"
}, {
messageId: "wrongQuotes",
data: { description: "backtick" },
type: "Literal"
}]
},
{
code: "; 'use asm';",
output: "; \"use asm\";",
errors: [{
messageId: "wrongQuotes",
data: { description: "doublequote" },
type: "Literal"
}]
},
{
code: "{ `foobar`; }",
output: "{ \"foobar\"; }",
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "wrongQuotes",
data: { description: "doublequote" },
type: "TemplateLiteral"
}]
},
{
code: "foo(() => `bar`);",
output: "foo(() => \"bar\");",
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "wrongQuotes",
data: { description: "doublequote" },
type: "TemplateLiteral"
}]
}
]
});

0 comments on commit 9d94ba4

Please sign in to comment.