Skip to content

Commit 6fd7532

Browse files
authored
chore: upgrade @lwc/eslint-plugin-lwc and add rules to recommended preset (#42)
1- Configures lwc/valid-api in the recommended preset, disallowing mixed use of upper case and underscore in API fields and methods. 2- Adds lwc/prefer-custom-event to the recommended preset. 3- Adds lwc/no-dupe-class-members to the recommended preset.
1 parent b6630a2 commit 6fd7532

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"test": "mocha"
1818
},
1919
"dependencies": {
20-
"@lwc/eslint-plugin-lwc": "~0.10.0",
20+
"@lwc/eslint-plugin-lwc": "~0.11.0",
2121
"babel-eslint": "~10.1.0",
2222
"eslint-plugin-import": "~2.22.1",
2323
"eslint-plugin-jest": "~23.8.2",

recommended.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,16 @@ module.exports = {
105105
'@lwc/lwc/no-api-reassignments': 'error',
106106
'@lwc/lwc/no-async-operation': 'error',
107107
'@lwc/lwc/no-document-query': 'error',
108+
'@lwc/lwc/no-dupe-class-members': 'error',
108109
'@lwc/lwc/no-inner-html': 'error',
109110
'@lwc/lwc/no-leading-uppercase-api-name': 'error',
111+
'@lwc/lwc/prefer-custom-event': 'error',
112+
'@lwc/lwc/valid-api': [
113+
'error',
114+
{
115+
disallowUnderscoreUppercaseMix: true,
116+
},
117+
],
110118

111119
// Disable unresolved import rule since it doesn't work well with the way the LWC compiler
112120
// resolves the different modules

test/recommended.js

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ const eslint = require('eslint');
1111

1212
const { linkConfig, unlinkConfig } = require('./utils');
1313

14+
function getCliEngineWithRecommendedRules() {
15+
return new eslint.CLIEngine({
16+
useEslintrc: false,
17+
baseConfig: {
18+
extends: '@salesforce/eslint-config-lwc/recommended',
19+
},
20+
});
21+
}
22+
1423
describe('recommended config', () => {
1524
before(() => {
1625
linkConfig();
@@ -21,17 +30,57 @@ describe('recommended config', () => {
2130
});
2231

2332
it('should load properly recommended config', () => {
24-
const cli = new eslint.CLIEngine({
25-
useEslintrc: false,
26-
baseConfig: {
27-
extends: '@salesforce/eslint-config-lwc/recommended',
28-
},
29-
});
33+
const cli = getCliEngineWithRecommendedRules();
3034

3135
const report = cli.executeOnText('document.querySelectorAll("a")');
3236

3337
const { messages } = report.results[0];
3438
assert.equal(messages.length, 1);
3539
assert.equal(messages[0].ruleId, '@lwc/lwc/no-document-query');
3640
});
41+
42+
it('should forbid mixing uppercase and underscore characters in public properties', () => {
43+
const cli = getCliEngineWithRecommendedRules();
44+
45+
const report = cli.executeOnText(`
46+
import { LightningElement, api } from 'lwc';
47+
export default class Foo extends LightningElement {
48+
@api bar_Foo() {}
49+
}
50+
`);
51+
52+
const { messages } = report.results[0];
53+
assert.equal(messages.length, 1);
54+
assert.equal(messages[0].ruleId, '@lwc/lwc/valid-api');
55+
});
56+
57+
it('should suggest usage of CustomEvent over Event constructor', () => {
58+
const cli = getCliEngineWithRecommendedRules();
59+
60+
const report = cli.executeOnText(`dispatchEvent(new Event('test'));`);
61+
62+
const { messages } = report.results[0];
63+
assert.equal(messages.length, 1);
64+
assert.equal(messages[0].ruleId, '@lwc/lwc/prefer-custom-event');
65+
});
66+
67+
it('should forbid duplicate class members', () => {
68+
const cli = getCliEngineWithRecommendedRules();
69+
70+
const report = cli.executeOnText(`
71+
import { LightningElement, api } from 'lwc';
72+
73+
export default class App extends LightningElement {
74+
@api foo = 1;
75+
76+
set foo(value) { this._foo = value }
77+
get foo() { return this._foo; }
78+
}
79+
`);
80+
81+
const { messages } = report.results[0];
82+
assert.equal(messages.length, 2);
83+
assert.equal(messages[0].ruleId, '@lwc/lwc/no-dupe-class-members');
84+
assert.equal(messages[1].ruleId, '@lwc/lwc/no-dupe-class-members');
85+
});
3786
});

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@
126126
minimatch "^3.0.4"
127127
strip-json-comments "^3.1.1"
128128

129-
"@lwc/eslint-plugin-lwc@~0.10.0":
130-
version "0.10.0"
131-
resolved "https://registry.yarnpkg.com/@lwc/eslint-plugin-lwc/-/eslint-plugin-lwc-0.10.0.tgz#e282910b01b3b403d8fec03d34c4c853e29c4676"
132-
integrity sha512-UsosUow0xWrzzcSK1fWDWHKBT81/pJz1/icfv7w8T+BCE9G4Kb4TwJexHLOIZvY7e/dv4Fjme4lTOWehuNE5Sg==
129+
"@lwc/eslint-plugin-lwc@~0.11.0":
130+
version "0.11.0"
131+
resolved "https://registry.yarnpkg.com/@lwc/eslint-plugin-lwc/-/eslint-plugin-lwc-0.11.0.tgz#1c69cc673b9798e04e87ececb8529b93eb2215b1"
132+
integrity sha512-wJOD4XWOH91GaZfypMSKfEeMXqMfvKdsb2gSJ/9FEwJVlziKg1aagtRYJh2ln3DyEZV33tBC/p/dWzIeiwa1tg==
133133
dependencies:
134134
minimatch "^3.0.4"
135135

0 commit comments

Comments
 (0)