Skip to content

Commit

Permalink
Skip file types that contain more code than what stylelint extracts
Browse files Browse the repository at this point in the history
Drop the stylelint required version back down
  • Loading branch information
BPScott committed May 11, 2019
1 parent 2accd79 commit 2c7a214
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 378 deletions.
5 changes: 3 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
!.eslintrc.js
node_modules

# This test file is deliberately unparsable
test/fixtures/check.unparsable.js
# Ignore everything in the fixtures dir except the stylelint config
test/fixtures
!test/fixtures/stylelint.config.js
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"peerDependencies": {
"prettier": ">= 0.11.0",
"stylelint": ">= 9.10.0"
"stylelint": ">= 9.2.1"
},
"devDependencies": {
"eslint": "^5.0.1",
Expand All @@ -42,7 +42,7 @@
"eslint-plugin-prettier": "^3.0.0",
"jest": "^23.6.0",
"prettier": "^1.17.0",
"stylelint": "^9.10.0",
"stylelint": "^9.5.0",
"stylelint-config-prettier": "^4.0.0"
},
"engines": {
Expand Down
33 changes: 15 additions & 18 deletions stylelint-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,25 @@ module.exports = stylelint.createPlugin(
// languages, thus we can't rely on guessing the parser based off the
// filename.

// JS may change the language based on if you're using a template literal
// or an object: `styled.a`color: red`;` or `style.a({color: 'red'})`.
// Prettier does not have a parser for any of these formats so do nothing
const parserBlockList = ['babel', 'flow', 'typescript'];
// In all of the following cases stylelint extracts a part of a file to
// be formatted and there exists a prettier parser for the whole file.
// If you're interested in prettier you'll want a fully formatted file so
// you're about to run prettier over the whole file anyway.
// Therefore running prettier over just the style section is wasteful, so
// skip it.

const parserBlockList = [
'babel',
'flow',
'typescript',
'vue',
'markdown',
'html',
];
if (parserBlockList.indexOf(prettierFileInfo.inferredParser) !== -1) {
return;
}

// html and vue may change the language based on a style element's `lang`
// tag: `<style>` should be parsed as CSS while `<style lang="scss">`
// should be pared as SCSS.
// markdown may change the language based on the fenced code blocks'
// language hint: Blocks begining with "```scss" should be parsed as SCSS
//
// This is added to the options first, so that
// prettierRcOptions and stylelintPrettierOptions can still override
// the parser.
const parserInferLangList = ['vue', 'markdown', 'html'];
if (parserInferLangList.indexOf(prettierFileInfo.inferredParser) !== -1) {
initialOptions.parser = root.source.lang;
}

const prettierOptions = Object.assign(
{},
initialOptions,
Expand Down
26 changes: 26 additions & 0 deletions test/fixtures/check.inert.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Page Title</title>
<style>
.foo {
background-image: url("x");
}
</style>

<style lang="scss">
.foo {
background-image: url("x");
}

$map: (
'alpha': 10,
'beta': 20,
'gamma': 30
);
</style>
</head>

<body></body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const Button = styled.div`
`;

const Button2 = styled.div({
'background-image': url("x"),
'color': 'red',
backgroundImage: 'url("x")',
color: 'red',
});
File renamed without changes.
File renamed without changes.
27 changes: 0 additions & 27 deletions test/fixtures/check.invalid.html

This file was deleted.

23 changes: 4 additions & 19 deletions test/stylelint-prettier-e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,17 @@ check.invalid.scss
8:14 ✖ Insert "," prettier/prettier
`.trim();

expect(result.status).toEqual(2);
expect(result.output).toEqual(expectedResult);
expect(result.status).toEqual(2);
});

test('HTML/Markdown/Vue files', () => {
const result = runStylelint('*.{html,md,vue}');

const expectedResult = `
check.invalid.html
8:25 ✖ Replace ""x"" with "'x'" prettier/prettier
14:25 ✖ Replace ""x"" with "'x'" prettier/prettier
20:14 ✖ Insert "," prettier/prettier
check.invalid.md
5:25 ✖ Replace ""x"" with "'x'" prettier/prettier
11:25 ✖ Replace ""x"" with "'x'" prettier/prettier
17:14 ✖ Insert "," prettier/prettier
check.invalid.vue
7:25 ✖ Replace ""x"" with "'x'" prettier/prettier
13:25 ✖ Replace ""x"" with "'x'" prettier/prettier
19:14 ✖ Insert "," prettier/prettier
`.trim();
const expectedResult = ``;

expect(result.status).toEqual(2);
expect(result.output).toEqual(expectedResult);
expect(result.status).toEqual(0);
});

/**
Expand All @@ -50,8 +35,8 @@ check.invalid.vue

const expectedResult = ``;

expect(result.status).toEqual(0);
expect(result.output).toEqual(expectedResult);
expect(result.status).toEqual(0);
});
});

Expand Down
Loading

0 comments on commit 2c7a214

Please sign in to comment.