Skip to content

Commit e252c21

Browse files
committed
Initial commit
0 parents  commit e252c21

17 files changed

+667
-0
lines changed

.editorconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
indent_style = tab
10+
indent_size = 2
11+
insert_final_newline = true
12+
tab_width = 4
13+
trim_trailing_whitespace = true
14+
block_comment_start = /*
15+
block_comment = *
16+
block_comment_end = */
17+
18+
[*.{cmd,bat}]
19+
end_of_line = crlf
20+
21+
[{package.json,*.yml}]
22+
indent_style = space

.eslintrc.json

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"parserOptions": {
3+
"sourceType": "script",
4+
"impliedStrict": false
5+
},
6+
"env": {
7+
"es6": true,
8+
"node": true
9+
},
10+
"extends": [
11+
"standard"
12+
],
13+
"root": true,
14+
"rules": {
15+
"comma-dangle": [
16+
"error",
17+
{
18+
"arrays": "always-multiline",
19+
"objects": "always-multiline",
20+
"imports": "always-multiline",
21+
"exports": "always-multiline",
22+
"functions": "never"
23+
}
24+
],
25+
"comma-spacing": [
26+
"error",
27+
{
28+
"after": true,
29+
"before": false
30+
}
31+
],
32+
"indent": [
33+
"error",
34+
"tab",
35+
{
36+
"SwitchCase": 1
37+
}
38+
],
39+
"no-tabs": [
40+
"off"
41+
],
42+
"no-var": [
43+
"error"
44+
],
45+
"prefer-arrow-callback": [
46+
"error"
47+
],
48+
"prefer-const": [
49+
"error"
50+
],
51+
"quotes": [
52+
"error",
53+
"double"
54+
],
55+
"semi": [
56+
"error",
57+
"always",
58+
{
59+
"omitLastInOneLineBlock": false
60+
}
61+
],
62+
"strict": [
63+
"error",
64+
"safe"
65+
]
66+
},
67+
"overrides": [
68+
{
69+
"files": [
70+
"test/**/*"
71+
],
72+
"env": {
73+
"mocha": true
74+
},
75+
"rules": {
76+
"no-template-curly-in-string": "off"
77+
}
78+
}
79+
]
80+
}

.gitignore

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Created by https://www.gitignore.io/api/node
2+
3+
### Node ###
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
yarn-debug.log*
9+
yarn-error.log*
10+
11+
# Runtime data
12+
pids
13+
*.pid
14+
*.seed
15+
*.pid.lock
16+
17+
# Directory for instrumented libs generated by jscoverage/JSCover
18+
lib-cov
19+
20+
# Coverage directory used by tools like istanbul
21+
coverage
22+
23+
# nyc test coverage
24+
.nyc_output
25+
26+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
27+
.grunt
28+
29+
# Bower dependency directory (https://bower.io/)
30+
bower_components
31+
32+
# node-waf configuration
33+
.lock-wscript
34+
35+
# Compiled binary addons (http://nodejs.org/api/addons.html)
36+
build/Release
37+
38+
# Dependency directories
39+
node_modules/
40+
jspm_packages/
41+
42+
# Typescript v1 declaration files
43+
typings/
44+
45+
# Optional npm cache directory
46+
.npm
47+
48+
# Optional eslint cache
49+
.eslintcache
50+
51+
# Optional REPL history
52+
.node_repl_history
53+
54+
# Output of 'npm pack'
55+
*.tgz
56+
57+
# Yarn Integrity file
58+
.yarn-integrity
59+
60+
# dotenv environment variables file
61+
.env
62+
63+
64+
65+
# End of https://www.gitignore.io/api/node

.npmignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
*.log
2+
*.pid
3+
*.seed
4+
.editorconfig
5+
.eslintrc*
6+
.gitignore
7+
.grunt
8+
.lock-wscript
9+
.node_repl_history
10+
.nyc_output
11+
.stylelintrc*
12+
.travis.yml
13+
.vscode
14+
appveyor.yml
15+
coverage
16+
gulpfile.js
17+
lib-cov
18+
logs
19+
node_modules
20+
npm-debug.log*
21+
pids
22+
test

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
dist: trusty
2+
sudo: false
3+
language: node_js
4+
5+
node_js:
6+
- stable
7+
- 4
8+
9+
after_script:
10+
- npm run report-coverage

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 刘祺
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# postcss-styled
2+
PostCSS syntax for parsing styled components

lib/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
const parser = require("./parser");
3+
const syntax = require("postcss-syntax/lib/syntax")(parser);
4+
5+
module.exports = syntax;

