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

Commit

Permalink
Add fix for object-literal-key-quotes (#1953)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-hanson authored and nchen63 committed Jan 1, 2017
1 parent 7d9f7e7 commit b7741b0
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/rules/objectLiteralKeyQuotesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class ObjectLiteralKeyQuotesWalker extends Lint.RuleWalker {
break;
case "consistent":
if (quotesAreInconsistent(properties)) {
// No fix -- don't know if they would want to add quotes or remove them.
this.addFailureAt(node.getStart(), 1, Rule.INCONSISTENT_PROPERTY);
}
break;
Expand All @@ -120,18 +121,24 @@ class ObjectLiteralKeyQuotesWalker extends Lint.RuleWalker {
private allMustHaveQuotes(properties: ts.ObjectLiteralElementLike[]) {
for (const { name } of properties) {
if (name !== undefined && name.kind !== ts.SyntaxKind.StringLiteral && name.kind !== ts.SyntaxKind.ComputedPropertyName) {
this.addFailureAtNode(name, Rule.UNQUOTED_PROPERTY(name.getText()));
const fix = this.fix(this.appendText(name.getStart(), '"'), this.appendText(name.getEnd(), '"'));
this.addFailureAtNode(name, Rule.UNQUOTED_PROPERTY(name.getText()), fix);
}
}
}

private noneMayHaveQuotes(properties: ts.ObjectLiteralElementLike[], noneNeedQuotes?: boolean) {
for (const { name } of properties) {
if (name !== undefined && name.kind === ts.SyntaxKind.StringLiteral && (noneNeedQuotes || !propertyNeedsQuotes(name.text))) {
this.addFailureAtNode(name, Rule.UNNEEDED_QUOTES(name.text));
const fix = this.fix(this.deleteText(name.getStart(), 1), this.deleteText(name.getEnd() - 1, 1));
this.addFailureAtNode(name, Rule.UNNEEDED_QUOTES(name.text), fix);
}
}
}

private fix(...replacements: Lint.Replacement[]) {
return new Lint.Fix(Rule.metadata.ruleName, replacements);
}
}

function quotesAreInconsistent(properties: ts.ObjectLiteralElementLike[]): boolean {
Expand Down
18 changes: 18 additions & 0 deletions test/rules/object-literal-key-quotes/always/test.js.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const o = {
'hello': 123,
"goodbye": 234, // failure
"quote": 345,
"needs quote": 789,
"hyphens-need-quotes": null,
[computed]: 456,
"123": "hello", // failure
"1e4": "something", // failure
".123": "float", // failure
'123': 'numbers do not need quotes',
'010': 'but this one does.',
'.123': 'as does this one',
"fn"() { return },
"true": 0, // failure
"0x0": 0,
"true": 0,
};
18 changes: 18 additions & 0 deletions test/rules/object-literal-key-quotes/always/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const o = {
'hello': 123,
"goodbye": 234, // failure
"quote": 345,
"needs quote": 789,
"hyphens-need-quotes": null,
[computed]: 456,
"123": "hello", // failure
"1e4": "something", // failure
".123": "float", // failure
'123': 'numbers do not need quotes',
'010': 'but this one does.',
'.123': 'as does this one',
"fn"() { return },
"true": 0, // failure
"0x0": 0,
"true": 0,
};
19 changes: 19 additions & 0 deletions test/rules/object-literal-key-quotes/as-needed/test.js.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const o = {
hello: 123, // failure
goodbye: 234,
quote: 345, // failure
"needs quote": 789,
"hyphens-need-quotes": null,
[computed]: 456,
123: "hello",
1e4: "something",
.123: "float",
123: 'numbers do not need quotes', // failure
'010': 'but this one does.',
'.123': 'as does this one',
fn() { return },
true: 0,
"0x0": 0,
true: 0, // failure
'': 'always quote the empty string',
};
20 changes: 20 additions & 0 deletions test/rules/object-literal-key-quotes/as-needed/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const o = {
hello: 123, // failure
goodbye: 234,
quote: 345, // failure
"needs quote": 789,
"hyphens-need-quotes": null,
[computed]: 456,
123: "hello",
1e4: "something",
.123: "float",
123: 'numbers do not need quotes', // failure
'010': 'but this one does.',
'.123': 'as does this one',
fn() { return },
true: 0,
"0x0": 0,
true: 0, // failure
'': 'always quote the empty string',
...{},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

const o = {
hello: 123,
bye: 45,
};
const v = {
hello: 123,
bye: 45,
};
const s = {
hello: 123,
bye: 45,
};
const r = {
"hello": 123,
"bye-bye": 45,
};
const p = {
hello: 123,
bye: 45,
};
const q = {
"hello": 123,
"bye-bye": 45,
};
const t = {
hello: 123,
bye-bye: 45,
nested: {
bird: 2,
egg: 3,
}
};
const u = {
hello: 123,
bye: 45,
nested: {
bird: 1,
egg: 2,
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

const o = {
hello: 123,
bye: 45,
};
const v = {
hello: 123,
bye: 45,
};
const s = {
hello: 123,
bye: 45,
};
const r = {
"hello": 123,
"bye-bye": 45,
};
const p = {
hello: 123,
bye: 45,
};
const q = {
"hello": 123,
"bye-bye": 45,
};
const t = {
hello: 123,
bye-bye: 45,
nested: {
bird: 2,
egg: 3,
}
};
const u = {
hello: 123,
bye: 45,
nested: {
bird: 1,
egg: 2,
}
};
const v = {
...o,
};

0 comments on commit b7741b0

Please sign in to comment.