Skip to content

Commit

Permalink
Prevent Scopes from extending other Scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Leland Takamine authored and Leland-Takamine committed Jan 7, 2020
1 parent 1d90d28 commit 2400b50
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
# 0.3.2-SNAPSHOT

* Throw a compiler error when a Scope extends another Scope

# 0.3.1

* No changes
Expand Down
Expand Up @@ -52,6 +52,7 @@ internal interface ErrorHandler {
is InvalidQualifier -> InvalidQualifierHandler(error)
is DuplicatedChildParameterSource -> DuplicatedChildParameterSourceHandler(error)
is DuplicatedDependenciesMethod -> DuplicatedDependenciesMethodHandler(error)
is ScopeExtendsScope -> ScopeExtendsScopeMethodHandler(error)
}
is ProcessingError -> when (error) {
is ScopeCycleError -> ScopeCycleHandler(error)
Expand Down
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2018-2019 Uber Technologies, Inc.
*
* 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.
*/
package motif.errormessage

import motif.models.ScopeExtendsScope

class ScopeExtendsScopeMethodHandler(private val error: ScopeExtendsScope) : ErrorHandler {

override val name = "SCOPE EXTENDS SCOPE"

override fun StringBuilder.handle() {
appendln("""
Scopes can't extend other Scopes:
${error.scope.qualifiedName}
""".trimIndent())
}
}
1 change: 1 addition & 0 deletions models/src/main/kotlin/motif/models/ParsingError.kt
Expand Up @@ -39,3 +39,4 @@ class NullableSpreadMethod(val objects: Objects, val factoryMethod: IrMethod, va
class InvalidQualifier(val annotated: IrAnnotated, val annotation: IrAnnotation) : ParsingError()
class DuplicatedChildParameterSource(val scope: Scope, val childScopeMethod: ChildMethod, val duplicatedParameters: List<ChildMethod.Parameter>) : ParsingError()
class DuplicatedDependenciesMethod(val scope: Scope, val duplicatedMethods: List<Dependencies.Method>) : ParsingError()
class ScopeExtendsScope(val scope: Scope) : ParsingError()
15 changes: 15 additions & 0 deletions models/src/main/kotlin/motif/models/Scope.kt
Expand Up @@ -53,6 +53,7 @@ class ValidScope internal constructor(clazz: IrClass) : Scope(clazz) {

init {
if (clazz.kind != IrClass.Kind.INTERFACE) throw ScopeMustBeAnInterface(clazz)
detectScopeSuperinterface(this)
}

override val objects: Objects? = Objects.fromScope(this)
Expand All @@ -66,6 +67,20 @@ class ValidScope internal constructor(clazz: IrClass) : Scope(clazz) {
override val factoryMethods: List<FactoryMethod> = objects?.factoryMethods ?: emptyList()

override val dependencies: Dependencies? = Dependencies.fromScope(this)

companion object {

private fun detectScopeSuperinterface(scope: Scope, clazz: IrClass = scope.clazz) {
clazz.supertypes.forEach { superType ->
superType.resolveClass()?.let { superClass ->
if (superClass.hasAnnotation(motif.Scope::class)) {
throw ScopeExtendsScope(scope)
}
detectScopeSuperinterface(scope, superClass)
}
}
}
}
}

private class ScopeFactory(
Expand Down
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2018-2019 Uber Technologies, Inc.
*
* 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.
*/
package testcases.E057_scope_extends_scope;

@motif.Scope
public interface BarScope {}
24 changes: 24 additions & 0 deletions tests/src/main/java/testcases/E057_scope_extends_scope/ERROR.txt
@@ -0,0 +1,24 @@
########################################################################
# #
# This file is auto-generated by running the Motif compiler tests and #
# serves both as validation of error correctness and as a record of #
# the current compiler error output. #
# #
# - Do not edit manually. #
# - Commit changes to source control. #
# - Since this file is autogenerated, code review changes carefully to #
# ensure correctness. #
# #
########################################################################

====================================
Motif Errors
====================================

[SCOPE EXTENDS SCOPE]

Scopes can't extend other Scopes:

testcases.E057_scope_extends_scope.FooScope

====================================
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2018-2019 Uber Technologies, Inc.
*
* 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.
*/
package testcases.E057_scope_extends_scope;

@motif.Scope
public interface FooScope extends BarScope {}
39 changes: 39 additions & 0 deletions tests/src/main/java/testcases/E057_scope_extends_scope/GRAPH.txt
@@ -0,0 +1,39 @@
########################################################################
# #
# This file is auto-generated by running the Motif compiler tests and #
# serves a as validation of graph correctness. IntelliJ plugin tests #
# also rely on this file to ensure that the plugin graph understanding #
# is equivalent to the compiler's. #
# #
# - Do not edit manually. #
# - Commit changes to source control. #
# - Since this file is autogenerated, code review changes carefully to #
# ensure correctness. #
# #
########################################################################

----------
| BarScope |
----------

==== Required ====

==== Provides ====

---- BarScope | implicit ----
[ Required ]
[ Consumed By ]

----------
| FooScope |
----------

==== Required ====

==== Provides ====

---- FooScope | implicit ----
[ Required ]
[ Consumed By ]


@@ -0,0 +1,19 @@
/*
* Copyright (c) 2018-2019 Uber Technologies, Inc.
*
* 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.
*/
package testcases.E058_scope_extends_scope_intermediate_class;

@motif.Scope
public interface BarScope {}
@@ -0,0 +1,24 @@
########################################################################
# #
# This file is auto-generated by running the Motif compiler tests and #
# serves both as validation of error correctness and as a record of #
# the current compiler error output. #
# #
# - Do not edit manually. #
# - Commit changes to source control. #
# - Since this file is autogenerated, code review changes carefully to #
# ensure correctness. #
# #
########################################################################

====================================
Motif Errors
====================================

[SCOPE EXTENDS SCOPE]

Scopes can't extend other Scopes:

testcases.E058_scope_extends_scope_intermediate_class.FooScope

====================================
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2018-2019 Uber Technologies, Inc.
*
* 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.
*/
package testcases.E058_scope_extends_scope_intermediate_class;

@motif.Scope
public interface FooScope extends Intermediate {}
@@ -0,0 +1,39 @@
########################################################################
# #
# This file is auto-generated by running the Motif compiler tests and #
# serves a as validation of graph correctness. IntelliJ plugin tests #
# also rely on this file to ensure that the plugin graph understanding #
# is equivalent to the compiler's. #
# #
# - Do not edit manually. #
# - Commit changes to source control. #
# - Since this file is autogenerated, code review changes carefully to #
# ensure correctness. #
# #
########################################################################

----------
| BarScope |
----------

==== Required ====

==== Provides ====

---- BarScope | implicit ----
[ Required ]
[ Consumed By ]

----------
| FooScope |
----------

==== Required ====

==== Provides ====

---- FooScope | implicit ----
[ Required ]
[ Consumed By ]


@@ -0,0 +1,18 @@
/*
* Copyright (c) 2018-2019 Uber Technologies, Inc.
*
* 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.
*/
package testcases.E058_scope_extends_scope_intermediate_class;

public interface Intermediate extends BarScope {}

0 comments on commit 2400b50

Please sign in to comment.