Skip to content
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

fixes #74, introduces LR0004 WARNING diagnostic on unused facts (not … #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions src/modelValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,19 @@ class ModelValidator {
const node = jsonc.findNodeAtLocation(this.tree, [expressionCheckPath[0], index, expressionCheckPath[1]])
// console.log("ExpCheck en index", expressionCheckPath, index);
if (node && typeof node.value === 'string') {
// console.log("Node", node);
// console.log("Node", node)
return this._validateExpression(node.value, node.offset, expressionCheckPath[2])
} else {
return this._validateParsedExpressionNode(node)
}
}).reduce(concat, [])
}).reduce(concat, [])

return createTerminateErrors.concat(expressionErrors)
const unusedFactErrors = this.model.facts.map((fact) => {
return this._checkFactUsage(fact)
}).reduce(concat, [])

return createTerminateErrors.concat(expressionErrors).concat(unusedFactErrors)
}

_checkCreateTerminate (referenceString, node) {
Expand Down Expand Up @@ -373,6 +377,25 @@ class ModelValidator {
}
}
}

_checkFactUsage (fact) {
if ((this.referencePaths[fact.fact]) && (this.referencePaths[fact.fact].length === 1)) {
const node = jsonc.findNodeAtLocation(this.tree, this.identifierPaths[fact.fact])
const beginPosition = node.offset
const endPosition = node.offset + node.length
const path = jsonc.getNodePath(node)
return [{
code: 'LR0004',
message: 'Unused fact: ' + fact.fact,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of duplicates the information in the 'source' attribute. In the end in vscode this would show as something like: "Unused fact [unusedfact] ([unusedfact])", so I would remove the fact.fact in the message, and just have 'Unused fact'

offset: [beginPosition, endPosition],
severity: 'WARNING',
source: fact.fact,
path: path
}]
} else {
return []
}
}
}

export { ModelValidator }
53 changes: 41 additions & 12 deletions test/modelValidator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('The Flint Model validator', () => {

it('should find errors with improperly named acts, facts, duties', async () => {
const model = JSON.stringify({
'acts': [{ 'act': 'test' }, { 'act': '<<test' }, { 'act': '<<act>>' }],
'acts': [{ 'act': 'test' }, { 'act': '<<test' }, { 'act': '<<act>>' }, { 'act': '<<act2>>', 'preconditions': '[fact]' }],
'facts': [{ 'fact': 'test' }, { 'fact': '[test' }, { 'fact': '[fact]' }],
'duties': [{ 'duty': 'test' }, { 'duty': '<test' }, { 'duty': '<duty>' }]
})
Expand All @@ -79,7 +79,7 @@ describe('The Flint Model validator', () => {
'code': 'LR0001',
'source': 'test',
'message': 'Invalid name for identifier',
'offset': [139, 145],
'offset': [183, 189],
'path': [
'duties',
0,
Expand Down Expand Up @@ -258,7 +258,7 @@ describe('The Flint Model validator', () => {

it('should find undefined facts used in acts in fact functions', async () => {
const model = JSON.stringify({
'acts': [],
'acts': [{ 'act': '<<act>>', 'preconditions': '[factname]' }],
'facts': [{ 'fact': '[factname]', 'function': '([cats] OF [dogs]) EN [sunshine]' }],
'duties': []
})
Expand All @@ -271,8 +271,8 @@ describe('The Flint Model validator', () => {
'code': 'LR0002',
'message': 'Undefined item',
'offset': [
54,
60
100,
106
],
'path': [
'facts',
Expand All @@ -286,8 +286,8 @@ describe('The Flint Model validator', () => {
'code': 'LR0002',
'message': 'Undefined item',
'offset': [
64,
70
110,
116
],
'path': [
'facts',
Expand All @@ -301,8 +301,8 @@ describe('The Flint Model validator', () => {
'code': 'LR0002',
'message': 'Undefined item',
'offset': [
75,
85
121,
131
],
'path': [
'facts',
Expand All @@ -318,7 +318,7 @@ describe('The Flint Model validator', () => {

it('should find undefined facts used in lists', async () => {
const model = JSON.stringify({
'acts': [],
'acts': [{ 'act': '<<act>>', 'preconditions': '[factname]' }],
'facts': [{ 'fact': '[factname]',
'function': {
'name': 'SomeList',
Expand All @@ -337,8 +337,8 @@ describe('The Flint Model validator', () => {
'code': 'LR0002',
'message': 'Undefined item',
'offset': [
100,
106
146,
152
],
'path': [
'facts',
Expand All @@ -351,4 +351,33 @@ describe('The Flint Model validator', () => {
}
])
})

it('should find unused facts (without acts relating to them)', async () => {
const model = JSON.stringify({
'acts': [{ 'act': '<<act>>', 'preconditions': '[usedFact]' }],
'facts': [{ 'fact': '[usedFact]' }, { 'fact': '[unusedFact]' }],
'duties': []
})
const modelValidator = new ModelValidator(model)

const errors = modelValidator.getDiagnostics()

expect(errors).to.deep.equal([
{
'code': 'LR0004',
'message': 'Unused fact: [unusedFact]',
'offset': [
96,
110
],
'path': [
'facts',
1,
'fact'
],
'severity': 'WARNING',
'source': '[unusedFact]'
}
])
})
})