-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analysis) scalar change analysis (#704)
* feat(analysis) scalar change analysis Signed-off-by: Dan Selman <[email protected]>
- Loading branch information
Showing
23 changed files
with
350 additions
and
8 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
72 changes: 72 additions & 0 deletions
72
packages/concerto-analysis/src/comparers/scalar-declarations.ts
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,72 @@ | ||
/* | ||
* 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 { ComparerFactory } from '../comparer'; | ||
import { getValidatorType } from '../compare-utils'; | ||
|
||
const scalarDeclarationExtendsChanged: ComparerFactory = (context) => ({ | ||
compareScalarDeclaration: (a, b) => { | ||
|
||
if (!a || !b) { | ||
return; | ||
} | ||
|
||
if(a.getType() !== b.getType()) { | ||
context.report({ | ||
key: 'scalar-extends-changed', | ||
message: `The scalar extends was changed from "${a.getType()}" to "${b.getType()}"`, | ||
element: b.getName() | ||
}); | ||
} | ||
}, | ||
}); | ||
|
||
const scalarValidatorChanged: ComparerFactory = (context) => ({ | ||
compareScalarDeclaration: (a, b) => { | ||
if (!a || !b) { | ||
return; | ||
} | ||
const aValidator = a.getValidator(); | ||
const bValidator = b.getValidator(); | ||
if (!aValidator && !bValidator) { | ||
return; | ||
} else if (!aValidator && bValidator) { | ||
const bValidatorType = getValidatorType(bValidator); | ||
context.report({ | ||
key: 'scalar-validator-added', | ||
message: `A ${bValidatorType} validator was added to the scalar "${a.getName()}"`, | ||
element: a.getName() | ||
}); | ||
return; | ||
} else if (aValidator && !bValidator) { | ||
const aValidatorType = getValidatorType(aValidator); | ||
context.report({ | ||
key: 'scalar-validator-removed', | ||
message: `A ${aValidatorType} validator was removed from the scalar "${a.getName()}"`, | ||
element: a.getName() | ||
}); | ||
return; | ||
} else if (!aValidator.compatibleWith(bValidator)) { | ||
const aValidatorType = getValidatorType(aValidator); | ||
context.report({ | ||
key: 'scalar-validator-changed', | ||
message: `A ${aValidatorType} validator for the scalar "${a.getName()}" was changed and is no longer compatible`, | ||
element: a.getName() | ||
}); | ||
return; | ||
} | ||
} | ||
}); | ||
|
||
export const scalarDeclarationComparerFactories = [scalarDeclarationExtendsChanged, scalarValidatorChanged]; |
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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends String |
3 changes: 3 additions & 0 deletions
3
packages/concerto-analysis/test/fixtures/scalar-changed-extends.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends Integer |
3 changes: 3 additions & 0 deletions
3
packages/concerto-analysis/test/fixtures/scalar-number-validator-added.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends Integer range=[-1,1] |
3 changes: 3 additions & 0 deletions
3
packages/concerto-analysis/test/fixtures/scalar-number-validator-changed-lowercompat.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends Integer range=[-2,1] |
3 changes: 3 additions & 0 deletions
3
packages/concerto-analysis/test/fixtures/scalar-number-validator-changed-uppercompat.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends Integer range=[-1,2] |
3 changes: 3 additions & 0 deletions
3
packages/concerto-analysis/test/fixtures/scalar-number-validator-changed-upperincompat.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends Integer range=[-1,0] |
3 changes: 3 additions & 0 deletions
3
packages/concerto-analysis/test/fixtures/scalar-number-validators.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends Integer |
3 changes: 3 additions & 0 deletions
3
packages/concerto-analysis/test/fixtures/scalar-string-validator-added.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends String regex=/foo/ |
3 changes: 3 additions & 0 deletions
3
packages/concerto-analysis/test/fixtures/scalar-string-validator-changed.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends String regex=/bar/ |
3 changes: 3 additions & 0 deletions
3
packages/concerto-analysis/test/fixtures/scalar-string-validator-length-added.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends String length=[2,10] |
3 changes: 3 additions & 0 deletions
3
...es/concerto-analysis/test/fixtures/scalar-string-validator-length-changed-lowercompat.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends String length=[1,10] |
3 changes: 3 additions & 0 deletions
3
.../concerto-analysis/test/fixtures/scalar-string-validator-length-changed-lowerincompat.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends String length=[3,10] |
3 changes: 3 additions & 0 deletions
3
...es/concerto-analysis/test/fixtures/scalar-string-validator-length-changed-uppercompat.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends String length=[2,100] |
3 changes: 3 additions & 0 deletions
3
.../concerto-analysis/test/fixtures/scalar-string-validator-length-changed-upperincompat.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends String length=[2,8] |
3 changes: 3 additions & 0 deletions
3
packages/concerto-analysis/test/fixtures/scalar-string-validators.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,3 @@ | ||
namespace [email protected] | ||
|
||
scalar Thing extends String |
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 |
---|---|---|
|
@@ -22,6 +22,11 @@ test('should throw for unknown class declaration type', () => { | |
expect(() => getDeclarationType(classDeclaration)).toThrow('unknown class declaration type "ClassDeclaration {[email protected] super=Concept enum=false abstract=false}"'); | ||
}); | ||
|
||
test('should throw for unknown thing', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
expect(() => getDeclarationType('thing' as any)).toThrow('unknown declaration type "thing"'); | ||
}); | ||
|
||
test('should throw for unknown class property type', () => { | ||
expect(() => getPropertyType(property)).toThrow('unknown property type "[object Object]'); | ||
}); | ||
|
Oops, something went wrong.