Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Fix #4514: [quotemark: backtick] Incorrectly flags string literals that must use single/double quotes #4535

Merged
merged 3 commits into from Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/rules/quotemarkRule.ts
Expand Up @@ -14,8 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { isNoSubstitutionTemplateLiteral, isSameLine, isStringLiteral } from "tsutils";
import {
isImportDeclaration,
isNoSubstitutionTemplateLiteral,
isPropertyAssignment,
isSameLine,
isStringLiteral,
} from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";
Expand Down Expand Up @@ -122,6 +127,14 @@ function walk(ctx: Lint.WalkContext<Options>) {
: options.quotemark;
const actualQuotemark = sourceFile.text[node.end - 1];

// Don't use backticks instead of single/double quotes when it breaks TypeScript syntax.
if (
expectedQuotemark === "`" &&
(isImportDeclaration(node.parent) || isPropertyAssignment(node.parent))
) {
return;
}

if (actualQuotemark === expectedQuotemark) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions test/rules/quotemark/backtick/test.ts.fix
@@ -1,3 +1,5 @@
import { Something } from "some-package"

var single = `single`;
var double = `married`;
var singleWithinDouble = `'singleWithinDouble'`;
Expand All @@ -7,3 +9,5 @@ var tabNewlineWithinSingle = `tab\tNewline\nWithinSingle`;

// "avoid-template" option is not set.
`foo`;

const object = { "kebab-case": 3 }
4 changes: 4 additions & 0 deletions test/rules/quotemark/backtick/test.ts.lint
@@ -1,3 +1,5 @@
import { Something } from "some-package"

var single = 'single';
~~~~~~~~ [' should be `]
var double = "married";
Expand All @@ -13,3 +15,5 @@ var tabNewlineWithinSingle = 'tab\tNewline\nWithinSingle';

// "avoid-template" option is not set.
`foo`;

const object = { "kebab-case": 3 }