Skip to content

Commit 6a0d083

Browse files
authored
Merge pull request #42 from humanmade/add-examples-and-update-rules
Add ESLint config test script with example fixtures
2 parents 0ea301b + 78ecc60 commit 6a0d083

File tree

8 files changed

+146
-0
lines changed

8 files changed

+146
-0
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@ install:
66
- composer install
77
- npm install
88

9+
# Install for ESLint tests
10+
- cd packages/eslint-config-humanmade
11+
- npm install
12+
13+
# Reset
14+
- cd ../..
15+
916
script:
1017
- vendor/bin/phpunit
18+
- node packages/eslint-config-humanmade/fixtures/test-lint-config
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = tab
6+
end_of_line = lf
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
3+
const World = () => <span>World</span>;
4+
5+
const Hello = () => ( <div>
6+
<p>Hello <World /></p>
7+
</div> );
8+
9+
export default class Test extends React.Component {
10+
render() {
11+
return <div>
12+
<Hello />
13+
</div>;
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let obj = [ 'only assigned once' ];
2+
3+
var str = 'default value';
4+
const str2 = `${ str }, but declared with const`;
5+
if ( obj[0] === global.condition ) {
6+
str2 = 'other value maybe assigned later';
7+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
3+
const World = () => <span>World</span>;
4+
5+
const Hello = () => (
6+
<div>
7+
<p>Hello <World /></p>
8+
</div>
9+
);
10+
11+
export default class Test extends React.Component {
12+
render() {
13+
return (
14+
<div>
15+
<Hello />
16+
</div>
17+
);
18+
}
19+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* eslint-disable no-console */
2+
process.on( 'unhandledRejection', err => {
3+
throw err;
4+
} );
5+
6+
const chalk = require( 'chalk' );
7+
const join = require( 'path' ).join;
8+
const { CLIEngine } = require( 'eslint' );
9+
10+
const cli = new CLIEngine( { useEslintrc: true } );
11+
const formatter = CLIEngine.getFormatter();
12+
13+
const verbose = process.argv.indexOf( '--verbose' ) > -1;
14+
15+
/** Utility to count errors, warnings & processed file count. */
16+
const count = results => results.reduce( ( counts, file ) => ( {
17+
errors: counts.errors + file.errorCount,
18+
warnings: counts.warnings + file.warningCount,
19+
files: counts.files + 1,
20+
} ), {
21+
errors: 0,
22+
warnings: 0,
23+
files: 0,
24+
} );
25+
26+
console.log( 'Running ESLint on fixture directories. Use --verbose for a detailed report.' );
27+
console.log( '\nLinting `fixtures/fail/**`...' );
28+
29+
const antipatternReport = cli.executeOnFiles( [ join( __dirname, 'fail/**' ) ] );
30+
const antipatternCounts = count( antipatternReport.results );
31+
const allFail = antipatternReport.results.reduce( ( didFail, file ) => didFail = didFail && ( file.errorCount > 0 || file.warningCount > 0 ), true );
32+
33+
if ( allFail ) {
34+
console.log( chalk.green( 'ESLint logs errors as expected.\n' ) );
35+
} else if ( antipatternCounts.errors ) {
36+
console.log( chalk.bold.red( 'The following files did not produce errors:' ) );
37+
antipatternReport.results.forEach( file => {
38+
if ( file.errorCount > 0 || file.warningCount > 0 ) {
39+
return;
40+
}
41+
42+
console.log( ' ' + file.filePath );
43+
} );
44+
console.log( '' );
45+
46+
process.exitCode = 1;
47+
} else {
48+
console.log( chalk.bold.red( 'Errors expected, but none encountered!\n' ) );
49+
process.exitCode = 1;
50+
}
51+
52+
// Log full report when --verbose, or when no errors are reported.
53+
if ( verbose || ! antipatternCounts.errors ) {
54+
console.log( formatter( antipatternReport.results ) );
55+
}
56+
57+
console.log( 'Linting `fixtures/pass/**`...' );
58+
59+
const exampleReport = cli.executeOnFiles( [ join( __dirname, 'pass/**' ) ] );
60+
const exampleCounts = count( exampleReport.results );
61+
62+
// Log full report when --verbose, or whenever errors are unexpectedly reported.
63+
if ( verbose || exampleCounts.errors || exampleCounts.warnings ) {
64+
console.log( formatter( exampleReport.results ) );
65+
}
66+
67+
if ( exampleCounts.errors ) {
68+
const { errors } = exampleCounts;
69+
console.log( chalk.bold.red( `${ errors } unexpected error${ errors !== 1 ? 's' : '' }!\n` ) );
70+
process.exitCode = 1;
71+
} else {
72+
const { files } = exampleCounts;
73+
console.log( chalk.green( `${ files } files pass lint.` ) );
74+
}

packages/eslint-config-humanmade/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ module.exports = {
7272
},
7373
} ],
7474
'object-curly-spacing': [ 'error', 'always' ],
75+
'object-property-newline': [ 'error' ],
7576
'quotes': [ 'error', 'single' ],
7677
'semi-spacing': [ 'error', {
7778
'before': false,
@@ -94,5 +95,6 @@ module.exports = {
9495
} ],
9596
'yoda': [ 'error', 'never' ],
9697
'react/jsx-curly-spacing': [ 'error', 'always' ],
98+
'react/jsx-wrap-multilines': [ 'error' ],
9799
},
98100
};

packages/eslint-config-humanmade/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
"type": "git",
66
"url": "https://github.com/humanmade/coding-standards.git"
77
},
8+
"scripts": {
9+
"test": "node fixtures/test-lint-config"
10+
},
11+
"devDependencies": {
12+
"babel-eslint": "^7.2.3",
13+
"chalk": "^2.3.2",
14+
"eslint": "^4.19.0",
15+
"eslint-config-react-app": "^0.5.0",
16+
"eslint-plugin-flowtype": "^2.21.0",
17+
"eslint-plugin-import": "^2.0.1",
18+
"eslint-plugin-jsx-a11y": "^2.2.3",
19+
"eslint-plugin-react": "^6.4.1"
20+
},
821
"peerDependencies": {
922
"babel-eslint": "^7.0.0",
1023
"eslint": "^4.19.0",

0 commit comments

Comments
 (0)