-
-
Notifications
You must be signed in to change notification settings - Fork 212
fix(concerto-linter): lint reserved system concept declarations #1237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rishabh060105
wants to merge
2
commits into
accordproject:main
Choose a base branch
from
Rishabh060105:Rishabh060105/issue-1216-reserved-system-concept-lint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
100 changes: 100 additions & 0 deletions
100
...oncerto-linter/default-ruleset/src/functions/find-reserved-system-concept-declarations.ts
This file contains hidden or 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,100 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import type { IFunction, IFunctionResult } from '@stoplight/spectral-core'; | ||
| import semver from 'semver'; | ||
|
|
||
| const RESERVED_SYSTEM_CONCEPT_NAMES = new Set([ | ||
| 'Concept', | ||
| 'Asset', | ||
| 'Transaction', | ||
| 'Participant', | ||
| 'Event', | ||
| ]); | ||
|
|
||
| interface Declaration { | ||
| name?: string; | ||
| } | ||
|
|
||
| interface Model { | ||
| namespace?: string; | ||
| declarations?: Declaration[]; | ||
| concertoVersion?: string; | ||
| } | ||
|
|
||
| interface ReservedSystemConceptOptions { | ||
| dangerouslyAllowReservedSystemTypeNamesInUserModels?: boolean; | ||
| } | ||
|
|
||
| function isLegacyOrV3Model(model: Model): boolean { | ||
| if (typeof model.concertoVersion === 'string') { | ||
| const minimumVersion = semver.minVersion(model.concertoVersion); | ||
| if (minimumVersion) { | ||
| return minimumVersion.major === 3; | ||
| } | ||
| } | ||
|
|
||
| return typeof model.namespace === 'string' && !model.namespace.includes('@'); | ||
| } | ||
|
|
||
| function getReservedSystemConceptMessage(declarationName: string, isLegacyModel: boolean): string { | ||
| if (isLegacyModel) { | ||
| return `Declaration '${declarationName}' collides with a reserved Concerto system concept. Rename the declaration or migrate safely before relying on this model.`; | ||
| } | ||
|
|
||
| return `Declaration '${declarationName}' collides with a reserved Concerto system concept. Rename the declaration, disable dangerouslyAllowReservedSystemTypeNamesInUserModels, or migrate safely before relying on this model.`; | ||
| } | ||
|
|
||
| /** | ||
| * Finds declaration names that collide with reserved Concerto system concepts | ||
| * in risky compatibility contexts. | ||
| * | ||
| * @param {unknown} targetVal The AST node to check, expected to be a Concerto model. | ||
| * @param {ReservedSystemConceptOptions} options Rule options controlling v4 dangerous mode behavior. | ||
| * @returns {IFunctionResult[]} A result for each matching declaration. | ||
| */ | ||
| export const findReservedSystemConceptDeclarations: IFunction = ( | ||
| targetVal, | ||
| options?: unknown, | ||
| ): IFunctionResult[] => { | ||
| if (typeof targetVal !== 'object' || targetVal === null) { | ||
| throw new Error('Value must be a valid AST object for a Concerto model.'); | ||
| } | ||
|
|
||
| const model = targetVal as Model; | ||
| if (!Array.isArray(model.declarations)) { | ||
| return []; | ||
| } | ||
|
|
||
| const functionOptions = options as ReservedSystemConceptOptions | undefined; | ||
| const dangerousModeEnabled = Boolean(functionOptions?.dangerouslyAllowReservedSystemTypeNamesInUserModels); | ||
| const legacyOrV3Model = isLegacyOrV3Model(model); | ||
| const shouldReport = legacyOrV3Model || dangerousModeEnabled; | ||
|
|
||
| if (!shouldReport) { | ||
| return []; | ||
| } | ||
|
|
||
| return model.declarations.reduce<IFunctionResult[]>((results, declaration) => { | ||
| if (!declaration?.name || !RESERVED_SYSTEM_CONCEPT_NAMES.has(declaration.name)) { | ||
| return results; | ||
| } | ||
|
|
||
| results.push({ | ||
| message: getReservedSystemConceptMessage(declaration.name, legacyOrV3Model), | ||
| }); | ||
|
|
||
| return results; | ||
| }, []); | ||
| }; | ||
This file contains hidden or 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
30 changes: 30 additions & 0 deletions
30
packages/concerto-linter/default-ruleset/src/reserved-system-concept-declarations.ts
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { findReservedSystemConceptDeclarations } from './functions/find-reserved-system-concept-declarations'; | ||
|
|
||
| /** | ||
| * Rule: Reserved System Concept Declarations | ||
| * ------------------------------------------ | ||
| * Flags declaration names that collide with reserved Concerto system concepts | ||
| * in legacy/v3 models, or when dangerous reserved system type names are | ||
| * explicitly allowed in v4 compatibility mode. | ||
| */ | ||
| export default { | ||
| given: '$.models[*]', | ||
| severity: 0, // 0 = error, 1 = warning, 2 = info, 3 = hint | ||
| then: { | ||
| function: findReservedSystemConceptDeclarations, | ||
| }, | ||
| }; |
This file contains hidden or 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
9 changes: 9 additions & 0 deletions
9
...certo-linter/default-ruleset/test/fixtures/no-reserved-keywords-system-concepts-valid.cto
This file contains hidden or 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,9 @@ | ||
| namespace org.example@1.0.0 | ||
|
|
||
| asset Asset identified by assetId { | ||
| o String assetId | ||
| } | ||
|
|
||
| transaction Transaction { | ||
| o String transactionId optional | ||
| } |
5 changes: 5 additions & 0 deletions
5
...ter/default-ruleset/test/fixtures/reserved-system-concept-declarations-legacy-invalid.cto
This file contains hidden or 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,5 @@ | ||
| namespace org.example | ||
|
|
||
| participant Participant identified by participantId { | ||
| o String participantId | ||
| } |
11 changes: 11 additions & 0 deletions
11
...-linter/default-ruleset/test/fixtures/reserved-system-concept-declarations-v3-invalid.cto
This file contains hidden or 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,11 @@ | ||
| concerto version "^3.0.0" | ||
|
|
||
| namespace org.example@1.0.0 | ||
|
|
||
| asset Asset identified by assetId { | ||
| o String assetId | ||
| } | ||
|
|
||
| concept Concept { | ||
| o String name | ||
| } |
9 changes: 9 additions & 0 deletions
9
...-linter/default-ruleset/test/fixtures/reserved-system-concept-declarations-v4-invalid.cto
This file contains hidden or 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,9 @@ | ||
| namespace org.example@1.0.0 | ||
|
|
||
| asset Asset identified by assetId { | ||
| o String assetId | ||
| } | ||
|
|
||
| event Event { | ||
| o String eventId optional | ||
| } |
9 changes: 9 additions & 0 deletions
9
...to-linter/default-ruleset/test/fixtures/reserved-system-concept-declarations-v4-valid.cto
This file contains hidden or 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,9 @@ | ||
| namespace org.example@1.0.0 | ||
|
|
||
| asset Vehicle identified by vehicleId { | ||
| o String vehicleId | ||
| } | ||
|
|
||
| concept Shipment { | ||
| o String shipmentId | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.