Skip to content
Closed
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
6 changes: 3 additions & 3 deletions lib/common/identifier-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ function match(text, regex) {

module.exports = {
isMixedCase(text) {
return match(text, /[_]*[a-z]+[a-zA-Z0-9$]*[_]?/)
return match(text, /[_]*[a-z$]+[a-zA-Z0-9$]*[_]?/)
},

isNotMixedCase(text) {
return !this.isMixedCase(text)
},

isCamelCase(text) {
return match(text, /[A-Z]+[a-zA-Z0-9$]*/)
return match(text, /[A-Z$]+[a-zA-Z0-9$]*/)
},

isNotCamelCase(text) {
return !this.isCamelCase(text)
},

isUpperSnakeCase(text) {
return match(text, /_{0,2}[A-Z0-9]+[_A-Z0-9]*/)
return match(text, /_{0,2}[A-Z0-9$]+[_A-Z0-9$]*/)
},

isNotUpperSnakeCase(text) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/naming/contract-name-camelcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const meta = {
type: 'naming',

docs: {
description: 'Contract name must be in CamelCase.',
description: 'Contract, struct and enum names must be in CamelCase.',
category: 'Style Guide Rules',
},

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/naming/func-param-name-mixedcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const meta = {
type: 'naming',

docs: {
description: 'Function param name must be in mixedCase.',
description: 'Functions, events and params names must be in mixedCase',
category: 'Style Guide Rules',
},

Expand Down
25 changes: 21 additions & 4 deletions test/rules/naming/const-name-snakecase.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ describe('Linter - const-name-snakecase', () => {
assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('SNAKE_CASE'))
})

it('should not raise const name error for immutable variables in SNAKE_CASE', () => {
it('should not raise const name error for immutable variables in mixedCase', () => {
const code = contractWith('uint32 private immutable SNAKE_CASE;')

const report = linter.processStr(code, {
Expand All @@ -64,8 +64,25 @@ describe('Linter - const-name-snakecase', () => {

assert.equal(report.errorCount, 0)
})

it('should not raise const name error for immutable variables in mixedCase', () => {

describe('with $ character', () => {
const WITH_$ = {
$: contractWith('uint32 private constant $ = 10;'),
'starting with $': contractWith('uint32 private constant $THE_CONSTANT = 10;'),
'containing a $': contractWith('uint32 private constant THE_$_CONSTANT = 10;'),
'ending with $': contractWith('uint32 private constant THE_CONSTANT$ = 10;'),
}

for (const [key, code] of Object.entries(WITH_$)) {
it(`should not raise event name error for events ${key}`, () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

seems this description is not ok, right ?
Don't worry I'll fix... just wanted to make sure

Copy link
Author

Choose a reason for hiding this comment

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

You're correct, it had to be:

Suggested change
it(`should not raise event name error for events ${key}`, () => {
it(`should not raise event name error for constants ${key}`, () => {

const report = linter.processStr(code, {
rules: { 'const-name-snakecase': 'error' },
})

assert.equal(report.errorCount, 0)
})
}
it('should not raise const name error for immutable variables in SNAKE_CASE', () => {
const code = contractWith('uint32 private immutable SNAKE_CASE;')

const report = linter.processStr(code, {
Expand Down
129 changes: 111 additions & 18 deletions test/rules/naming/contract-name-camelcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,129 @@ const linter = require('../../../lib/index')
const contractWith = require('../../common/contract-builder').contractWith

describe('Linter - contract-name-camelcase', () => {
it('should raise struct name error', () => {
const code = contractWith('struct a {}')
describe('in structs', () => {
it('should raise contract name error', () => {
const code = contractWith('struct a {}')

const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
})

assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('CamelCase'))
})

it('should not raise contract name error', () => {
const code = contractWith('struct MyStruct {}')

const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
})

assert.equal(report.errorCount, 0)
})

assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('CamelCase'))
describe('with $ character', () => {
const WITH_$ = {
$: contractWith('struct $ {}'),
'starting with $': contractWith('struct $MyStruct {}'),
'containing a $': contractWith('struct My$Struct {}'),
'ending with $': contractWith('struct MyStruct$ {}'),
}

for (const [key, code] of Object.entries(WITH_$)) {
it(`should not raise contract name error for structs ${key}`, () => {
const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
})

assert.equal(report.errorCount, 0)
})
}
})
})

it('should raise contract name error', () => {
const code = 'contract a {}'
describe('in contracts', () => {
it('should raise contract name error', () => {
const code = 'contract a {}'

const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
})

assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('CamelCase'))
})

it('should not raise contract name error', () => {
const code = 'contract MyContract {}'

const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
})

assert.equal(report.errorCount, 0)
})

assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('CamelCase'))
describe('with $ character', () => {
const WITH_$ = {
$: 'contract $ {}',
'starting with $': 'contract $MyContract {}',
'containing a $': 'contract My$Contract {}',
'ending with $': 'contract MyContract$ {}',
}

for (const [key, code] of Object.entries(WITH_$)) {
it(`should not raise contract name error for contracts ${key}`, () => {
const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
})

assert.equal(report.errorCount, 0)
})
}
})
})

it('should raise enum name error', () => {
const code = contractWith('enum abc {}')
describe('in enums', () => {
it('should raise contract name error', () => {
const code = contractWith('enum abc {}')

const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
})

assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('CamelCase'))
})

it('should not raise contract name error', () => {
const code = contractWith('enum MyEnum {}')

const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
})

const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
assert.equal(report.errorCount, 0)
})

assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('CamelCase'))
describe('with $ character', () => {
const WITH_$ = {
$: contractWith('enum $ {}'),
'starting with $': contractWith('enum $MyEnum {}'),
'containing a $': contractWith('enum My$Enum {}'),
'ending with $': contractWith('enum MyEnum$ {}'),
}

for (const [key, code] of Object.entries(WITH_$)) {
it(`should not raise contract name error for structs ${key}`, () => {
const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
})

assert.equal(report.errorCount, 0)
})
}
})
})
})
29 changes: 29 additions & 0 deletions test/rules/naming/event-name-camelcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,33 @@ describe('Linter - event-name-camelcase', () => {
assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('CamelCase'))
})

it('should not raise event name error for event in camelCase', () => {
const code = contractWith('event Event1(uint a);')

const report = linter.processStr(code, {
rules: { 'event-name-camelcase': 'error' },
})

assert.equal(report.errorCount, 0)
})

describe('with $ character', () => {
const WITH_$ = {
$: contractWith('event $(uint a);'),
'starting with $': contractWith('event $Event1(uint a);'),
'containing a $': contractWith('event Eve$nt1(uint a);'),
'ending with $': contractWith('event Event1$(uint a);'),
}

for (const [key, code] of Object.entries(WITH_$)) {
it(`should not raise event name error for events ${key}`, () => {
const report = linter.processStr(code, {
rules: { 'contract-name-camelcase': 'error' },
Copy link
Collaborator

Choose a reason for hiding this comment

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

this one should be event-name-camelcase, right ? (I fix)

Copy link
Author

Choose a reason for hiding this comment

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

Correct, it should be:

Suggested change
rules: { 'contract-name-camelcase': 'error' },
rules: { 'event-name-camelcase': 'error' },

})

assert.equal(report.errorCount, 0)
})
}
})
})
21 changes: 20 additions & 1 deletion test/rules/naming/func-name-mixedcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Linter - func-name-mixedcase', () => {
assert.ok(report.messages[0].message.includes('mixedCase'))
})

