Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
feat: Add proper Prettier + ESLint testing in place.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Previously our `eslint --fix` was failing to fix certain cases. This has happened since I moved the `prettier` plugin next to the other plugins within this repositories. That configuration, unlike the initial setup, caused `prettier` to not be able to override the `rules` we explicitly set here. Hence, we needed to remove those explicit sets. I also integrated a test step that makes sure we won't mistakenly add any conflicting rules going forward.
  • Loading branch information
Cansin Yildiz committed Jan 28, 2019
1 parent 6aa49cc commit f60bfb1
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 77 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"scripts": {
"lint": "eslint ./",
"lint-fix": "eslint ./ --fix",
"lint-fix": "eslint --fix ./",
"test": "lerna bootstrap && lerna run build && yarn lint && node tests"
}
}
4 changes: 4 additions & 0 deletions packages/eslint-config-tester/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const path = require('path');
const pathToEslintConfig = path.resolve(__dirname, 'index.js');
const jsSnippetToTest = "const hello = 'world';\nexport default hello;\n";

eslintConfigTester.prettierCheck(
pathToYourEslintConfig,
);

eslintConfigTester.runOnText(
pathToYourEslintConfig,
jsSnippetToTest
Expand Down
9 changes: 9 additions & 0 deletions packages/eslint-config-tester/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const assert = require('assert');
const eslint = require('eslint');
const prettierCheck = require('eslint-config-prettier/bin/cli');

module.exports = {
runOnText(configFile, text) {
Expand All @@ -16,4 +17,12 @@ module.exports = {
`Should have no errors but had ${errorCount}:\n${formatter(report.results)}`,
);
},
prettierCheck(configFile) {
const cli = new eslint.CLIEngine({configFile});
const result = prettierCheck.processString(
JSON.stringify(cli.getConfigForFile('a-random/path.js')),
);

assert.strictEqual(result.code, 0, result.stderr || result.stdout);
},
};
5 changes: 3 additions & 2 deletions packages/eslint-config-tester/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"type": "git",
"url": "https:/udemy/js-tooling.git"
},
"peerDependencies": {
"eslint": "^5.9.0"
"dependencies": {
"eslint": "^5.9.0",
"eslint-config-prettier": "^3.6.0"
},
"engines": {
"node": ">=8.12.0",
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config-udemy-babel-addons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"url": "https:/udemy/js-tooling.git"
},
"dependencies": {
"eslint-config-prettier": "^3.6.0",
"eslint-plugin-babel": "^5.2.1",
"eslint-plugin-import": "^2.14.0"
},
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-config-udemy-babel-addons/parts/prettier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
extends: ['prettier/babel'],
};
2 changes: 2 additions & 0 deletions packages/eslint-config-udemy-babel-addons/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const path = require('path');
// eslint-disable-next-line no-console
console.log('test eslint-config-udemy-babel-addons');

eslintConfigTester.prettierCheck(path.resolve(__dirname, 'index.js'));

