Skip to content

Commit

Permalink
no break when import from directive is missing (added diagnostic test…
Browse files Browse the repository at this point in the history
… to warn of invalid import)
  • Loading branch information
idoros committed Jul 26, 2017
1 parent b1e48a0 commit 79c93a5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/import.ts
Expand Up @@ -13,6 +13,8 @@ export class Import {

SbFrom = SbFrom || cssImportDef[valueMapping.from];

if(!SbFrom){ return null; }

if (cssImportDef[valueMapping.named]) {
cssImportDef[valueMapping.named].split(',').forEach((name) => {
const parts = name.trim().split(/\s+as\s+/);
Expand Down
3 changes: 2 additions & 1 deletion src/process.ts
Expand Up @@ -36,7 +36,8 @@ function processDefinition(sheet: Stylesheet, selector: string, rules: CSSRulesO
if (type === "pseudo-class") {
if (name === 'import') {
const { content } = <PseudoSelectorAstNode>node;
sheet.imports.push(Import.fromImportObject(content, rules));
const importRes = Import.fromImportObject(content, rules);
importRes && sheet.imports.push(importRes);
} else if (name === 'vars') {
Object.assign(sheet.vars, rules);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/generator.generate.spec.ts
Expand Up @@ -135,6 +135,25 @@ describe('static Generator.generate', function () {
expect(css.length).to.equal(res.length);
});

it('not append imports with unknown "from" to the output', function () {
const sheetA = Stylesheet.fromCSS(`
:import{}
.containerA{ color:red; }
`, "TheNameSpace");

const css = Generator.generate([sheetA], new Generator({
namespaceDivider: "__THE_DIVIDER__",
resolver: new Resolver({})
}));

const res = [
'.TheNameSpace__THE_DIVIDER__containerA {\n color: red\n}',
];

css.forEach((chunk, index) => expect(chunk).to.eql(res[index]));
expect(css.length).to.equal(res.length);
});

it('scoped typed selector that extends root', function () {

const sheetA = Stylesheet.fromCSS(``, "TheNameSpace");
Expand Down
40 changes: 40 additions & 0 deletions tests/import.spec.ts
Expand Up @@ -18,5 +18,45 @@ describe('Import', function () {

});

describe('fromImportObject', function() {

it('should return Import instance from import CSS definition', function(){
const _import = Import.fromImportObject('./a', {
'-st-default':'DefaultName',
'-st-named':'NamedA, NamedB',
});

if(!_import){
throw new Error('expected import to have value');
}
expect(_import.from).to.equal('./a');
expect(_import.defaultExport).to.equal('DefaultName');
expect(_import.containsSymbol('NamedA'), 'NamedA').to.equal(true);
expect(_import.containsSymbol('NamedB'), 'NamedB').to.equal(true);
});

it('should return Import instance from import CSS definition with "from" inside definition', function(){
const _import = Import.fromImportObject('', {
'-st-from':'./a',
'-st-default':'DefaultName',
});

if(!_import){
throw new Error('expected import to have value');
}
expect(_import.from).to.equal('./a');
expect(_import.defaultExport).to.equal('DefaultName');
});

it('should return null when "from" is not found', function(){
const _import = Import.fromImportObject('', {
'-st-default':'DefaultName',
});

expect(_import).to.equal(null);
});

});

});

0 comments on commit 79c93a5

Please sign in to comment.