forked from accordproject/concerto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): detect enum value add/remove (contributes to accordproject#442)
Signed-off-by: Simon Stone <[email protected]>
- Loading branch information
Simon Stone
authored and
Simon Stone
committed
Aug 5, 2022
1 parent
958074b
commit b7d6cc7
Showing
8 changed files
with
150 additions
and
75 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 was deleted.
Oops, something went wrong.
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,87 @@ | ||
/* | ||
* 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 { EnumValueDeclaration, Field } from '@accordproject/concerto-core'; | ||
import { getClassDeclarationType, getPropertyType } from '../compare-utils'; | ||
import { ComparerFactory } from '../comparer'; | ||
|
||
const propertyAdded: ComparerFactory = (context) => ({ | ||
compareProperty: (a, b) => { | ||
if (a || !b) { | ||
return; | ||
} | ||
const classDeclarationType = getClassDeclarationType(b.getParent()); | ||
if (b instanceof EnumValueDeclaration) { | ||
context.report({ | ||
key: 'enum-value-added', | ||
message: `The enum value "${b.getName()}" was added to the ${classDeclarationType} "${b.getParent().getName()}"`, | ||
element: b, | ||
}); | ||
return; | ||
} | ||
const isOptional = b.isOptional(); | ||
const hasDefault = b instanceof Field ? !!b.getDefaultValue() : false; | ||
const required = !isOptional && !hasDefault; | ||
const type = getPropertyType(b); | ||
if (required) { | ||
context.report({ | ||
key: 'required-property-added', | ||
message: `The required ${type} "${b.getName()}" was added to the ${classDeclarationType} "${b.getParent().getName()}"`, | ||
element: b, | ||
}); | ||
} else { | ||
context.report({ | ||
key: 'optional-property-added', | ||
message: `The optional ${type} "${b.getName()}" was added to the ${classDeclarationType} "${b.getParent().getName()}"`, | ||
element: b, | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
const propertyRemoved: ComparerFactory = (context) => ({ | ||
compareProperty: (a, b) => { | ||
if (!a || b) { | ||
return; | ||
} | ||
const classDeclarationType = getClassDeclarationType(a.getParent()); | ||
if (a instanceof EnumValueDeclaration) { | ||
context.report({ | ||
key: 'enum-value-removed', | ||
message: `The enum value "${a.getName()}" was removed from the ${classDeclarationType} "${a.getParent().getName()}"`, | ||
element: b, | ||
}); | ||
return; | ||
} | ||
const isOptional = a.isOptional(); | ||
const hasDefault = a instanceof Field ? !!a.getDefaultValue() : false; | ||
const required = !isOptional && !hasDefault; | ||
const type = getPropertyType(a); | ||
if (required) { | ||
context.report({ | ||
key: 'required-property-removed', | ||
message: `The required ${type} "${a.getName()}" was removed from the ${classDeclarationType} "${a.getParent().getName()}"`, | ||
element: a | ||
}); | ||
} else { | ||
context.report({ | ||
key: 'optional-property-removed', | ||
message: `The optional ${type} "${a.getName()}" was removed from the ${classDeclarationType} "${a.getParent().getName()}"`, | ||
element: a | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
export const propertyComparerFactories = [propertyAdded, propertyRemoved]; |
5 changes: 5 additions & 0 deletions
5
packages/concerto-analysis/test/fixtures/enum-value-added-a.cto
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,5 @@ | ||
namespace [email protected] | ||
|
||
enum Thing { | ||
o FOO | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/concerto-analysis/test/fixtures/enum-value-added-b.cto
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,6 @@ | ||
namespace [email protected] | ||
|
||
enum Thing { | ||
o FOO | ||
o BAR | ||
} |
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