From a3bd485a682ad30e22fd0896f991bcb8adf61288 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 17 May 2017 00:01:15 -0700 Subject: [PATCH 1/9] Add WIP for stylelint integration --- .stylelintrc | 3 +++ check-diff.sh | 50 ++++++++++++++++++++++++++++++++++++++++++++++-- pre-commit | 1 + readme.md | 8 +++++--- travis.script.sh | 1 + 5 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 .stylelintrc diff --git a/.stylelintrc b/.stylelintrc new file mode 100644 index 0000000..58568a7 --- /dev/null +++ b/.stylelintrc @@ -0,0 +1,3 @@ +{ + "extends": "stylelint-config-wordpress" +} \ No newline at end of file diff --git a/check-diff.sh b/check-diff.sh index a9060f0..d604b98 100755 --- a/check-diff.sh +++ b/check-diff.sh @@ -166,6 +166,13 @@ function set_environment_variables { ESLINT_IGNORE="$( upsearch .eslintignore )" fi + if [ -z "$STYLELINT_CONFIG" ]; then + STYLELINT_CONFIG="$( upsearch .stylelintrc )" + fi + if [ -z "$STYLELINT_CONFIG" ]; then + STYLELINT_CONFIG="$( upsearch stylelint.config.js )" + fi + # Load any environment variable overrides from config files ENV_FILE=$( upsearch .ci-env.sh ) if [ ! -z "$ENV_FILE" ]; then @@ -245,7 +252,7 @@ function set_environment_variables { cat "$TEMP_DIRECTORY/paths-scope" | grep -E '\.php(:|$)' | cat - > "$TEMP_DIRECTORY/paths-scope-php" cat "$TEMP_DIRECTORY/paths-scope" | grep -E '\.jsx?(:|$)' | cat - > "$TEMP_DIRECTORY/paths-scope-js" - cat "$TEMP_DIRECTORY/paths-scope" | grep -E '\.(css|scss)(:|$)' | cat - > "$TEMP_DIRECTORY/paths-scope-scss" + cat "$TEMP_DIRECTORY/paths-scope" | grep -E '\.(css|scss)(:|$)' | cat - > "$TEMP_DIRECTORY/paths-scope-css" cat "$TEMP_DIRECTORY/paths-scope" | grep -E '\.(xml|svg|xml.dist)(:|$)' | cat - > "$TEMP_DIRECTORY/paths-scope-xml" # Gather the proper states of files to run through linting (this won't apply to phpunit) @@ -283,7 +290,7 @@ function set_environment_variables { done # Make sure linter configs get copied linting directory since upsearch is relative. - for linter_file in .jshintrc .jshintignore .jscsrc .jscs.json .eslintignore .eslintrc phpcs.ruleset.xml ruleset.xml; do + for linter_file in .jshintrc .jshintignore .jscsrc .jscs.json .eslintignore .eslintrc .stylelintrc stylelint.config.js phpcs.ruleset.xml ruleset.xml; do if git ls-files "$linter_file" --error-unmatch > /dev/null 2>&1; then if [ -L $linter_file ]; then ln -fs $(git show :"$linter_file") "$LINTING_DIRECTORY/$linter_file" @@ -463,6 +470,15 @@ function install_tools { fi fi + # Install stylelint + if [ -n "$STYLELINT_CONFIG" ] && [ -e "$STYLELINT_CONFIG" ] && [ ! -e "$(npm bin)/stylelint" ] && check_should_execute 'stylelint'; then + echo "Installing ESLint" + if ! npm install -g eslint 2>/dev/null; then + echo "Failed to install stylelint (try manually doing: sudo npm install -g stylelint), so skipping stylelint" + DEV_LIB_SKIP="$DEV_LIB_SKIP,stylelint" + fi + fi + # YUI Compressor if [ "$YUI_COMPRESSOR_CHECK" == 1 ] && command -v java >/dev/null 2>&1 && check_should_execute 'yuicompressor'; then if [ ! -e "$YUI_COMPRESSOR_PATH" ]; then @@ -776,6 +792,36 @@ function lint_js_files { fi } + +function lint_css_files { + if [ ! -s "$TEMP_DIRECTORY/paths-scope-css" ]; then + return + fi + + set -e + + # Run ESLint. + if [ -n "$STYLELINT_CONFIG" ] && [ -e "$STYLELINT_CONFIG" ] && [ -e "$(npm bin)/stylelint" ] && check_should_execute 'stylelint'; then + ( + echo "## stylelint" + cd "$LINTING_DIRECTORY" + # TODO: --format=compact is not right. + if ! cat "$TEMP_DIRECTORY/paths-scope-css" | remove_diff_range | xargs "$(npm bin)/stylelint" --format=compact --config="$STYLELINT_CONFIG" > "$TEMP_DIRECTORY/stylelint-report"; then + if [ "$CHECK_SCOPE" == 'patches' ]; then + cat "$TEMP_DIRECTORY/stylelint-report" | php "$DEV_LIB_PATH/diff-tools/filter-report-for-patch-ranges.php" "$TEMP_DIRECTORY/paths-scope-css" | cut -c$( expr ${#LINTING_DIRECTORY} + 2 )- + exit_code="${PIPESTATUS[1]}" + if [[ $exit_code != 0 ]]; then + return $exit_code + fi + elif [ -s "$TEMP_DIRECTORY/stylelint-report" ]; then + cat "$TEMP_DIRECTORY/stylelint-report" | cut -c$( expr ${#LINTING_DIRECTORY} + 2 )- + exit 1 + fi + fi + ) + fi +} + # @todo: This is wrong, as we should be doing `npm test` instead of calling `grunt qunit` directly. function run_qunit { if [ ! -s "$TEMP_DIRECTORY/paths-scope-js" ] || ! check_should_execute 'grunt'; then diff --git a/pre-commit b/pre-commit index ce24531..929878b 100755 --- a/pre-commit +++ b/pre-commit @@ -40,6 +40,7 @@ echo "## Checking files, scope $CHECK_SCOPE:" cat "$TEMP_DIRECTORY/paths-scope" check_execute_bit +lint_css_files lint_js_files lint_php_files lint_xml_files diff --git a/readme.md b/readme.md index 37151b3..d8100ba 100644 --- a/readme.md +++ b/readme.md @@ -27,7 +27,7 @@ To install the pre-commit hook, symlink to [`pre-commit`](pre-commit) from your ./dev-lib/install-pre-commit-hook.sh ``` -Also symlink (or copy) the [`.jshintrc`](.jshint), [`.jshintignore`](.jshintignore), [`.jscsrc`](.jscsrc), [`phpcs.ruleset.xml`](phpcs.ruleset.xml), and [`phpunit-plugin.xml`](phpunit-plugin.xml) (note the PHPUnit config will need its paths modified if it is copied instead of symlinked): +Also symlink (or copy) the desired linting configs and [`phpunit-plugin.xml`](phpunit-plugin.xml) (note the PHPUnit config will need its paths modified if it is copied instead of symlinked): ```bash ln -s dev-lib/phpunit-plugin.xml phpunit.xml.dist && git add phpunit.xml.dist # (if working with a plugin) @@ -35,15 +35,17 @@ ln -s dev-lib/.jshintrc . && git add .jshintrc ln -s dev-lib/.jscsrc . && git add .jscsrc ln -s dev-lib/.eslintrc . && git add .eslintrc ln -s dev-lib/.eslintignore . && git add .eslintignore +ln -s dev-lib/.stylelintrc . && git add .stylelintrc ln -s dev-lib/.editorconfig . && git add .editorconfig cp dev-lib/.jshintignore . && git add .jshintignore # don't use symlink for this ``` -For ESLint, you'll also likely want to make `eslint` as a dev dependency for your NPM package: +For ESLint and stylelint, you'll also likely want to make then dev dependencies for your NPM package: ```bash npm init # if you don't have a package.json already npm install --save-dev eslint +npm install --save-dev stylelint stylelint-formatter-compact git add package.json echo 'node_modules' >> .gitignore git add .gitignore @@ -62,7 +64,7 @@ git clone https://github.com/xwp/wp-dev-lib.git ~/Projects/wp-dev-lib For the Travis CI checks, the `.travis.yml` copied and committed to the repo (see below) will clone the repo into the `dev-lib` directory if it doesn't exist (or whatever your `DEV_LIB_PATH` environment variable is set to). -To install the [`.jshintrc`](.jshint), [`.jshintignore`](.jshintignore), [`.jscsrc`](.jscsrc), and (especially optionally) [`phpcs.ruleset.xml`](phpcs.ruleset.xml), copy the files into the repo root (as opposed to creating symlinks, as when installing via submodule). +To install the [`.jshintrc`](.jshint), [`.jshintignore`](.jshintignore), [`.jscsrc`](.jscsrc), [`.eslintrc`](.eslintrc), [`.stylelintrc`](.stylelintrc), and (especially optionally) [`phpcs.ruleset.xml`](phpcs.ruleset.xml), copy the files into the repo root (as opposed to creating symlinks, as when installing via submodule). To install dev-lib for all themes and plugins that don't already have a `pre-commit` hook installed, and to upgrade the dev-lib for any submodule installations, you can run the bundled script [`install-upgrade-pre-commit-hook.sh`](install-upgrade-pre-commit-hook.sh) which will look for any repos in the current directory tree and attempt to auto-install. For example: diff --git a/travis.script.sh b/travis.script.sh index 2aa6e2d..b3a001c 100755 --- a/travis.script.sh +++ b/travis.script.sh @@ -9,6 +9,7 @@ fi echo check_execute_bit +lint_css_files lint_js_files lint_php_files lint_xml_files From 3933f301ca9024be1fef6bfbfd6a4f26d38374f7 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 17 May 2017 11:02:08 -0700 Subject: [PATCH 2/9] Support all stylelint config file formats --- check-diff.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/check-diff.sh b/check-diff.sh index d604b98..fe9b0d3 100755 --- a/check-diff.sh +++ b/check-diff.sh @@ -167,10 +167,12 @@ function set_environment_variables { fi if [ -z "$STYLELINT_CONFIG" ]; then - STYLELINT_CONFIG="$( upsearch .stylelintrc )" - fi - if [ -z "$STYLELINT_CONFIG" ]; then - STYLELINT_CONFIG="$( upsearch stylelint.config.js )" + for SEARCHED_STYLELINT_CONFIG in .stylelintrc{,.yaml,.yml,.js,.json} stylelint.config.js; do + STYLELINT_CONFIG="$( upsearch $SEARCHED_STYLELINT_CONFIG )" + if [ ! -z "$STYLELINT_CONFIG" ]; then + break + fi + done fi # Load any environment variable overrides from config files @@ -290,7 +292,7 @@ function set_environment_variables { done # Make sure linter configs get copied linting directory since upsearch is relative. - for linter_file in .jshintrc .jshintignore .jscsrc .jscs.json .eslintignore .eslintrc .stylelintrc stylelint.config.js phpcs.ruleset.xml ruleset.xml; do + for linter_file in .jshintrc .jshintignore .jscsrc .jscs.json .eslintignore .eslintrc .stylelintrc{,.yaml,.yml,.js,.json} stylelint.config.js phpcs.ruleset.xml ruleset.xml; do if git ls-files "$linter_file" --error-unmatch > /dev/null 2>&1; then if [ -L $linter_file ]; then ln -fs $(git show :"$linter_file") "$LINTING_DIRECTORY/$linter_file" From bcdb3669db7525484a321a4ffbd2b3788d8486d6 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 17 May 2017 11:02:42 -0700 Subject: [PATCH 3/9] Omit SCSS from CSS manifest --- check-diff.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check-diff.sh b/check-diff.sh index fe9b0d3..0dc650c 100755 --- a/check-diff.sh +++ b/check-diff.sh @@ -254,7 +254,7 @@ function set_environment_variables { cat "$TEMP_DIRECTORY/paths-scope" | grep -E '\.php(:|$)' | cat - > "$TEMP_DIRECTORY/paths-scope-php" cat "$TEMP_DIRECTORY/paths-scope" | grep -E '\.jsx?(:|$)' | cat - > "$TEMP_DIRECTORY/paths-scope-js" - cat "$TEMP_DIRECTORY/paths-scope" | grep -E '\.(css|scss)(:|$)' | cat - > "$TEMP_DIRECTORY/paths-scope-css" + cat "$TEMP_DIRECTORY/paths-scope" | grep -E '\.css(:|$)' | cat - > "$TEMP_DIRECTORY/paths-scope-css" cat "$TEMP_DIRECTORY/paths-scope" | grep -E '\.(xml|svg|xml.dist)(:|$)' | cat - > "$TEMP_DIRECTORY/paths-scope-xml" # Gather the proper states of files to run through linting (this won't apply to phpunit) From 2b06eed8c50ce606abdb39187c8cc2ecd9e8230f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 17 May 2017 11:04:11 -0700 Subject: [PATCH 4/9] Fix references to stylelint from eslint --- check-diff.sh | 4 ++-- readme.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/check-diff.sh b/check-diff.sh index 0dc650c..cbaff68 100755 --- a/check-diff.sh +++ b/check-diff.sh @@ -474,8 +474,8 @@ function install_tools { # Install stylelint if [ -n "$STYLELINT_CONFIG" ] && [ -e "$STYLELINT_CONFIG" ] && [ ! -e "$(npm bin)/stylelint" ] && check_should_execute 'stylelint'; then - echo "Installing ESLint" - if ! npm install -g eslint 2>/dev/null; then + echo "Installing stylelint" + if ! npm install -g stylelint 2>/dev/null; then echo "Failed to install stylelint (try manually doing: sudo npm install -g stylelint), so skipping stylelint" DEV_LIB_SKIP="$DEV_LIB_SKIP,stylelint" fi diff --git a/readme.md b/readme.md index d8100ba..e7ea0a8 100644 --- a/readme.md +++ b/readme.md @@ -40,7 +40,7 @@ ln -s dev-lib/.editorconfig . && git add .editorconfig cp dev-lib/.jshintignore . && git add .jshintignore # don't use symlink for this ``` -For ESLint and stylelint, you'll also likely want to make then dev dependencies for your NPM package: +For ESLint and stylelint, you'll also likely want to make them dev dependencies for your NPM package: ```bash npm init # if you don't have a package.json already From 73fdbd22c3f4ca527385c7e648dc48dae2405beb Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 17 May 2017 11:31:23 -0700 Subject: [PATCH 5/9] Fix passing of custom-formatter --- check-diff.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/check-diff.sh b/check-diff.sh index cbaff68..12fc55e 100755 --- a/check-diff.sh +++ b/check-diff.sh @@ -478,6 +478,9 @@ function install_tools { if ! npm install -g stylelint 2>/dev/null; then echo "Failed to install stylelint (try manually doing: sudo npm install -g stylelint), so skipping stylelint" DEV_LIB_SKIP="$DEV_LIB_SKIP,stylelint" + elif ! -e node_modules/stylelint-formatter-compact; then + echo "The stylelint-formatter-compact node module is not installed, skipping stylelint." + DEV_LIB_SKIP="$DEV_LIB_SKIP,stylelint" fi fi @@ -807,8 +810,7 @@ function lint_css_files { ( echo "## stylelint" cd "$LINTING_DIRECTORY" - # TODO: --format=compact is not right. - if ! cat "$TEMP_DIRECTORY/paths-scope-css" | remove_diff_range | xargs "$(npm bin)/stylelint" --format=compact --config="$STYLELINT_CONFIG" > "$TEMP_DIRECTORY/stylelint-report"; then + if ! cat "$TEMP_DIRECTORY/paths-scope-css" | remove_diff_range | xargs "$(npm bin)/stylelint" --custom-formatter=node_modules/stylelint-formatter-compact --config="$STYLELINT_CONFIG" > "$TEMP_DIRECTORY/stylelint-report"; then if [ "$CHECK_SCOPE" == 'patches' ]; then cat "$TEMP_DIRECTORY/stylelint-report" | php "$DEV_LIB_PATH/diff-tools/filter-report-for-patch-ranges.php" "$TEMP_DIRECTORY/paths-scope-css" | cut -c$( expr ${#LINTING_DIRECTORY} + 2 )- exit_code="${PIPESTATUS[1]}" From b0eff2f27b64d71c0a912e90a753034dd375cd3f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 17 May 2017 11:48:25 -0700 Subject: [PATCH 6/9] Update .editorconfig with latest from core with also linter configs --- .editorconfig | 4 ++-- .stylelintrc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.editorconfig b/.editorconfig index 21f03ed..4a6709c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,6 @@ # WordPress Coding Standards # https://make.wordpress.org/core/handbook/coding-standards/ - + root = true [*] @@ -10,7 +10,7 @@ insert_final_newline = true trim_trailing_whitespace = true indent_style = tab -[{package.json,*.yml}] +[{*.json,*.yml,.stylelintrc,.jshintrc,.eslintrc,.jscsrc}] indent_style = space indent_size = 2 diff --git a/.stylelintrc b/.stylelintrc index 58568a7..58c6190 100644 --- a/.stylelintrc +++ b/.stylelintrc @@ -1,3 +1,3 @@ { "extends": "stylelint-config-wordpress" -} \ No newline at end of file +} From a23b89bf15d52b711c81da7d7aac217a56fd0477 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 17 May 2017 16:18:14 -0700 Subject: [PATCH 7/9] Include *.yaml in .editorconfig --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 4a6709c..82b17fb 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,7 +10,7 @@ insert_final_newline = true trim_trailing_whitespace = true indent_style = tab -[{*.json,*.yml,.stylelintrc,.jshintrc,.eslintrc,.jscsrc}] +[{*.json,*.yml,*.yaml,.stylelintrc,.jshintrc,.eslintrc,.jscsrc}] indent_style = space indent_size = 2 From 580e3883135308089d39b2b51ca9c6d6c3ced5ba Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 17 May 2017 17:07:02 -0700 Subject: [PATCH 8/9] Fix comment --- check-diff.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check-diff.sh b/check-diff.sh index 12fc55e..0d99c86 100755 --- a/check-diff.sh +++ b/check-diff.sh @@ -805,7 +805,7 @@ function lint_css_files { set -e - # Run ESLint. + # Run stylelint. if [ -n "$STYLELINT_CONFIG" ] && [ -e "$STYLELINT_CONFIG" ] && [ -e "$(npm bin)/stylelint" ] && check_should_execute 'stylelint'; then ( echo "## stylelint" From c2d14a06ee16983fd075305648de99298ea9cea4 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 24 Jul 2017 13:09:06 -0700 Subject: [PATCH 9/9] Revert "Update .editorconfig with latest from core with also linter configs" This partially reverts commit b0eff2f27b64d71c0a912e90a753034dd375cd3f. --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 82b17fb..e271f67 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,7 +10,7 @@ insert_final_newline = true trim_trailing_whitespace = true indent_style = tab -[{*.json,*.yml,*.yaml,.stylelintrc,.jshintrc,.eslintrc,.jscsrc}] +[{package.json,*.yml}] indent_style = space indent_size = 2