Skip to content

Commit

Permalink
Merge pull request #465 from protofire/p453-allow-moneySymbol-in-names
Browse files Browse the repository at this point in the history
P453 allow $ symbol as part of the name
  • Loading branch information
dbale-altoros authored Aug 3, 2023
2 parents 51b59c9 + d92be4e commit 121a59f
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 48 deletions.
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
19 changes: 19 additions & 0 deletions test/rules/naming/const-name-snakecase.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,23 @@ describe('Linter - const-name-snakecase', () => {

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

describe('constant name with $ character', () => {
const WITH_$ = {
'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;'),
'only with $': contractWith('uint32 private constant $ = 10;'),
}

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

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

describe('Struct name with $ character', () => {
const WITH_$ = {
'starting with $': contractWith('struct $MyStruct {}'),
'containing a $': contractWith('struct My$Struct {}'),
'ending with $': contractWith('struct MyStruct$ {}'),
'only with $': contractWith('struct $ {}'),
}

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)
})
}
})

describe('Enums name with $ character', () => {
const WITH_$ = {
'starting with $': contractWith('enum $MyEnum {}'),
'containing a $': contractWith('enum My$Enum {}'),
'ending with $': contractWith('enum MyEnum$ {}'),
'only with $': contractWith('enum $ {}'),
}

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

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

describe('Contract name with $ character', () => {
const WITH_$ = {
'starting with $': 'contract $MyContract {}',
'containing a $': 'contract My$Contract {}',
'ending with $': 'contract MyContract$ {}',
'only with $': 'contract $ {}',
}

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)
})
}
})
})
19 changes: 19 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,23 @@ describe('Linter - event-name-camelcase', () => {
assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('CamelCase'))
})

describe('Event name with $ character', () => {
const WITH_$ = {
'starting with $': contractWith('event $Event1(uint a);'),
'containing a $': contractWith('event Eve$nt1(uint a);'),
'ending with $': contractWith('event Event1$(uint a);'),
'only with $': contractWith('event $(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: { 'event-name-camelcase': 'error' },
})

assert.equal(report.errorCount, 0)
})
}
})
})
19 changes: 19 additions & 0 deletions test/rules/naming/func-name-mixedcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,23 @@ describe('Linter - func-name-mixedcase', () => {

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

describe('Function names with $ character', () => {
const WITH_$ = {
'starting with $': contractWith('function $aFunc1Nam23e () public {}'),
'containing a $': contractWith('function aFunc$1Nam23e () public {}'),
'ending with $': contractWith('function aFunc1Nam23e$ () public {}'),
'only with $': contractWith('function $() 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)
})
}
})
})
37 changes: 37 additions & 0 deletions test/rules/naming/func-param-name-mixedcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,41 @@ describe('Linter - func-param-name-mixedcase', () => {
assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('mixedCase'))
})

describe('Function Paramters name with $ character', () => {
const WITH_$ = {
'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 {}'),
'only with $': contractWith('function funcName(uint $) public {}'),
}

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

assert.equal(report.errorCount, 0)
})
}
})
describe('Event parameters name with $ character', () => {
const WITH_$ = {
'starting with $': contractWith('event Event1(uint $param);'),
'containing a $': contractWith('event Event1(uint pa$ram);'),
'ending with $': contractWith('event Event1(uint param$);'),
'only with $': contractWith('event Event1(uint $);'),
}

for (const [key, code] of Object.entries(WITH_$)) {
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)
})
}
})
})
80 changes: 35 additions & 45 deletions test/rules/naming/immutable-vars-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,53 +65,43 @@ describe('immutable-vars-naming', () => {
assert.equal(report.errorCount, 0)
})

