Skip to content

Commit

Permalink
Adding Styled Components linter w/ default config as pre-commit hook …
Browse files Browse the repository at this point in the history
…and dev/script (#25441)

* Initial pass at adding styled comp linter and pre-commit hook
* Added project support to stylelint dev script, error/success logging on pre-commit hook, and fixed stylelint errors in secops project
  • Loading branch information
spong authored and FrankHassanabad committed Nov 9, 2018
1 parent fa7ecdd commit 1e160a2
Show file tree
Hide file tree
Showing 12 changed files with 881 additions and 18 deletions.
7 changes: 7 additions & 0 deletions .stylelintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"processors": ["stylelint-processor-styled-components"],
"extends": [
"stylelint-config-recommended",
"stylelint-config-styled-components"
]
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@
"rxjs": "^6.2.1",
"script-loader": "0.7.2",
"semver": "^5.5.0",
"style-loader": "0.19.0",
"tar": "2.2.0",
"tinygradient": "0.3.0",
"tinymath": "1.1.0",
Expand Down Expand Up @@ -344,6 +343,11 @@
"simple-git": "1.37.0",
"sinon": "^5.0.7",
"strip-ansi": "^3.0.1",
"style-loader": "0.19.0",
"stylelint": "^9.7.1",
"stylelint-processor-styled-components": "^1.5.0",
"stylelint-config-styled-components": "^0.1.1",
"stylelint-config-standard": "^18.2.0",
"supertest": "^3.1.0",
"supertest-as-promised": "^4.0.2",
"tree-kill": "^1.1.0",
Expand Down
21 changes: 21 additions & 0 deletions scripts/stylelint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

require('../src/setup_node_env');
require('../src/dev/stylelint').runStylelintCli();
3 changes: 2 additions & 1 deletion src/dev/run_precommit_hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { run, combineErrors } from './run';
import * as Eslint from './eslint';
import * as Tslint from './tslint';
import * as Stylelint from './stylelint';
import { getFilesForCommit, checkFileCasing } from './precommit_hook';

run(async ({ log }) => {
Expand All @@ -32,7 +33,7 @@ run(async ({ log }) => {
errors.push(error);
}

for (const Linter of [Eslint, Tslint]) {
for (const Linter of [Eslint, Tslint, Stylelint]) {
const filesToLint = Linter.pickFilesToLint(log, files);
if (filesToLint.length > 0) {
try {
Expand Down
22 changes: 22 additions & 0 deletions src/dev/stylelint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export { runStylelintCli } from './run_stylelint_cli';
export { lintFiles } from './lint_files';
export { pickFilesToLint } from './pick_files_to_lint';
41 changes: 41 additions & 0 deletions src/dev/stylelint/lint_files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { ToolingLog } from '@kbn/dev-utils';
import { File } from '../file';
import { createFailError } from '../run';

/**
* Lints a list of files with eslint. eslint reports are written to the log
* and a FailError is thrown when linting errors occur.
*
* @param {ToolingLog} log
* @param {Array<File>} files
* @return {undefined}
*/
export async function lintFiles(log: ToolingLog, files: File[]) {
const paths = files.map(file => file.getRelativePath());
await require('stylelint/lib/cli')(paths);

if (process.exitCode === 2) {
throw createFailError('[stylelint] failure', 1);
} else {
log.success('[stylelint] staged files linted successfully');
}
}
26 changes: 26 additions & 0 deletions src/dev/stylelint/pick_files_to_lint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { ToolingLog } from '@kbn/dev-utils';

import { File } from '../file';

export function pickFilesToLint(log: ToolingLog, files: File[]) {
return files.filter(file => file.isTypescript() && !file.isFixture());
}
29 changes: 29 additions & 0 deletions src/dev/stylelint/run_stylelint_cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export function runStylelintCli() {
const args = process.argv.slice(2);
let opts = '**/*.tsx';

if (args.length > 0) {
opts = args.map(el => `x-pack/plugins/${el}/**/*.tsx`).join(' ');
}

require('stylelint/lib/cli')(opts);
}
9 changes: 9 additions & 0 deletions tasks/config/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ module.exports = function (grunt) {
]
},

// used by dev/run_precommit_hook.js
// runs the stylelint script to check for linting errors in Style Components
stylelint: {
cmd: process.execPath,
args: [
require.resolve('../../scripts/stylelint')
]
},

// used by the test and jenkins:unit tasks
// runs the tslint script to check for Typescript linting errors
tslint: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const ColumnHeadersSpan = styled.span`
display: flex;
`;

/* stylelint-disable block-no-empty */
const ColumnHeaderContainer = styled.div``;

const Flex = styled.div`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const SuricataRow = styled.div`
padding-top: 10px;
padding-bottom: 20px;
animation: ${dropInEffect} 2s;
margin-left -1px;
margin-left: -1px;
transition: 700ms background, 700ms border-color, 1s transform, 1s box-shadow;
transition-delay: 0s;
&:hover {
Expand Down
Loading

0 comments on commit 1e160a2

Please sign in to comment.