Skip to content

Commit

Permalink
feat: add better naming for entity references
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The entity references have changed
to be more descriptive. `Alphanumeric` is now `named` and
`unicode` is now `numeric` as these actually reflect the
name of the references.
  • Loading branch information
ljbc1994 committed Nov 14, 2021
1 parent d6f1a38 commit 5717c81
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 561 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
node-version: 14.7
- name: Cache node modules
uses: actions/cache@v2
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-node@v1
with:
node-version: 12
node-version: 14.17
- name: Cache node modules
uses: actions/cache@v2
env:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Enforce the preferred use of curly quote/apostrophe characters.

**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.

> This plugin supports converting quotes and apostrophes that have been specified using their `alphanumeric` or `unicode` equivalent into their curly `alphanumeric` or `unicode` values.
> This plugin supports converting quotes and apostrophes that have been specified using their `numeric` or `named` equivalent into their curly `named` or `numeric` values.
## Installation

Expand Down Expand Up @@ -44,12 +44,12 @@ To configure the plugin rules:
}
```

### Convert alphanumeric values into their curly unicode equivalent
### Convert named values into their curly numeric equivalent

```json
{
"rules": {
"prefer-smart-quotes/prefer": ["error", { "inputFormat": "alphanumeric", "outputFormat": "unicode" }]
"prefer-smart-quotes/prefer": ["error", { "inputFormat": "named", "outputFormat": "numeric" }]
}
}
```
Expand Down
14 changes: 7 additions & 7 deletions docs/rules/prefer.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Rule Details

This rule enforces the preferred use of curly quotes/apostrophe characters. To ensure consistency, the plugin can also convert quotes
and apostrophes that have been specified using their `alphanumeric` or `unicode` equivalent into curly `alphanumeric` or `unicode`
and apostrophes that have been specified using their `named` or `numeric` equivalent into curly `numeric` or `named`
values.

## Options
Expand All @@ -14,16 +14,16 @@ This rule has two options, a string option and an object option.

**String option:**
- `"character"` (default) requires the use of curly characters over straight characters (`'`, `"`)
- `"alphanumeric"` requires the use of curly alphanumeric values over straight alphanumeric values (`'`, `"`)
- `"unicode"` requires the use of curly unicode values over straight unicode values (`'`, `"`)
- `"named"` requires the use of curly named values over straight named values (`'`, `"`)
- `"numeric"` requires the use of curly numeric values over straight numeric values (`'`, `"`)
- `"all"` requires the use of all curly entities over straight entities

**Object option:**

- `"inputFormat": "character"` requires the use of curly characters over straight characters
- "all", "character", "alphanumeric", "unicode"
- "all", "character", "named", "numeric"
- `"outputFormat": "all"` specify the output entity format
- "all", "character", "alphanumeric", "unicode"
- "all", "character", "named", "numeric"

## Examples

Expand All @@ -46,7 +46,7 @@ const Component = () => <>That&rsquo;s a nice boulder!<>
Examples of **inline** usage for this rule:
```jsx
/*eslint smart-quotes: ["error", "alphanumeric"]*/
/*eslint smart-quotes: ["error", "named"]*/
/*eslint smart-quotes: ["error", { outputFormat: "unicode" }]*/
/*eslint smart-quotes: ["error", { outputFormat: "numeric" }]*/
```
76 changes: 38 additions & 38 deletions lib/rules/prefer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,45 @@ const matchAll = require("string.prototype.matchall");
const MAPPINGS = {
singleQuote: {
character: "'",
unicode: "&#39;",
alphanumeric: "&apos;",
numeric: "&#39;",
named: "&apos;",
requires: ["curlyLeftQuote", "curlyRightQuote"],
},
doubleQuote: {
character: '"',
unicode: "&#34;",
alphanumeric: "&quot;",
numeric: "&#34;",
named: "&quot;",
requires: ["curlyLeftDoubleQuote", "curlyRightDoubleQuote"],
},
curlyLeftQuote: {
character: "‘",
unicode: "&#8216;",
alphanumeric: "&lsquo;",
numeric: "&#8216;",
named: "&lsquo;",
},
curlyRightQuote: {
character: "’",
unicode: "&#8217;",
alphanumeric: "&rsquo;",
numeric: "&#8217;",
named: "&rsquo;",
},
curlyLeftDoubleQuote: {
character: "“",
unicode: "&#8220;",
alphanumeric: "&ldquo;",
numeric: "&#8220;",
named: "&ldquo;",
},
curlyRightDoubleQuote: {
character: "”",
unicode: "&#8221;",
alphanumeric: "&rdquo;",
numeric: "&#8221;",
named: "&rdquo;",
},
};

