Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added rule to enforce quotes type (#475)
* feat: new quotes rule * chore: change error messaging to specifying string literals Co-authored-by: Angelica Bocanegra <angelica.bocanegra@tabcorp.com.au>
- Loading branch information
1 parent
998eb5a
commit 8008f78
Showing
7 changed files
with
278 additions
and
13 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,12 @@ | ||
### `quotes` | ||
|
||
Enforces single quotes or double quotes around string literals. | ||
|
||
#### Options | ||
|
||
The rule has string options of: | ||
|
||
* `"double"` (default) requires double quotes around string literals. | ||
* `"single"` requires single quotes around string literals. | ||
|
||
<!-- assertions quotes --> |
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,51 @@ | ||
const schema = [ | ||
{ | ||
enum: ['double', 'single'], | ||
type: 'string', | ||
}, | ||
]; | ||
|
||
const meta = { | ||
fixable: 'code', | ||
}; | ||
|
||
const create = (context) => { | ||
const double = (context.options[0] || 'double') === 'double'; | ||
const sourceCode = context.getSourceCode(); | ||
|
||
return { | ||
StringLiteralTypeAnnotation (node) { | ||
if (double && sourceCode.text[node.range[0]] !== '"') { | ||
// double | ||
context.report({ | ||
fix: (fixer) => { | ||
return [ | ||
fixer.replaceTextRange([node.range[0], node.range[0] + 1], '"'), | ||
fixer.replaceTextRange([node.range[1] - 1, node.range[1]], '"'), | ||
]; | ||
}, | ||
message: 'String literals must use double quote.', | ||
node, | ||
}); | ||
} else if (!double && sourceCode.text[node.range[0]] !== '\'') { | ||
// single | ||
context.report({ | ||
fix: (fixer) => { | ||
return [ | ||
fixer.replaceTextRange([node.range[0], node.range[0] + 1], '\''), | ||
fixer.replaceTextRange([node.range[1] - 1, node.range[1]], '\''), | ||
]; | ||
}, | ||
message: 'String literals must use single quote.', | ||
node, | ||
}); | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
export default { | ||
create, | ||
meta, | ||
schema, | ||
}; |
Oops, something went wrong.