-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
120 changed files
with
1,006 additions
and
660 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 |
---|---|---|
@@ -1 +1 @@ | ||
7.1.0 | ||
7.2.0 |
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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import { descriptionDot } from './rules/descriptionDot'; | ||
import { outOfLineEnum } from './rules/outOfLineEnum'; | ||
import { singleQuoteRef } from './rules/singleQuoteRef'; | ||
|
||
const rules = { | ||
'description-dot': descriptionDot, | ||
'out-of-line-enum': outOfLineEnum, | ||
'single-quote-ref': singleQuoteRef, | ||
}; | ||
|
||
export { rules }; |
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,44 @@ | ||
import type { Rule } from 'eslint'; | ||
|
||
import { isPairWithKey } from '../utils'; | ||
|
||
export const outOfLineEnum: Rule.RuleModule = { | ||
meta: { | ||
docs: { | ||
description: 'enum must be out of line', | ||
}, | ||
messages: { | ||
enumNotOutOfLine: 'enum is not out of line', | ||
}, | ||
}, | ||
create(context) { | ||
if (!context.parserServices.isYAML) { | ||
return {}; | ||
} | ||
|
||
return { | ||
YAMLPair(node): void { | ||
if (!isPairWithKey(node, 'enum')) { | ||
return; | ||
} | ||
// parent is mapping, and parent is real parent that must be to the far left | ||
if (node.parent.parent.loc.start.column === 0) { | ||
return; | ||
} | ||
if ( | ||
isPairWithKey( | ||
node.parent.parent.parent.parent?.parent?.parent?.parent ?? null, | ||
'servers' | ||
) | ||
) { | ||
// accept out of line enum if in servers | ||
return; | ||
} | ||
context.report({ | ||
node: node.parent.parent as any, | ||
messageId: 'enumNotOutOfLine', | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
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,55 @@ | ||
import type { Rule } from 'eslint'; | ||
|
||
import { isBLockScalar, isPairWithKey, isScalar } from '../utils'; | ||
|
||
export const singleQuoteRef: Rule.RuleModule = { | ||
meta: { | ||
docs: { | ||
description: '$ref must be wrapped in single quote', | ||
}, | ||
messages: { | ||
refNoQuote: '$ref is not wrapped in single quote', | ||
}, | ||
fixable: 'code', | ||
}, | ||
create(context) { | ||
if (!context.parserServices.isYAML) { | ||
return {}; | ||
} | ||
|
||
return { | ||
YAMLPair(node): void { | ||
if (!isPairWithKey(node, '$ref')) { | ||
return; | ||
} | ||
if (!isScalar(node.value)) { | ||
// not our problem, something else will fail like path resolution | ||
return; | ||
} | ||
if (node.value.style === 'single-quoted') { | ||
// that's what we want | ||
return; | ||
} | ||
if (isBLockScalar(node.value)) { | ||
// another rule should take care of that case | ||
return; | ||
} | ||
const hasDoubleQuote = node.value.style === 'double-quoted'; | ||
const [start, end] = node.value.range; | ||
context.report({ | ||
node: node as any, | ||
messageId: 'refNoQuote', | ||
*fix(fixer) { | ||
if (hasDoubleQuote) { | ||
yield fixer.replaceTextRange([start, start + 1], "'"); | ||
yield fixer.replaceTextRange([end - 1, end], "'"); | ||
} else { | ||
yield fixer.insertTextBeforeRange([start, start], "'"); | ||
yield fixer.insertTextAfterRange([end, end], "'"); | ||
} | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
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,66 @@ | ||
import { RuleTester } from 'eslint'; | ||
|
||
import { outOfLineEnum } from '../src/rules/outOfLineEnum'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('yaml-eslint-parser'), | ||
}); | ||
|
||
ruleTester.run('out-of-line-enum', outOfLineEnum, { | ||
valid: [ | ||
` | ||
simple: | ||
type: string | ||
enum: [bla, blabla] | ||
`, | ||
` | ||
simple: | ||
type: string | ||
enum: | ||
- bla | ||
- blabla | ||
`, | ||
` | ||
simple: | ||
type: string | ||
enum: [bla, blabla] | ||
useIt: | ||
$ref: '#/simple' | ||
`, | ||
` | ||
servers: | ||
- url: http://test-server.com | ||
variables: | ||
region: | ||
default: us | ||
enum: | ||
- us | ||
- de | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
root: | ||
inside: | ||
type: string | ||
enum: [bla, blabla] | ||
`, | ||
errors: [{ messageId: 'enumNotOutOfLine' }], | ||
}, | ||
{ | ||
code: ` | ||
root: | ||
inside: | ||
deeper: | ||
type: string | ||
enum: [bla, blabla] | ||
useIt: | ||
$ref: '#/root/inside/deeper' | ||
`, | ||
errors: [{ messageId: 'enumNotOutOfLine' }], | ||
}, | ||
], | ||
}); |
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,82 @@ | ||
import { RuleTester } from 'eslint'; | ||
|
||
import { singleQuoteRef } from '../src/rules/singleQuoteRef'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('yaml-eslint-parser'), | ||
}); | ||
|
||
ruleTester.run('single-quote-ref', singleQuoteRef, { | ||
valid: [ | ||
` | ||
simple: | ||
$ref: 'random/path.yml'`, | ||
` | ||
sameFile: | ||
$ref: '#/inside'`, | ||
` | ||
long: | ||
$ref: 'some/random/file.yml#/root/object/prop' | ||
`, | ||
` | ||
post: | ||
description: test desc. | ||
'400': | ||
$ref: '../../common/responses/BadRequest.yml' | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
simple: | ||
$ref: random/path.yml | ||
`, | ||
errors: [{ messageId: 'refNoQuote' }], | ||
output: ` | ||
simple: | ||
$ref: 'random/path.yml' | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
long: | ||
$ref: some/random/file.yml#/root/object/prop | ||
`, | ||
errors: [{ messageId: 'refNoQuote' }], | ||
output: ` | ||
long: | ||
$ref: 'some/random/file.yml#/root/object/prop' | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
post: | ||
description: test desc. | ||
'400': | ||
$ref: ../../common/responses/BadRequest.yml | ||
'404': | ||
$ref: ../../common/responses/IndexNotFound.yml | ||
`, | ||
errors: [{ messageId: 'refNoQuote' }, { messageId: 'refNoQuote' }], | ||
output: ` | ||
post: | ||
description: test desc. | ||
'400': | ||
$ref: '../../common/responses/BadRequest.yml' | ||
'404': | ||
$ref: '../../common/responses/IndexNotFound.yml' | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
double: | ||
$ref: "some/ref" | ||
`, | ||
errors: [{ messageId: 'refNoQuote' }], | ||
output: ` | ||
double: | ||
$ref: 'some/ref' | ||
`, | ||
}, | ||
], | ||
}); |
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
Oops, something went wrong.