const ENTITY_MAPPINGS = {
character: [MAPPINGS.singleQuote.character, MAPPINGS.doubleQuote.character],
alphanumeric: [
MAPPINGS.singleQuote.alphanumeric,
MAPPINGS.doubleQuote.alphanumeric,
named: [
MAPPINGS.singleQuote.named,
MAPPINGS.doubleQuote.named,
],
unicode: [MAPPINGS.singleQuote.unicode, MAPPINGS.doubleQuote.unicode],
numeric: [MAPPINGS.singleQuote.numeric, MAPPINGS.doubleQuote.numeric],
};

const DEFAULT_OPTIONS = {
Expand All @@ -75,16 +75,16 @@ module.exports = {
{
anyOf: [
{
enum: ["all", "character", "alphanumeric", "unicode"],
enum: ["all", "character", "named", "numeric"],
},
{
type: "object",
properties: {
inputFormat: {
enum: ["all", "character", "alphanumeric", "unicode"],
enum: ["all", "character", "named", "numeric"],
},
outputFormat: {
enum: ["all", "character", "alphanumeric", "unicode"],
enum: ["all", "character", "named", "numeric"],
},
},
},
Expand Down Expand Up @@ -118,10 +118,10 @@ module.exports = {
pluginOptions.inputFormat === "character"
? ENTITY_MAPPINGS.character
: null,
pluginOptions.inputFormat === "alphanumeric"
? ENTITY_MAPPINGS.alphanumeric
pluginOptions.inputFormat === "named"
? ENTITY_MAPPINGS.named
: null,
pluginOptions.inputFormat === "unicode" ? ENTITY_MAPPINGS.unicode : null,
pluginOptions.inputFormat === "numeric" ? ENTITY_MAPPINGS.numeric : null,
]
.filter(Boolean)
.reduce((all, val) => all.concat(val), [])
Expand All @@ -144,8 +144,8 @@ module.exports = {
return (
[
MAPPINGS.singleQuote.character,
MAPPINGS.singleQuote.unicode,
MAPPINGS.singleQuote.alphanumeric,
MAPPINGS.singleQuote.numeric,
MAPPINGS.singleQuote.named,
].indexOf(value) !== -1
);
}
Expand All @@ -160,8 +160,8 @@ module.exports = {
return (
[
MAPPINGS.doubleQuote.character,
MAPPINGS.doubleQuote.unicode,
MAPPINGS.doubleQuote.alphanumeric,
MAPPINGS.doubleQuote.numeric,
MAPPINGS.doubleQuote.named,
].indexOf(value) !== -1
);
}
Expand Down Expand Up @@ -198,17 +198,17 @@ module.exports = {
*/
function getValueEntity(value, char) {
if (value.charAt(0) === "&") {
const isUnicode = ENTITY_MAPPINGS.unicode.indexOf(value) !== -1;
const isAlphanumeric =
ENTITY_MAPPINGS.alphanumeric.indexOf(value) !== -1;
const isNumeric = ENTITY_MAPPINGS.numeric.indexOf(value) !== -1;
const isNamed =
ENTITY_MAPPINGS.named.indexOf(value) !== -1;

if (isUnicode || isAlphanumeric) {
if (isNumeric || isNamed) {
return {
value: value,
type: isUnicode
? "unicode"
: isAlphanumeric
? "alphanumeric"
type: isNumeric
? "numeric"
: isNamed
? "named"
: null,
};
}
Expand Down Expand Up @@ -287,14 +287,14 @@ module.exports = {
isQuote = true;
}
if (
pluginOptions.inputFormat === "unicode" &&
value === MAPPINGS.singleQuote.unicode
pluginOptions.inputFormat === "numeric" &&
value === MAPPINGS.singleQuote.numeric
) {
isQuote = true;
}
if (
pluginOptions.inputFormat === "alphanumeric" &&
value === MAPPINGS.singleQuote.alphanumeric
pluginOptions.inputFormat === "named" &&
value === MAPPINGS.singleQuote.named
) {
isQuote = true;
}
Expand Down
Loading

0 comments on commit 5717c81

Please sign in to comment.