Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined variable inside function #90

Open
AlexWayfer opened this issue Aug 18, 2020 · 3 comments
Open

Undefined variable inside function #90

AlexWayfer opened this issue Aug 18, 2020 · 3 comments

Comments

@AlexWayfer
Copy link

Hello.

I have such files:

// postcss.config.js
module.exports = {
	plugins: [
		require('postcss-import')({
			root: `${__dirname}/assets/styles`
		}),
		require('postcss-simple-vars'),
		require('postcss-conditionals'),
		require('postcss-nested'),
		require('postcss-flexbugs-fixes'),
		// require('postcss-color-mod-function'),
		require('autoprefixer')
	]
}
/* main.pcss */
@import 'colors.pcss';

@import 'components/flash.pcss';
@import 'components/forms.pcss';

/* ... */
/* colors.pcss */
$danger-text-color: red;
$error-color: $danger-text-color;
/* components/flash.pcss */
.flash {
	&.error {
		color: $error-color;
	}
}
/* components/forms.pcss */
form {
	p.error {
		color: color-mod($error-color whiteness(15%));
	}
}

And I'm getting such error:

CssSyntaxError: postcss-simple-vars: /home/alex/Projects/ruby/test/foo_bar/assets/styles/components/forms.pcss:22:3: Undefined variable $error-color

  20 | 		margin: 0;
  21 | 		padding-left: 0.4em;
> 22 | 		color: color-mod($error-color whiteness(15%));
     | 		^
  23 | 	}
  24 | 
@ai
Copy link
Member

ai commented Aug 19, 2020

Can you send a PR? I spend all my time on PostCSS 8 release preparation.

It should be related with this code

function simpleSyntax (variables, node, str, opts, result) {
return str.replace(/(^|[^\w])\$([\w\d-_]+)/g, function (_, bef, name) {
return bef + variable(variables, node, '$' + name, name, opts, result)
})
}

The plugin code is very simple and small.

@firasdib
Copy link

@ai Maybe you're looking for a regex more along the lines of this

/\B\$([\w\d\-_]+)/g

See: https://regex101.com/r/18W8P6/1

@sampullman
Copy link
Collaborator

I can't reproduce this, I don't have time to set up an example repo to include imports but the problem could be related to plugin order. Or, unless I misunderstand how scoping is supposed to work, should components/forms.pcss have @import 'colors.pcss'; in order to resolve the variable?

Replacement in functions seems to work fine, I tested the specific case above and added a unit test for this kind of thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants