Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit 1da206b

Browse files
Safe postcss values parser (#17)
* Dev dependencies using ranges consistently with other packages * Ignoring unknown words * If there's an exception during parsing, do not let it blow up * Adding tests
1 parent 019c35f commit 1da206b

9 files changed

+67
-15
lines changed

.tape.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module.exports = {
22
'basic': {
3-
message: 'supports basic usage'
3+
message: 'supports basic usage',
4+
warnings: 1
45
},
56
'basic:import': {
67
message: 'supports { importFrom: { environmentVariables: { ... } } } usage',
8+
warnings: 1,
79
options: {
810
importFrom: {
911
environmentVariables: {
@@ -15,6 +17,7 @@ module.exports = {
1517
},
1618
'basic:import-fn': {
1719
message: 'supports { importFrom() } usage',
20+
warnings: 1,
1821
options: {
1922
importFrom() {
2023
return {
@@ -30,6 +33,7 @@ module.exports = {
3033
},
3134
'basic:import-fn-promise': {
3235
message: 'supports { async importFrom() } usage',
36+
warnings: 1,
3337
options: {
3438
importFrom() {
3539
return new Promise(resolve => {
@@ -47,6 +51,7 @@ module.exports = {
4751
},
4852
'basic:import-json': {
4953
message: 'supports { importFrom: "test/import-variables.json" } usage',
54+
warnings: 1,
5055
options: {
5156
importFrom: 'test/import-variables.json'
5257
},
@@ -55,6 +60,7 @@ module.exports = {
5560
},
5661
'basic:import-js': {
5762
message: 'supports { importFrom: "test/import-variables.js" } usage',
63+
warnings: 1,
5864
options: {
5965
importFrom: 'test/import-variables.js'
6066
},
@@ -63,6 +69,7 @@ module.exports = {
6369
},
6470
'basic:import-cjs': {
6571
message: 'supports { importFrom: "test/import-variables.cjs" } usage',
72+
warnings: 1,
6673
options: {
6774
importFrom: 'test/import-variables.cjs'
6875
},
@@ -71,6 +78,7 @@ module.exports = {
7178
},
7279
'basic:import-js-from': {
7380
message: 'supports { importFrom: { from: "test/import-variables.js" } } usage',
81+
warnings: 1,
7482
options: {
7583
importFrom: { from: 'test/import-variables.js' }
7684
},
@@ -79,6 +87,7 @@ module.exports = {
7987
},
8088
'basic:import-js-from-type': {
8189
message: 'supports { importFrom: [ { from: "test/import-variables.js", type: "js" } ] } usage',
90+
warnings: 1,
8291
options: {
8392
importFrom: [ { from: 'test/import-variables.js', type: 'js' } ]
8493
},
@@ -87,6 +96,7 @@ module.exports = {
8796
},
8897
'basic:import-is-empty': {
8998
message: 'supports { importFrom: {} } usage',
99+
warnings: 1,
90100
options: {
91101
importFrom: {}
92102
}

package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
"postcss": "^8.3"
3333
},
3434
"devDependencies": {
35-
"@babel/core": "7.15.8",
36-
"@babel/preset-env": "7.15.8",
37-
"@rollup/plugin-babel": "5.3.0",
38-
"eslint": "8.1.0",
39-
"postcss": "8.3.6",
40-
"postcss-tape": "6.0.1",
41-
"pre-commit": "1.2.2",
42-
"rollup": "2.58.3"
35+
"@babel/core": "^7.15.8",
36+
"@babel/preset-env": "^7.15.8",
37+
"@rollup/plugin-babel": "^5.3.0",
38+
"eslint": "^8.1.0",
39+
"postcss": "^8.3.6",
40+
"postcss-tape": "^6.0.1",
41+
"pre-commit": "^1.2.2",
42+
"rollup": "^2.58.3"
4343
},
4444
"babel": {
4545
"presets": [

src/index.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,41 @@ export default function creator(opts) {
1414

1515
return {
1616
postcssPlugin: 'postcss-env-fn',
17-
async AtRule(atRule) {
18-
const replacedValue = getReplacedValue(atRule.params, await environmentVariablesPromise)
17+
async AtRule(atRule, { result }) {
18+
let replacedValue
19+
20+
try {
21+
replacedValue = getReplacedValue(atRule.params, await environmentVariablesPromise)
22+
} catch (error) {
23+
atRule.warn(
24+
result,
25+
`Failed to parse params '${atRule.params}' as an environment value. Leaving the original value intact.`
26+
)
27+
}
28+
29+
if (typeof replacedValue === 'undefined') {
30+
return
31+
}
1932

2033
if (replacedValue !== atRule.params) {
2134
atRule.params = replacedValue
2235
}
2336
},
24-
async Declaration(decl) {
25-
const replacedValue = getReplacedValue(decl.value, await environmentVariablesPromise)
37+
async Declaration(decl, { result }) {
38+
let replacedValue
39+
40+
try {
41+
replacedValue = getReplacedValue(decl.value, await environmentVariablesPromise)
42+
} catch (error) {
43+
decl.warn(
44+
result,
45+
`Failed to parse value '${decl.value}' as an environment value. Leaving the original value intact.`
46+
)
47+
}
48+
49+
if (typeof replacedValue === 'undefined') {
50+
return
51+
}
2652

2753
if (replacedValue !== decl.value) {
2854
decl.value = replacedValue

src/lib/get-replaced-value.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import walkEnvFuncs from './walk-env-funcs'
99
*/
1010
export default (originalValue, variables) => {
1111
// get the ast of the original value
12-
const ast = parse(originalValue)
12+
const ast = parse(originalValue, { ignoreUnknownWords: true })
1313

1414
// walk all of the css env() functions
1515
walkEnvFuncs(ast, node => {

src/lib/import-from.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function importEnvironmentVariablesFromObject(object) {
1414
)
1515

1616
for (const key in environmentVariables) {
17-
environmentVariables[key] = parse(environmentVariables[key]).nodes
17+
environmentVariables[key] = parse(environmentVariables[key], { ignoreUnknownWords: true }).nodes
1818
}
1919

2020
return environmentVariables

test/basic.css

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ body {
1414
order: 3;
1515
}
1616
}
17+
18+
html {
19+
background-image: url(data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==);
20+
}

test/basic.expect.css

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ body {
1414
order: 3;
1515
}
1616
}
17+
18+
html {
19+
background-image: url(data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==);
20+
}

test/basic.import-is-empty.expect.css

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ body {
1414
order: 3;
1515
}
1616
}
17+
18+
html {
19+
background-image: url(data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==);
20+
}

test/basic.import.expect.css

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ body {
1414
order: 3;
1515
}
1616
}
17+
18+
html {
19+
background-image: url(data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==);
20+
}

0 commit comments

Comments
 (0)