-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add quotes [extension] (#762)
- Loading branch information
1 parent
656d255
commit 9f82099
Showing
8 changed files
with
772 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.