Skip to content

Commit 2dfd721

Browse files
authored
Merge pull request #2290 from onflow/holyfuchs/lint-warnings-as-errors
add --warnings-as-errors flag to flow cadence lint
2 parents b8666eb + 441f2c4 commit 2dfd721

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

internal/cadence/lint.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ import (
3939
"github.com/onflow/flow-cli/internal/util"
4040
)
4141

42-
type lintFlagsCollection struct{}
42+
type lintFlagsCollection struct {
43+
WarningsAsErrors bool `default:"false" flag:"warnings-as-errors" info:"Treat warnings as errors"`
44+
}
4345

4446
type fileResult struct {
4547
FilePath string
@@ -98,7 +100,7 @@ func lint(
98100
filePaths = args
99101
}
100102

101-
result, err := lintFiles(state, filePaths...)
103+
result, err := lintFiles(state, lintFlags.WarningsAsErrors, filePaths...)
102104
if err != nil {
103105
return nil, err
104106
}
@@ -108,6 +110,7 @@ func lint(
108110

109111
func lintFiles(
110112
state *flowkit.State,
113+
warningsAsErrors bool,
111114
filePaths ...string,
112115
) (
113116
*lintResult,
@@ -140,14 +143,18 @@ func lintFiles(
140143
Diagnostics: diagnostics,
141144
})
142145

143-
// Set the exitCode to 1 if any of the diagnostics are error-level
144-
// In the future, this may be configurable
146+
// Set the exitCode to 1 if any of the diagnostics are error-level,
147+
// or warning-level when warningsAsErrors is set.
145148
for _, diagnostic := range diagnostics {
146149
severity := getDiagnosticSeverity(diagnostic)
147150
if severity == errorSeverity {
148151
exitCode = 1
149152
break
150153
}
154+
if severity == warningSeverity && warningsAsErrors {
155+
exitCode = 1
156+
break
157+
}
151158
}
152159
}
153160

internal/cadence/lint_test.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func Test_Lint(t *testing.T) {
4949

5050
state := setupMockState(t)
5151

52-
results, err := lintFiles(state, "NoError.cdc")
52+
results, err := lintFiles(state, false, "NoError.cdc")
5353
require.NoError(t, err)
5454

5555
require.Equal(t,
@@ -71,7 +71,7 @@ func Test_Lint(t *testing.T) {
7171

7272
state := setupMockState(t)
7373

74-
results, err := lintFiles(state, "foo/WithImports.cdc")
74+
results, err := lintFiles(state, false, "foo/WithImports.cdc")
7575
require.NoError(t, err)
7676

7777
// Should not have results for imported file, only for the file being linted
@@ -94,7 +94,7 @@ func Test_Lint(t *testing.T) {
9494

9595
state := setupMockState(t)
9696

97-
results, err := lintFiles(state, "NoError.cdc", "foo/WithImports.cdc")
97+
results, err := lintFiles(state, false, "NoError.cdc", "foo/WithImports.cdc")
9898
require.NoError(t, err)
9999

100100
require.Equal(t,
@@ -120,7 +120,7 @@ func Test_Lint(t *testing.T) {
120120

121121
state := setupMockState(t)
122122

123-
results, err := lintFiles(state, "LintWarning.cdc")
123+
results, err := lintFiles(state, false, "LintWarning.cdc")
124124
require.NoError(t, err)
125125

126126
require.Equal(t,
@@ -147,12 +147,26 @@ func Test_Lint(t *testing.T) {
147147
)
148148
})
149149

150+
t.Run("warnings as errors: exits 0 without flag, exits 1 with flag", func(t *testing.T) {
151+
t.Parallel()
152+
153+
state := setupMockState(t)
154+
155+
results, err := lintFiles(state, false, "LintWarning.cdc")
156+
require.NoError(t, err)
157+
require.Equal(t, 0, results.exitCode)
158+
159+
results, err = lintFiles(state, true, "LintWarning.cdc")
160+
require.NoError(t, err)
161+
require.Equal(t, 1, results.exitCode)
162+
})
163+
150164
t.Run("lints file with error", func(t *testing.T) {
151165
t.Parallel()
152166

153167
state := setupMockState(t)
154168

155-
results, err := lintFiles(state, "LintError.cdc")
169+
results, err := lintFiles(state, false, "LintError.cdc")
156170
require.NoError(t, err)
157171

158172
require.Equal(t,
@@ -194,7 +208,7 @@ func Test_Lint(t *testing.T) {
194208

195209
state := setupMockState(t)
196210

197-
results, err := lintFiles(state, "ReplacementHint.cdc")
211+
results, err := lintFiles(state, false, "ReplacementHint.cdc")
198212
require.NoError(t, err)
199213

200214
require.Len(t, results.Results, 1)
@@ -221,7 +235,7 @@ func Test_Lint(t *testing.T) {
221235

222236
state := setupMockState(t)
223237

224-
results, err := lintFiles(state, "WithFlowkitImport.cdc")
238+
results, err := lintFiles(state, false, "WithFlowkitImport.cdc")
225239
require.NoError(t, err)
226240

227241
require.Equal(t,
@@ -243,7 +257,7 @@ func Test_Lint(t *testing.T) {
243257

244258
state := setupMockState(t)
245259

246-
results, err := lintFiles(state, "StdlibImportsContract.cdc")
260+
results, err := lintFiles(state, false, "StdlibImportsContract.cdc")
247261
require.NoError(t, err)
248262

249263
// Expects an error because getAuthAccount is only available in scripts
@@ -277,7 +291,7 @@ func Test_Lint(t *testing.T) {
277291

278292
state := setupMockState(t)
279293

280-
results, err := lintFiles(state, "StdlibImportsTransaction.cdc")
294+
results, err := lintFiles(state, false, "StdlibImportsTransaction.cdc")
281295
require.NoError(t, err)
282296

283297
// Expects an error because getAuthAccount is only available in scripts
@@ -311,7 +325,7 @@ func Test_Lint(t *testing.T) {
311325

312326
state := setupMockState(t)
313327

314-
results, err := lintFiles(state, "StdlibImportsScript.cdc")
328+
results, err := lintFiles(state, false, "StdlibImportsScript.cdc")
315329
require.NoError(t, err)
316330

317331
require.Equal(t,
@@ -333,7 +347,7 @@ func Test_Lint(t *testing.T) {
333347

334348
state := setupMockState(t)
335349

336-
results, err := lintFiles(state, "StdlibImportsCrypto.cdc")
350+
results, err := lintFiles(state, false, "StdlibImportsCrypto.cdc")
337351
require.NoError(t, err)
338352

339353
require.Equal(t,
@@ -355,7 +369,7 @@ func Test_Lint(t *testing.T) {
355369

356370
state := setupMockState(t)
357371

358-
results, err := lintFiles(state, "TransactionImportingContractWithNestedImports.cdc")
372+
results, err := lintFiles(state, false, "TransactionImportingContractWithNestedImports.cdc")
359373
require.NoError(t, err)
360374

361375
require.Equal(t,
@@ -377,7 +391,7 @@ func Test_Lint(t *testing.T) {
377391

378392
state := setupMockStateWithAccountAccess(t)
379393

380-
results, err := lintFiles(state, "ContractA.cdc")
394+
results, err := lintFiles(state, false, "ContractA.cdc")
381395
require.NoError(t, err)
382396

383397
// Should have no errors since ContractA and ContractB are on same account
@@ -400,7 +414,7 @@ func Test_Lint(t *testing.T) {
400414

401415
state := setupMockStateWithAccountAccess(t)
402416

403-
results, err := lintFiles(state, "ContractC.cdc")
417+
results, err := lintFiles(state, false, "ContractC.cdc")
404418
require.NoError(t, err)
405419

406420
// Should have error since ContractC and ContractB are on different accounts
@@ -416,7 +430,7 @@ func Test_Lint(t *testing.T) {
416430

417431
state := setupMockStateWithDependencies(t)
418432

419-
results, err := lintFiles(state, "imports/testaddr/DepA.cdc")
433+
results, err := lintFiles(state, false, "imports/testaddr/DepA.cdc")
420434
require.NoError(t, err)
421435

422436
// Should have no errors since DepA and DepB are dependencies on same address
@@ -448,7 +462,7 @@ func Test_Lint(t *testing.T) {
448462
require.NotNil(t, alias, "Alias should be automatically created from Source")
449463
require.Equal(t, "dfc20aee650fcbdf", alias.Address.String(), "Alias address should match Source address")
450464

451-
results, err := lintFiles(state, "imports/testaddr/SourceA.cdc")
465+
results, err := lintFiles(state, false, "imports/testaddr/SourceA.cdc")
452466
require.NoError(t, err)
453467

454468
// Should have no errors since SourceA and SourceB have same Source.Address (converted to Aliases)

0 commit comments

Comments
 (0)