Skip to content

Commit

Permalink
feat(eslint-plugin): add quotes [extension] (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk authored and JamesHenry committed Aug 19, 2019
1 parent 656d255 commit 9f82099
Show file tree
Hide file tree
Showing 8 changed files with 772 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
| [`@typescript-eslint/prefer-regexp-exec`](./docs/rules/prefer-regexp-exec.md) | Prefer RegExp#exec() over String#match() if no global flag is provided | :heavy_check_mark: | | :thought_balloon: |
| [`@typescript-eslint/prefer-string-starts-ends-with`](./docs/rules/prefer-string-starts-ends-with.md) | Enforce the use of `String#startsWith` and `String#endsWith` instead of other equivalent methods of checking substrings | :heavy_check_mark: | :wrench: | :thought_balloon: |
| [`@typescript-eslint/promise-function-async`](./docs/rules/promise-function-async.md) | Requires any function or method that returns a Promise to be marked async | | | :thought_balloon: |
| [`@typescript-eslint/quotes`](./docs/rules/quotes.md) | Enforce the consistent use of either backticks, double, or single quotes | | :wrench: | |
| [`@typescript-eslint/require-array-sort-compare`](./docs/rules/require-array-sort-compare.md) | Enforce giving `compare` argument to `Array#sort` | | | :thought_balloon: |
| [`@typescript-eslint/require-await`](./docs/rules/require-await.md) | Disallow async functions which have no `await` expression | :heavy_check_mark: | | :thought_balloon: |
| [`@typescript-eslint/restrict-plus-operands`](./docs/rules/restrict-plus-operands.md) | When adding two variables, operands must both be of type number or of type string | | | :thought_balloon: |
Expand Down
22 changes: 22 additions & 0 deletions packages/eslint-plugin/docs/rules/quotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Enforce the consistent use of either backticks, double, or single quotes

## Rule Details

This rule extends the base [eslint/quotes](https://eslint.org/docs/rules/quotes) rule.
It supports all options and features of the base rule.

## How to use

```cjson
{
// note you must disable the base rule as it can report incorrect errors
"quotes": "off",
"@typescript-eslint/quotes": ["error"]
}
```

## Options

See [eslint/quotes options](https://eslint.org/docs/rules/quotes#options).

<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/quotes.md)</sup>
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/configs/all.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
"@typescript-eslint/prefer-regexp-exec": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/promise-function-async": "error",
"quotes": "off",
"@typescript-eslint/quotes": "error",
"@typescript-eslint/require-array-sort-compare": "error",
"require-await": "off",
"@typescript-eslint/require-await": "error",
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import preferReadonly from './prefer-readonly';
import preferRegexpExec from './prefer-regexp-exec';
import preferStringStartsEndsWith from './prefer-string-starts-ends-with';
import promiseFunctionAsync from './promise-function-async';
import quotes from './quotes';
import requireArraySortCompare from './require-array-sort-compare';
import requireAwait from './require-await';
import restrictPlusOperands from './restrict-plus-operands';
Expand Down Expand Up @@ -112,6 +113,7 @@ export default {
'prefer-regexp-exec': preferRegexpExec,
'prefer-string-starts-ends-with': preferStringStartsEndsWith,
'promise-function-async': promiseFunctionAsync,
quotes: quotes,
'require-array-sort-compare': requireArraySortCompare,
'require-await': requireAwait,
'restrict-plus-operands': restrictPlusOperands,
Expand Down
62 changes: 62 additions & 0 deletions packages/eslint-plugin/src/rules/quotes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import baseRule from 'eslint/lib/rules/quotes';
import * as util from '../util';

export type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
export type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;

export default util.createRule<Options, MessageIds>({
name: 'quotes',
meta: {
type: 'layout',
docs: {
description:
'Enforce the consistent use of either backticks, double, or single quotes',
category: 'Stylistic Issues',
recommended: false,
},
fixable: 'code',
messages: baseRule.meta.messages,
schema: baseRule.meta.schema,
},
defaultOptions: [
'double',
{
allowTemplateLiterals: false,
avoidEscape: false,
},
],
create(context, [option]) {
const rules = baseRule.create(context);

const isModuleDeclaration = (node: TSESTree.Literal): boolean => {
return (
!!node.parent && node.parent.type === AST_NODE_TYPES.TSModuleDeclaration
);
};

const isTypeLiteral = (node: TSESTree.Literal): boolean => {
return !!node.parent && node.parent.type === AST_NODE_TYPES.TSLiteralType;
};

return {
Literal(node) {
if (
option === 'backtick' &&
(isModuleDeclaration(node) || isTypeLiteral(node))
) {
return;
}

rules.Literal(node);
},

TemplateLiteral(node) {
rules.TemplateLiteral(node);
},
};
},
});
Loading

0 comments on commit 9f82099

Please sign in to comment.