eslintConfigTester.runOnText(
path.resolve(__dirname, 'index.js'),
// This is a minimal JS code, just to make sure config has no errors.
Expand Down
1 change: 0 additions & 1 deletion packages/eslint-config-udemy-basics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"url": "https:/udemy/js-tooling.git"
},
"dependencies": {
"eslint-config-prettier": "^3.6.0",
"eslint-import-resolver-node": "^0.3.2",
"eslint-plugin-filenames": "^1.3.2",
"eslint-plugin-import-order-alphabetical": "^0.0.1",
Expand Down
6 changes: 1 addition & 5 deletions packages/eslint-config-udemy-basics/parts/prettier.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
'use strict';

module.exports = {
extends: ['prettier'],
plugins: ['prettier'],
rules: {
'prettier/prettier': 'error',
},
extends: ['plugin:prettier/recommended'],
parserOptions: {
ecmaVersion: 2017,
},
Expand Down
56 changes: 0 additions & 56 deletions packages/eslint-config-udemy-basics/parts/stylistic-issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,10 @@
module.exports = {
plugins: ['import-order-alphabetical'],
rules: {
// enforce spacing inside array brackets
'array-bracket-spacing': ['error', 'never'],
// enforce spacing inside single-line blocks
'block-spacing': ['error', 'always'],
// enforce one true brace style
'brace-style': ['error', '1tbs', {allowSingleLine: true}],
// require camel case names
camelcase: ['error', {properties: 'never'}],
// allow trailing commas in multiline object literals
'comma-dangle': ['error', 'always-multiline'],
// enforce spacing after comma
'comma-spacing': 'error',
// enforce one true comma style
'comma-style': ['error', 'last'],
// disallow padding inside computed properties
'computed-property-spacing': ['error', 'never'],
// enforce braces for all control statements
curly: ['error', 'all'],
// enforce at least one newline at the end of files
'eol-last': ['error', 'always'],
// disallow space between function identifier and application
'func-call-spacing': 'error',
// enforce a convention in the order of require() and import statements with alphabetical sorting
'import-order-alphabetical/order': [
'error',
Expand All @@ -33,67 +15,29 @@ module.exports = {
groups: [['builtin', 'external'], 'internal', ['parent', 'sibling', 'index']],
},
],
// this option sets a specific tab width for your code
indent: ['error', 4, {SwitchCase: 1, VariableDeclarator: 1}],
// require a space before & after certain keywords
'keyword-spacing': ['error', {before: true, after: true}],
// enforces spacing between keys and values in object literal properties
'key-spacing': ['error', {beforeColon: false, afterColon: true}],
// enforce consistent linebreak style
'linebreak-style': ['error', 'unix'],
// specify the maximum number of statement allowed in a function
'max-statements': ['error', 40],
// require a capital letter for constructors
'new-cap': ['error', {newIsCap: true, capIsNew: true, properties: false}],
// require parentheses when invoking a constructor with no arguments
'new-parens': 'error',
// disallow use of the Array constructor
'no-array-constructor': 'error',
// disallow if as the only statement in an else block
'no-lonely-if': 'error',
// disallow mixed spaces and tabs for indentation
'no-mixed-spaces-and-tabs': 'error',
// disallow multiple empty lines and only one newline at the end
'no-multiple-empty-lines': ['error', {max: 1, maxEOF: 1}],
// disallow use of the Object constructor
'no-new-object': 'error',
// disallow tabs
'no-tabs': 'error',
// disallow trailing whitespace at the end of lines
'no-trailing-spaces': 'error',
// disallow the use of boolean literals in conditional expressions and prefer `a || b` over `a ? a : b`
'no-unneeded-ternary': ['error', {defaultAssignment: false}],
// disallow whitespace before properties
'no-whitespace-before-property': 'error',
// enforce the location of single-line statements
'nonblock-statement-body-position': ['error', 'beside'],
// require newlines around variable declarations with initializations
'one-var-declaration-per-line': ['error', 'initializations'],
// enforce padding within blocks
'padded-blocks': ['error', 'never'],
// specify whether double or single quotes should be used
quotes: ['error', 'single', 'avoid-escape'],
// require or disallow use of semicolons instead of ASI
semi: ['error', 'always'],
// enforce spacing before and after semicolons
'semi-spacing': 'error',
// require or disallow a space immediately following the // or /* in a comment
'spaced-comment': [
'error',
'always',
{line: {exceptions: ['/']}, block: {exceptions: ['*']}},
],
// require or disallow space before blocks
'space-before-blocks': 'error',
// require or disallow space before function opening parenthesis
'space-before-function-paren': ['error', {anonymous: 'always', named: 'never'}],
// require or disallow spaces inside parentheses
'space-in-parens': 'error',
// require spaces around operators
'space-infix-ops': 'error',
// Require or disallow spaces before/after unary operators
'space-unary-ops': 'error',
// require or disallow the Unicode Byte Order Mark
'unicode-bom': ['error', 'never'],
},
};
2 changes: 2 additions & 0 deletions packages/eslint-config-udemy-basics/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const path = require('path');
// eslint-disable-next-line no-console
console.log('test eslint-config-udemy-basics');

eslintConfigTester.prettierCheck(path.resolve(__dirname, 'index.js'));

eslintConfigTester.runOnText(
path.resolve(__dirname, 'index.js'),
// This is a minimal JS code, just to make sure config has no errors.
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-config-udemy-jasmine-addons/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const path = require('path');
// eslint-disable-next-line no-console
console.log('test eslint-config-udemy-jasmine-addons');

eslintConfigTester.prettierCheck(path.resolve(__dirname, 'index.js'));

eslintConfigTester.runOnText(
path.resolve(__dirname, 'index.js'),
// This is a minimal JS code, just to make sure config has no errors.
Expand Down
12 changes: 0 additions & 12 deletions packages/eslint-config-udemy-react-addons/parts/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
module.exports = {
plugins: ['react'],
rules: {
// specify whether double or single quotes should be used in JSX attributes
'jsx-quotes': ['error', 'prefer-double'],
// disallow <img> tags in JSX
'react/forbid-elements': ['error', {forbid: ['img']}],
// enforce boolean attributes notation in JSX
'react/jsx-boolean-value': ['error', 'never'],
// enforce or disallow spaces inside of curly braces in JSX attributes
'react/jsx-curly-spacing': ['error', {when: 'always', children: true}],
// prevent usage of .bind() in JSX props
'react/jsx-no-bind': [
'error',
Expand All @@ -24,8 +20,6 @@ module.exports = {
'react/jsx-no-undef': 'error',
// enforce PascalCase for user-defined JSX components
'react/jsx-pascal-case': ['error', {allowAllCaps: true, ignore: []}],
// enforce spaces before the closing bracket of self-closing JSX elements
'react/jsx-tag-spacing': ['error', {beforeSelfClosing: 'always'}],
// prevent React to be incorrectly marked as unused
'react/jsx-uses-react': 'error',
// prevent variables used in JSX to be incorrectly marked as unused
Expand Down Expand Up @@ -69,12 +63,6 @@ module.exports = {
'react/sort-comp': 'error',
// enforce style prop value being an object
'react/style-prop-object': 'error',
// prevent missing parentheses around multilines JSX
'react/jsx-wrap-multilines': ['error', {declaration: true, assignment: true, return: true}],
// enforce spacing around jsx equals signs
'react/jsx-equals-spacing': ['error', 'never'],
// enforce JSX indentation
'react/jsx-indent': ['error', 4],
// disallow target="_blank" on links
'react/jsx-no-target-blank': 'error',
// only .jsx files may have JSX
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-config-udemy-react-addons/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const path = require('path');
// eslint-disable-next-line no-console
console.log('test eslint-config-udemy-react-addons');

eslintConfigTester.prettierCheck(path.resolve(__dirname, 'index.js'));

eslintConfigTester.runOnText(
path.resolve(__dirname, 'index.js'),
// This is a minimal JS code, just to make sure config has no errors.
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-config-udemy-website/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const path = require('path');
// eslint-disable-next-line no-console
console.log('test eslint-config-udemy-website');

eslintConfigTester.prettierCheck(path.resolve(__dirname, 'index.js'));

eslintConfigTester.runOnText(
path.resolve(__dirname, 'index.js'),
// This is a minimal JS code, just to make sure config has no errors.
Expand Down

0 comments on commit f60bfb1

Please sign in to comment.