-
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.
chore(specs): add eslint rule to avoid cross-references (#3413)
- Loading branch information
Showing
13 changed files
with
188 additions
and
32 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
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
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,73 @@ | ||
import type { Rule } from 'eslint'; | ||
|
||
import { isPairWithKey, isScalar } from '../utils'; | ||
|
||
const allSpecs = [ | ||
'abtesting', | ||
'analytics', | ||
'crawler', | ||
'ingestion', | ||
'insights', | ||
'monitoring', | ||
'personalization', | ||
'query-suggestions', | ||
'recommend', | ||
'search', | ||
'usage', | ||
]; | ||
|
||
export const refCommon: Rule.RuleModule = { | ||
meta: { | ||
docs: { | ||
description: | ||
'the $ref must target the current spec, or the common spec. If you intended to use a model from an other spec, move it to the common folder', | ||
}, | ||
messages: { | ||
refCommon: '$ref to another spec', | ||
}, | ||
}, | ||
create(context) { | ||
if (!context.sourceCode.parserServices.isYAML) { | ||
return {}; | ||
} | ||
|
||
return { | ||
YAMLPair(node): void { | ||
if (!isPairWithKey(node, '$ref')) { | ||
return; | ||
} | ||
|
||
if (!isScalar(node.value)) { | ||
return; | ||
} | ||
|
||
let ref = node.value.value; | ||
if ( | ||
typeof ref !== 'string' || | ||
ref.trim().startsWith('#/') || | ||
ref.startsWith('./') | ||
) { | ||
return; | ||
} | ||
|
||
const spec = context.filename.match(/specs\/([a-z-]+?)\//)?.[1]; | ||
if (!spec) { | ||
return; | ||
} | ||
while (ref.startsWith(`../`)) { | ||
ref = ref.slice(3); | ||
} | ||
if ( | ||
allSpecs.filter((s) => s !== spec).every((s) => !ref.startsWith(s)) | ||
) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: node as any, | ||
messageId: 'refCommon', | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
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 |
---|---|---|
|
@@ -35,6 +35,6 @@ multiple lines`, | |
`, | ||
errors: [{ messageId: 'noNewLine' }], | ||
output: `multiple new lines`, | ||
} | ||
}, | ||
], | ||
}); |
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,67 @@ | ||
import { RuleTester } from 'eslint'; | ||
|
||
import { refCommon } from '../src/rules/refCommon'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('yaml-eslint-parser'), | ||
}); | ||
|
||
ruleTester.run('ref-common', refCommon, { | ||
valid: [ | ||
{ | ||
filename: 'api-client-automation/specs/search/path/test.yml', | ||
code: ` | ||
local: | ||
$ref: '#/components/schemas/Example' | ||
`, | ||
}, | ||
{ | ||
filename: 'renamedRepo/specs/search/path/test.yml', | ||
code: ` | ||
local: | ||
$ref: '#/components/schemas/Example' | ||
`, | ||
}, | ||
{ | ||
filename: 'api-client-automation/specs/search/path/test.yml', | ||
code: ` | ||
sameFolder: | ||
$ref: './schemas/Example' | ||
`, | ||
}, | ||
{ | ||
filename: 'api-client-automation/specs/search/path/test.yml', | ||
code: ` | ||
external: | ||
$ref: '../../common/schemas/Example' | ||
`, | ||
}, | ||
{ | ||
filename: 'api-client-automation/specs/search/path/test.yml', | ||
code: ` | ||
external: | ||
$ref: '../../../search/schemas/Example' | ||
`, | ||
}, | ||
{ | ||
filename: 'api-client-automation/specs/search/path/test.yml', | ||
code: ` | ||
nested: | ||
type: object | ||
properties: | ||
nested: | ||
$ref: '#/components/schemas/Example' | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'api-client-automation/specs/search/path/test.yml', | ||
code: ` | ||
out: | ||
$ref: '../../recommend/schemas/Example' | ||
`, | ||
errors: [{ messageId: 'refCommon' }], | ||
}, | ||
], | ||
}); |
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
}, | ||
"include": [ | ||
"src/**/*.ts", | ||
"tests/**/*.ts" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
|
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