it('should dot raise incorrect func name error', () => {
it('should not raise incorrect func name error', () => {
const code = contractWith('function aFunc1Nam23e () public {}')

const report = linter.processStr(code, {
Expand All @@ -23,4 +23,23 @@ describe('Linter - func-name-mixedcase', () => {

assert.equal(report.errorCount, 0)
})

describe('with $ character', () => {
const WITH_$ = {
$: contractWith('function $ () public {}'),
'starting with $': contractWith('function $aFunc1Nam23e () public {}'),
'containing a $': contractWith('function aFunc$1Nam23e () public {}'),
'ending with $': contractWith('function aFunc1Nam23e$ () public {}'),
}

for (const [key, code] of Object.entries(WITH_$)) {
it(`should not raise func name error for functions ${key}`, () => {
const report = linter.processStr(code, {
rules: { 'func-name-mixedcase': 'error' },
})

assert.equal(report.errorCount, 0)
})
}
})
})
86 changes: 74 additions & 12 deletions test/rules/naming/func-param-name-mixedcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,87 @@ const linter = require('../../../lib/index')
const contractWith = require('../../common/contract-builder').contractWith

describe('Linter - func-param-name-mixedcase', () => {
it('should raise incorrect func param name error', () => {
const code = contractWith('function funcName (uint A) public {}')
describe('in functions', () => {
it('should raise incorrect func param name error', () => {
const code = contractWith('function funcName (uint A) public {}')

const report = linter.processStr(code, {
rules: { 'func-param-name-mixedcase': 'error' },
const report = linter.processStr(code, {
rules: { 'func-param-name-mixedcase': 'error' },
})

assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('param'))
})

it('should not raise incorrect func param name error', () => {
const code = contractWith('function funcName (uint someParam) public {}')

const report = linter.processStr(code, {
rules: { 'func-param-name-mixedcase': 'error' },
})

assert.equal(report.errorCount, 0)
})

assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('param'))
describe('with $ character', () => {
const WITH_$ = {
$: contractWith('function funcName (uint $) public {}'),
'starting with $': contractWith('function funcName (uint $param) public {}'),
'containing a $': contractWith('function funcName (uint pa$ram) public {}'),
'ending with $': contractWith('function funcName (uint param$) public {}'),
}

for (const [key, code] of Object.entries(WITH_$)) {
it(`should not raise func param name error for parameters ${key}`, () => {
const report = linter.processStr(code, {
rules: { 'func-param-name-mixedcase': 'error' },
})

assert.equal(report.errorCount, 0)
})
}
})
})

it('should raise var name error for event arguments illegal styling', () => {
const code = contractWith('event Event1(uint B);')
describe('in events', () => {
it('should raise var name error for event arguments illegal styling', () => {
const code = contractWith('event Event1(uint B);')

const report = linter.processStr(code, {
rules: { 'func-param-name-mixedcase': 'error' },
const report = linter.processStr(code, {
rules: { 'func-param-name-mixedcase': 'error' },
})

assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('mixedCase'))
})

it('should not raise incorrect func param name error', () => {
const code = contractWith('event Event1(uint someParam);')

const report = linter.processStr(code, {
rules: { 'func-param-name-mixedcase': 'error' },
})

assert.equal(report.errorCount, 0)
})

assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('mixedCase'))
describe('with $ character', () => {
const WITH_$ = {
$: contractWith('event Event1(uint $);'),
'starting with $': contractWith('event Event1(uint $param);'),
'containing a $': contractWith('event Event1(uint pa$ram);'),
'ending with $': contractWith('event Event1(uint param$);'),
}

for (const [key, code] of Object.entries(WITH_$)) {
it(`should not raise func name error for functions ${key}`, () => {
Copy link
Collaborator

@dbale-altoros dbale-altoros Aug 1, 2023

Choose a reason for hiding this comment

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

for Event Parameters
(wrong description on IT statement)

Copy link
Author

Choose a reason for hiding this comment

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

Suggested change
it(`should not raise func name error for functions ${key}`, () => {
it(`should not raise func name error for event parameters ${key}`, () => {

const report = linter.processStr(code, {
rules: { 'func-name-mixedcase': 'error' },
})

assert.equal(report.errorCount, 0)
})
}
})
})
})
Loading