describe('immutable-vars-naming ==> warnings (same as above)', () => {
it('should raise warning when immutablesAsConstants = false and variable is in snake case', () => {
const code = contractWith('uint32 private immutable SNAKE_CASE;')

const report = linter.processStr(code, {
rules: { 'immutable-vars-naming': ['warn', { immutablesAsConstants: false }] },
})

assert.equal(report.warningCount, 1)
assert.ok(
report.messages[0].message.includes('Immutable variables names are set to be in mixedCase')
)
})

it('should NOT raise warning when immutablesAsConstants = false and variable is in snake case', () => {
const code = contractWith('uint32 private immutable SNAKE_CASE;')

const report = linter.processStr(code, {
rules: { 'immutable-vars-naming': ['warn', { immutablesAsConstants: true }] },
describe('Immutable variable with $ character as mixedCase', () => {
const WITH_$ = {
'starting with $': contractWith('uint32 immutable private $D;'),
'containing a $': contractWith('uint32 immutable private testWith$Contained;'),
'ending with $': contractWith('uint32 immutable private testWithEnding$;'),
'only with $': contractWith('uint32 immutable private $;'),
}

for (const [key, code] of Object.entries(WITH_$)) {
it(`should not raise immutable error for variables ${key}`, () => {
const report = linter.processStr(code, {
rules: { 'immutable-vars-naming': ['error', { immutablesAsConstants: false }] },
})

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

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

it('should raise warning when immutablesAsConstants = true and variable is in mixedCase', () => {
const code = contractWith('uint32 private immutable mixedCase;')

const report = linter.processStr(code, {
rules: { 'immutable-vars-naming': ['warn', { immutablesAsConstants: true }] },
})

assert.equal(report.warningCount, 1)
assert.ok(
report.messages[0].message.includes(
'Immutable variables name are set to be in capitalized SNAKE_CASE'
)
)
})

it('should NOT raise warning when immutablesAsConstants = false and variable is in mixedCase', () => {
const code = contractWith('uint32 private immutable mixedCase;')

const report = linter.processStr(code, {
rules: { 'immutable-vars-naming': ['warn', { immutablesAsConstants: false }] },
describe('Immutable variable with $ character as SNAKE_CASE', () => {
const WITH_$ = {
'starting with $': contractWith('uint32 immutable private $_D;'),
'starting with $D': contractWith('uint32 immutable private $D;'),
'containing a $': contractWith('uint32 immutable private TEST_WITH_$_CONTAINED;'),
'ending with $': contractWith('uint32 immutable private TEST_WITH_ENDING_$;'),
'ending with D$': contractWith('uint32 immutable private TEST_WITH_ENDING_D$;'),
'only with $': contractWith('uint32 immutable private $;'),
}

for (const [key, code] of Object.entries(WITH_$)) {
it(`should not raise immutable error for variables ${key}`, () => {
const report = linter.processStr(code, {
rules: { 'immutable-vars-naming': ['error', { immutablesAsConstants: true }] },
})

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

assert.equal(report.warningCount, 0)
})
}
})
})
19 changes: 19 additions & 0 deletions test/rules/naming/modifier-name-mixedcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,23 @@ describe('Linter - modifier-name-mixedcase', () => {

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

describe('with $ character', () => {
const WITH_$ = {
'starting with $': contractWith('modifier $ownedBy(address a) { }'),
'containing a $': contractWith('modifier owned$By(address a) { }'),
'ending with $': contractWith('modifier ownedBy$(address a) { }'),
'only with $': contractWith('modifier $(uint a) { }'),
}

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

assert.equal(report.errorCount, 0)
})
}
})
})
19 changes: 19 additions & 0 deletions test/rules/naming/var-name-mixedcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,23 @@ describe('Linter - var-name-mixedcase', () => {

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

describe('with $ character', () => {
const WITH_$ = {
'starting with $': contractWith('uint32 private $D = 10;'),
'containing a $': contractWith('uint32 private testWith$Contained = 10;'),
'ending with $': contractWith('uint32 private testWithEnding$ = 10;'),
'only with $': contractWith('uint32 private $;'),
}

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

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

0 comments on commit 121a59f

Please sign in to comment.