lib/parser.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
"use strict";
2+
const getSyntax = require("postcss-syntax/lib/get-syntax");
3+
4+
function literalParser (source, opts, styles) {
5+
styles = styles || [];
6+
let styledSyntax;
7+
8+
function getStyledSyntax () {
9+
if (!styledSyntax) {
10+
const cssSyntax = getSyntax("css", opts);
11+
styledSyntax = {
12+
parse: require(cssSyntax.parse.name === "safeParse" ? "./styled-safe-parse" : "./styled-parse"),
13+
stringify: cssSyntax.stringify,
14+
};
15+
}
16+
return styledSyntax;
17+
}
18+
19+
literal(source, (startIndex, endIndex, quote) => {
20+
if (quote !== "`") {
21+
return;
22+
}
23+
24+
const strSource = source.slice(startIndex, endIndex);
25+
26+
if (!strSource.trim()) {
27+
return;
28+
}
29+
30+
styles.push({
31+
startIndex: startIndex,
32+
content: strSource,
33+
ignoreErrors: true,
34+
syntax: /(^|\s|\{|\}|;|:)\$\{/m.test(strSource) && getStyledSyntax(),
35+
});
36+
});
37+
38+
return styles;
39+
};
40+
41+
function literal (source, callback) {
42+
let insideString = false;
43+
let insideComment = false;
44+
let insideSingleLineComment = false;
45+
let strStartIndex;
46+
let openingQuote;
47+
48+
for (let i = 0, l = source.length; i < l; i++) {
49+
const currentChar = source[i];
50+
51+
// Register the beginning of a comment
52+
if (
53+
!insideString && !insideComment &&
54+
currentChar === "/" &&
55+
source[i - 1] !== "\\" // escaping
56+
) {
57+
// standard comments
58+
if (source[i + 1] === "*") {
59+
insideComment = true;
60+
continue;
61+
}
62+
// single-line comments
63+
if (source[i + 1] === "/") {
64+
insideComment = true;
65+
insideSingleLineComment = true;
66+
continue;
67+
}
68+
}
69+
70+
if (insideComment) {
71+
// Register the end of a standard comment
72+
if (
73+
!insideSingleLineComment &&
74+
currentChar === "*" &&
75+
source[i - 1] !== "\\" && // escaping
76+
source[i + 1] === "/" &&
77+
source[i - 1] !== "/" // don't end if it's /*/
78+
) {
79+
insideComment = false;
80+
continue;
81+
}
82+
83+
// Register the end of a single-line comment
84+
if (
85+
insideSingleLineComment &&
86+
currentChar === "\n"
87+
) {
88+
insideComment = false;
89+
insideSingleLineComment = false;
90+
continue;
91+
}
92+
}
93+
94+
// Register the beginning of a string
95+
if (!insideComment && !insideString && (currentChar === "\"" || currentChar === "'" || currentChar === "`")) {
96+
if (source[i - 1] === "\\") continue; // escaping
97+
98+
openingQuote = currentChar;
99+
insideString = true;
100+
101+
strStartIndex = i + openingQuote.length;
102+
continue;
103+
}
104+
105+
// Register the end of a string
106+
if (insideString && currentChar === openingQuote) {
107+
if (source[i - 1] === "\\") continue; // escaping
108+
insideString = false;
109+
if ((i - strStartIndex) > 1) {
110+
callback(strStartIndex, i, openingQuote);
111+
}
112+
continue;
113+
}
114+
}
115+
}
116+
module.exports = literalParser;

lib/styled-parse.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
const StyledParser = require("./styled-parser");
4+
const Input = require("postcss/lib/input");
5+
6+
function styledParse (css, opts) {
7+
const input = new Input(css, opts);
8+
9+
const parser = new StyledParser(input);
10+
parser.parse();
11+
12+
return parser.root;
13+
}
14+
module.exports = styledParse;

lib/styled-parser.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
const Parser = require("postcss/lib/parser");
3+
const styledTokenize = require("./styled-tokenize");
4+
class StyledParser extends Parser {
5+
createTokenizer () {
6+
this.tokenizer = styledTokenize(this.input);
7+
}
8+
}
9+
module.exports = StyledParser;

lib/styled-safe-parse.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
const StyledSafeParser = require("./styled-safe-parser");
4+
const Input = require("postcss/lib/input");
5+
6+
function styledSafeParse (css, opts) {
7+
const input = new Input(css, opts);
8+
9+
const parser = new StyledSafeParser(input);
10+
parser.parse();
11+
12+
return parser.root;
13+
}
14+
module.exports = styledSafeParse;

0 commit comments

Comments
 (0)