Skip to content

Commit

Permalink
chore: replace deindent test dependency (#2799)
Browse files Browse the repository at this point in the history
  • Loading branch information
idoros authored Dec 26, 2022
1 parent 5278bc9 commit c42061b
Show file tree
Hide file tree
Showing 25 changed files with 169 additions and 126 deletions.
25 changes: 0 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"@types/css-selector-tokenizer": "^0.7.1",
"@types/cssesc": "^3.0.0",
"@types/csso": "^5.0.0",
"@types/deindent": "^0.1.0",
"@types/express": "^4.17.15",
"@types/find-config": "^1.0.1",
"@types/flat": "^5.0.2",
Expand Down
8 changes: 3 additions & 5 deletions packages/code-formatter/test/formatter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { deindent } from '@stylable/core-test-kit';
import { getDocumentFormatting } from '@stylable/code-formatter';
import { expect } from 'chai';
import deindent from 'deindent';

describe('Formatting', () => {
it('should format an entire stylesheet with extra spaces', () => {
Expand All @@ -19,7 +19,6 @@ describe('Formatting', () => {
it('should preserve grid-template declarations', () => {
const res = getDocumentFormatting(
deindent(`
/* PRESERVE INDENT */
.foo {
grid-template:
"icon name name ." 16px
Expand All @@ -30,7 +29,7 @@ describe('Formatting', () => {
);

expect(res).to.eql(
deindent(`/* PRESERVE INDENT */
deindent(`
.foo {
grid-template:
"icon name name ." 16px
Expand All @@ -43,7 +42,6 @@ describe('Formatting', () => {
it('should preserve grid declarations', () => {
const res = getDocumentFormatting(
deindent(`
/* PRESERVE INDENT */
.foo {
grid:
"icon name name ." 16px
Expand All @@ -54,7 +52,7 @@ describe('Formatting', () => {
);

expect(res).to.eql(
deindent(`/* PRESERVE INDENT */
deindent(`
.foo {
grid:
"icon name name ." 16px
Expand Down
2 changes: 1 addition & 1 deletion packages/code-formatter/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"outDir": "../dist/test",
"types": ["node", "externals", "mocha"]
},
"references": [{ "path": "../src" }]
"references": [{ "path": "../src" }, { "path": "../../core-test-kit/src" }]
}
29 changes: 29 additions & 0 deletions packages/core-test-kit/src/deindent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Naive deindent - takes in a string and:
* 1. finds the minimal indentation across all lines of content
* 2. removes the minimal whitespace for each line
* 3. attempts to remove first and last line in case of empty lines to improve usage
*
* NOTICE: treat tab (\t) as a single character - all lines are expected to be indented in the same format
*/
export function deindent(text: string) {
if (!text) {
return text;
}
const lines = text.split('\n');
let min = text.length;
for (const line of lines) {
if (!line || !line.trim()) {
continue;
}
const indent = line.match(/^[\s\t]+/);
const indentSize = indent?.[0].length || 0;
if (indentSize < min) {
min = indentSize;
}
}
return lines
.map((line) => line.slice(min))
.join('\n')
.trim();
}
4 changes: 2 additions & 2 deletions packages/core-test-kit/src/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { expect } from 'chai';
import deindent from 'deindent';
import type { Position } from 'postcss';
import { Diagnostics, DiagnosticSeverity, StylableMeta, StylableResults } from '@stylable/core';
import { DiagnosticBase, safeParse, StylableProcessor } from '@stylable/core/dist/index-internal';
import { deindent } from './deindent';
import { Config, generateStylableResult } from './generate-test-util';

export interface Diagnostic {
Expand Down Expand Up @@ -266,7 +266,7 @@ export function expectTransformDiagnostics(

const locations: Record<string, Location> = {};
for (const path in config.files) {
const source = findTestLocations(deindent(config.files[path].content).trim());
const source = findTestLocations(deindent(config.files[path].content));
config.files[path].content = source.css;
locations[path] = source;
}
Expand Down
1 change: 1 addition & 0 deletions packages/core-test-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export { matchAllRulesAndDeclarations, matchRuleAndDeclaration } from './match-r
export { collectAst } from './collect-ast';
export { testInlineExpects, testInlineExpectsErrors } from './inline-expectation';
export { testStylableCore } from './test-stylable-core';
export { deindent } from './deindent';
38 changes: 38 additions & 0 deletions packages/core-test-kit/test/deindent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expect } from 'chai';
import { deindent } from '@stylable/core-test-kit';

describe('helpers/deindent', () => {
it(`should trim single line`, () => {
const result = deindent(` A `);
expect(result, 'empty lines').to.eql(`A`);
});
it(`should trim first and last empty lines`, () => {
const result = deindent(`
A
`);
expect(result, 'empty lines').to.eql(`A`);
});
it(`should preserve first and last lines with context`, () => {
const result = deindent(`X
A
Y`);
expect(result, 'empty lines').to.eql(`X
A
Y`);
});
it(`should remove all indentation`, () => {
const result = deindent(`
A
B
`);
expect(result).to.eql(`A\nB`);
});
it(`should preserve relative indentation`, () => {
const result = deindent(`
A
B
C
`);
expect(result).to.eql(`A\n\tB\n C`);
});
});
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"balanced-match": "^2.0.0",
"css-selector-tokenizer": "^0.8.0",
"cssesc": "^3.0.0",
"deindent": "^0.1.0",
"enhanced-resolve": "^5.12.0",
"is-vendor-prefixed": "^4.0.0",
"lodash.clonedeep": "^4.5.0",
Expand Down
16 changes: 9 additions & 7 deletions packages/core/test/features/css-contains.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
testStylableCore,
shouldReportNoDiagnostics,
diagnosticBankReportToStrings,
deindent,
} from '@stylable/core-test-kit';
import deindent from 'deindent';
import { expect } from 'chai';

const diagnostics = diagnosticBankReportToStrings(CSSContains.diagnostics);
Expand Down Expand Up @@ -559,9 +559,11 @@ describe('features/css-contains', () => {
.entry__mix { id: mix-in-container; }
.entry__after { id: after-in-container; }
}
.entry__into {
.entry__into {
}
@container (inline-size > 1px) {
@container (inline-size > 1px) {
.entry__into { id: mix-in-container; }
}
`)
Expand All @@ -571,12 +573,12 @@ describe('features/css-contains', () => {
describe('native css', () => {
it('should not namespace', () => {
const { stylable } = testStylableCore({
'/native.css': deindent`
'/native.css': deindent(`
.x {
container-name: a;
}
@container a (inline-size > 100px) {}
`,
`),
'/entry.st.css': `
@st-import [container(a)] from './native.css';
Expand All @@ -592,12 +594,12 @@ describe('features/css-contains', () => {
shouldReportNoDiagnostics(meta);

expect(nativeMeta.targetAst?.toString().trim(), 'no native transform').to.eql(
deindent`
deindent(`
.x {
container-name: a;
}
@container a (inline-size > 100px) {}
`.trim()
`)
);

// JS exports
Expand Down
18 changes: 9 additions & 9 deletions packages/core/test/features/css-custom-property.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
testStylableCore,
shouldReportNoDiagnostics,
diagnosticBankReportToStrings,
deindent,
} from '@stylable/core-test-kit';
import { expect } from 'chai';
import deindent from 'deindent';

const stImportDiagnostics = diagnosticBankReportToStrings(STImport.diagnostics);
const stSymbolDiagnostics = diagnosticBankReportToStrings(STSymbol.diagnostics);
Expand Down Expand Up @@ -924,7 +924,7 @@ describe(`features/css-custom-property`, () => {
describe('native css', () => {
it('should not namespace', () => {
const { stylable } = testStylableCore({
'/native.css': deindent`
'/native.css': deindent(`
@property --a {
syntax: '<color>';
initial-value: green;
Expand All @@ -933,7 +933,7 @@ describe(`features/css-custom-property`, () => {
.x {
--b: var(--c);
}
`,
`),
'/entry.st.css': `
@st-import [--a, --b, --c] from './native.css';
Expand All @@ -957,7 +957,7 @@ describe(`features/css-custom-property`, () => {
shouldReportNoDiagnostics(meta);

expect(nativeMeta.targetAst?.toString().trim(), 'no native transform').to.eql(
deindent`
deindent(`
@property --a {
syntax: '<color>';
initial-value: green;
Expand All @@ -966,7 +966,7 @@ describe(`features/css-custom-property`, () => {
.x {
--b: var(--c);
}
`.trim()
`)
);

// JS exports
Expand All @@ -978,29 +978,29 @@ describe(`features/css-custom-property`, () => {
});
it('should ignore stylable specific transformations', () => {
const { stylable } = testStylableCore({
'/native.css': deindent`
'/native.css': deindent(`
@st-global-custom-property --a;
@property st-global(--a) {
syntax: '<color>';
initial-value: green;
inherits: false;
}
@property --no-body;
`,
`),
});

const { meta: nativeMeta } = stylable.transform('/native.css');

expect(nativeMeta.targetAst?.toString().trim(), 'no native transform').to.eql(
deindent`
deindent(`
@st-global-custom-property --a;
@property st-global(--a) {
syntax: '<color>';
initial-value: green;
inherits: false;
}
@property --no-body;
`.trim()
`)
);
});
});
Expand Down
Loading

0 comments on commit c42061b

Please sign in to comment.