Skip to content

Commit

Permalink
Merge pull request #138 from gajus/variance
Browse files Browse the repository at this point in the history
feat: Add variance support to spaceBeforeTypeColon & spaceAfterTypeColon
  • Loading branch information
danharper authored Oct 22, 2016
2 parents 9d5be73 + 4a7f87f commit 715a389
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 11 deletions.
19 changes: 15 additions & 4 deletions src/rules/spaceAfterTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,22 @@ const objectTypePropertyEvaluator = (context) => {
const sourceCode = context.getSourceCode();

const getColon = (objectTypeProperty) => {
if (objectTypeProperty.optional || objectTypeProperty.static) {
return sourceCode.getFirstToken(objectTypeProperty, 2);
} else {
return sourceCode.getFirstToken(objectTypeProperty, 1);
let tokenIndex = 1; // eslint-disable-line init-declarations

if (objectTypeProperty.optional) {
tokenIndex++;
}

if (objectTypeProperty.static) {
tokenIndex++;
}

if (objectTypeProperty.variance) {
tokenIndex++;
}


return sourceCode.getFirstToken(objectTypeProperty, tokenIndex);
};

return (objectTypeProperty) => {
Expand Down
23 changes: 17 additions & 6 deletions src/rules/spaceBeforeTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,28 @@ const objectTypePropertyEvaluator = (context) => {
const sourceCode = context.getSourceCode();

const getFirstTokens = (objectTypeProperty) => {
const tokens = sourceCode.getFirstTokens(objectTypeProperty, 3);
const tokens = sourceCode.getFirstTokens(objectTypeProperty, 4);

if (objectTypeProperty.optional || objectTypeProperty.static) {
return [tokens[1], tokens[2]];
} else {
return [tokens[0], tokens[1]];
let tokenIndex = 0; // eslint-disable-line init-declarations

if (objectTypeProperty.optional) {
tokenIndex++;
}

if (objectTypeProperty.static) {
tokenIndex++;
}

if (objectTypeProperty.variance) {
tokenIndex++;
}


return [tokens[tokenIndex], tokens[tokenIndex + 1]];
};

return (objectTypeProperty) => {
// tokenBeforeColon can be identifier, or a ? token if is optional
// tokenBeforeColon can be identifier, or a ? token if is optional
const [tokenBeforeColon, colon] = getFirstTokens(objectTypeProperty);
const spaces = colon.start - tokenBeforeColon.end;

Expand Down
12 changes: 11 additions & 1 deletion src/utilities/getParameterName.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ export default (identifierNode, context) => {
}

if (identifierNode.type === 'ObjectTypeProperty') {
return context.getSourceCode().getFirstToken(identifierNode, identifierNode.static ? 1 : 0).value;
let tokenIndex = 0; // eslint-disable-line init-declarations

if (identifierNode.static) {
tokenIndex++;
}

if (identifierNode.variance) {
tokenIndex++;
}

return context.getSourceCode().getFirstToken(identifierNode, tokenIndex).value;
}

if (identifierNode.type === 'FunctionTypeParam') {
Expand Down
92 changes: 92 additions & 0 deletions tests/rules/assertions/spaceAfterTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,38 @@ const CLASS_PROPERTIES = {
errors: [{message: 'There must be no space after "foo" type annotation colon.'}],
options: ['never'],
output: 'declare class X { static foo :number }'
},
{
code: 'class X { +foo:string }',
errors: [{message: 'There must be a space after "foo" class property type annotation colon.'}],
output: 'class X { +foo: string }'
},
{
code: 'class X { +foo: string }',
errors: [{message: 'There must be 1 space after "foo" class property type annotation colon.'}],
output: 'class X { +foo: string }'
},
{
code: 'class X { +foo: string }',
errors: [{message: 'There must be no space after "foo" class property type annotation colon.'}],
options: ['never'],
output: 'class X { +foo:string }'
},
{
code: 'class X { static +foo:string }',
errors: [{message: 'There must be a space after "foo" class property type annotation colon.'}],
output: 'class X { static +foo: string }'
},
{
code: 'class X { static +foo: string }',
errors: [{message: 'There must be 1 space after "foo" class property type annotation colon.'}],
output: 'class X { static +foo: string }'
},
{
code: 'class X { static +foo: string }',
errors: [{message: 'There must be no space after "foo" class property type annotation colon.'}],
options: ['never'],
output: 'class X { static +foo:string }'
}
],
valid: [
Expand Down Expand Up @@ -512,6 +544,20 @@ const CLASS_PROPERTIES = {
{
code: 'declare class X { static foo :number }',
options: ['never']
},
{
code: 'class X { +foo: string }'
},
{
code: 'class X { static +foo: string }'
},
{
code: 'class X { +foo:string }',
options: ['never']
},
{
code: 'class X { static +foo:string }',
options: ['never']
}
]
};
Expand Down Expand Up @@ -618,6 +664,38 @@ const OBJECT_TYPE_PROPERTIES = {
code: 'type X = { get: <X>() => A; }',
errors: [{message: 'There must be 1 space after "get" type annotation colon.'}],
output: 'type X = { get: <X>() => A; }'
},
{
code: 'type X = { +foo:string }',
errors: [{message: 'There must be a space after "foo" type annotation colon.'}],
output: 'type X = { +foo: string }'
},
{
code: 'type X = { +foo: string }',
errors: [{message: 'There must be 1 space after "foo" type annotation colon.'}],
output: 'type X = { +foo: string }'
},
{
code: 'type X = { +foo: string }',
errors: [{message: 'There must be no space after "foo" type annotation colon.'}],
options: ['never'],
output: 'type X = { +foo:string }'
},
{
code: 'type X = { +foo?:string }',
errors: [{message: 'There must be a space after "foo" type annotation colon.'}],
output: 'type X = { +foo?: string }'
},
{
code: 'type X = { +foo?: string }',
errors: [{message: 'There must be 1 space after "foo" type annotation colon.'}],
output: 'type X = { +foo?: string }'
},
{
code: 'type X = { +foo?: string }',
errors: [{message: 'There must be no space after "foo" type annotation colon.'}],
options: ['never'],
output: 'type X = { +foo?:string }'
}
],
valid: [
Expand Down Expand Up @@ -679,6 +757,20 @@ const OBJECT_TYPE_PROPERTIES = {
{
code: 'type X = { get:<X>() => A; }',
options: ['never']
},
{
code: 'type X = { +foo: string }'
},
{
code: 'type X = { +foo?: string }'
},
{
code: 'type X = { +foo:string }',
options: ['never']
},
{
code: 'type X = { +foo?:string }',
options: ['never']
}
]
};
Expand Down
100 changes: 100 additions & 0 deletions tests/rules/assertions/spaceBeforeTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,42 @@ const CLASS_PROPERTIES = {
errors: [{message: 'There must be a space before "bar" type annotation colon.'}],
options: ['always'],
output: 'declare class Foo { static bar : number; }'
},
{
code: 'class X { +foo: string }',
errors: [{message: 'There must be a space before "foo" class property type annotation colon.'}],
options: ['always'],
output: 'class X { +foo : string }'
},
{
code: 'class X { +foo : string }',
errors: [{message: 'There must be 1 space before "foo" class property type annotation colon.'}],
options: ['always'],
output: 'class X { +foo : string }'
},
{
code: 'class X { +foo : string }',
errors: [{message: 'There must be no space before "foo" class property type annotation colon.'}],
options: ['never'],
output: 'class X { +foo: string }'
},
{
code: 'class X { static +foo: string }',
errors: [{message: 'There must be a space before "foo" class property type annotation colon.'}],
options: ['always'],
output: 'class X { static +foo : string }'
},
{
code: 'class X { static +foo : string }',
errors: [{message: 'There must be 1 space before "foo" class property type annotation colon.'}],
options: ['always'],
output: 'class X { static +foo : string }'
},
{
code: 'class X { static +foo : string }',
errors: [{message: 'There must be no space before "foo" class property type annotation colon.'}],
options: ['never'],
output: 'class X { static +foo: string }'
}
],
valid: [
Expand Down Expand Up @@ -396,6 +432,20 @@ const CLASS_PROPERTIES = {
{
code: 'declare class Foo { static bar : number; }',
options: ['always']
},
{
code: 'class X { +foo: string }'
},
{
code: 'class X { static +foo: string }'
},
{
code: 'class X { +foo : string }',
options: ['always']
},
{
code: 'class X { static +foo : string }',
options: ['always']
}
]
};
Expand Down Expand Up @@ -447,6 +497,42 @@ const OBJECT_TYPE_PROPERTIES = {
errors: [{message: 'There must be a space before "foo" type annotation colon.'}],
options: ['always'],
output: 'type X = { foo ? : string }'
},
{
code: 'type X = { +foo: string }',
errors: [{message: 'There must be a space before "foo" type annotation colon.'}],
options: ['always'],
output: 'type X = { +foo : string }'
},
{
code: 'type X = { +foo : string }',
errors: [{message: 'There must be 1 space before "foo" type annotation colon.'}],
options: ['always'],
output: 'type X = { +foo : string }'
},
{
code: 'type X = { +foo : string }',
errors: [{message: 'There must be no space before "foo" type annotation colon.'}],
options: ['never'],
output: 'type X = { +foo: string }'
},
{
code: 'type X = { +foo?: string }',
errors: [{message: 'There must be a space before "foo" type annotation colon.'}],
options: ['always'],
output: 'type X = { +foo? : string }'
},
{
code: 'type X = { +foo? : string }',
errors: [{message: 'There must be 1 space before "foo" type annotation colon.'}],
options: ['always'],
output: 'type X = { +foo? : string }'
},
{
code: 'type X = { +foo? : string }',
errors: [{message: 'There must be no space before "foo" type annotation colon.'}],
options: ['never'],
output: 'type X = { +foo?: string }'
}
],
valid: [
Expand All @@ -466,6 +552,20 @@ const OBJECT_TYPE_PROPERTIES = {
{
code: 'type X = { foo? : string }',
options: ['always']
},
{
code: 'type X = { +foo: string }'
},
{
code: 'type X = { +foo?: string }'
},
{
code: 'type X = { +foo : string }',
options: ['always']
},
{
code: 'type X = { +foo? : string }',
options: ['always']
}
]
};
Expand Down

0 comments on commit 715a389

Please sign in to comment.