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

refactor(core)!: removed processed AST from meta #2525

Merged
merged 2 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/build-tools/src/process-url-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function processUrlDependencies(
}
};

meta.outputAst!.walkDecls((node) => {
meta.targetAst!.walkDecls((node) => {
processDeclarationFunctions(
node,
(functionNode) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/build-single-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export function buildSingleFile({
});
// .css
if (outputCSS) {
let cssCode = res.meta.outputAst!.toString();
let cssCode = res.meta.targetAst!.toString();
if (minify) {
cssCode = optimizer.minifyCSS(cssCode);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core-test-kit/src/generate-test-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export function generateStylableResult(
}

export function generateStylableRoot(config: Config) {
return generateStylableResult(config).meta.outputAst!;
return generateStylableResult(config).meta.targetAst!;
}

export function generateStylableExports(config: Config) {
Expand Down
12 changes: 6 additions & 6 deletions packages/core-test-kit/src/inline-expectation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const testScopes = Object.keys(tests) as TestScopes[];
const testScopesRegex = () => testScopes.join(`|`);

interface Context {
meta: Pick<StylableMeta, 'outputAst' | 'rawAst' | 'diagnostics' | 'transformDiagnostics'>;
meta: Pick<StylableMeta, 'sourceAst' | 'targetAst' | 'diagnostics' | 'transformDiagnostics'>;
}
const isRoot = (val: any): val is postcss.Root => val.type === `root`;

Expand Down Expand Up @@ -65,14 +65,14 @@ export function testInlineExpects(result: postcss.Root | Context, expectedTestIn
const context = isDeprecatedInput
? {
meta: {
outputAst: result,
rawAst: result,
sourceAst: result,
targetAst: result,
diagnostics: null as unknown as StylableMeta['diagnostics'],
transformDiagnostics: null as unknown as StylableMeta['transformDiagnostics'],
},
}
: result;
const rootAst = context.meta.rawAst;
const rootAst = context.meta.sourceAst;
const expectedTestAmount =
expectedTestInput ??
(rootAst.toString().match(new RegExp(`${testScopesRegex()}`, `gm`))?.length || 0);
Expand Down Expand Up @@ -427,10 +427,10 @@ function diagnosticTest(

function getTargetComment(meta: Context['meta'], { source }: postcss.Comment) {
let match: postcss.Comment | undefined = undefined;
if (!meta.outputAst) {
if (!meta.targetAst) {
return;
}
meta.outputAst.walkComments((outputComment) => {
meta.targetAst.walkComments((outputComment) => {
if (
outputComment.source?.start?.offset === source?.start?.offset &&
outputComment.source?.end?.offset === source?.end?.offset
Expand Down
16 changes: 8 additions & 8 deletions packages/core-test-kit/src/matchers/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export function mediaQuery(chai: Chai.ChaiStatic, util: Chai.ChaiUtils) {
);
}

const { outputAst } = actual.meta;
if (!outputAst) {
throw new Error(`expected result to be transformed - missing outputAst on meta`);
const { targetAst } = actual.meta;
if (!targetAst) {
throw new Error(`expected result to be transformed - missing targetAst on meta`);
}

const nodes = outputAst.nodes;
const nodes = targetAst.nodes;

if (!nodes) {
throw new Error(`no rules found for media`);
Expand Down Expand Up @@ -54,13 +54,13 @@ export function styleRules(chai: Chai.ChaiStatic, util: Chai.ChaiUtils) {

let scopeRule: postcss.Container | undefined = flag(this, 'actualRule');
if (!scopeRule) {
const { outputAst } = actual.meta;
if (!outputAst) {
const { targetAst } = actual.meta;
if (!targetAst) {
throw new Error(
`expected result to be transfromed - missing outputAst on meta`
`expected result to be transfromed - missing targetAst on meta`
);
} else {
scopeRule = outputAst;
scopeRule = targetAst;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core-test-kit/src/test-stylable-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function testStylableCore(
// inline test - build all and test
for (const path of allSheets) {
const meta = stylable.analyze(path);
if (!meta.outputAst) {
if (!meta.targetAst) {
// ToDo: test
stylable.transform(meta);
}
Expand Down
7 changes: 4 additions & 3 deletions packages/core-test-kit/test/inline-expectations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { transformerDiagnostics } from '@stylable/core/dist/stylable-transformer';
import { STImport, CSSType, CSSClass } from '@stylable/core/dist/features';
import { expect } from 'chai';
import type { Rule } from 'postcss';

const cssClassDiagnostics = diagnosticBankReportToStrings(CSSClass.diagnostics);
const cssTypeDiagnostics = diagnosticBankReportToStrings(CSSType.diagnostics);
Expand Down Expand Up @@ -377,7 +378,7 @@ describe('inline-expectations', () => {
},
});

result.meta.outputAst?.nodes[1].remove();
result.meta.targetAst?.nodes[1].remove();

expect(() => testInlineExpects(result)).to.throw(
testInlineExpectsErrors.removedNode(`rule`, `(label): `)
Expand Down Expand Up @@ -471,7 +472,7 @@ describe('inline-expectations', () => {
},
});

result.meta.outputAst?.nodes[1].remove();
result.meta.targetAst?.nodes[1].remove();

expect(() => testInlineExpects(result)).to.throw(
testInlineExpectsErrors.removedNode(`atrule`, `(label): `)
Expand Down Expand Up @@ -614,7 +615,7 @@ describe('inline-expectations', () => {
},
});

(result.meta.outputAst?.nodes[0] as any).nodes[1].remove();
(result.meta.targetAst!.nodes[0] as Rule).nodes[1].remove();

expect(() => testInlineExpects(result)).to.throw(
testInlineExpectsErrors.removedNode(`decl`, `(label): `)
Expand Down
2 changes: 1 addition & 1 deletion packages/core-test-kit/test/test-stylable-core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe(`testStylableCore()`, () => {
const newMeta = stylable.analyze(`/new.st.css`);
stylable.transform(newMeta);

expect(newMeta.outputAst?.toString().trim()).to.equal(`.entry__part {}`);
expect(newMeta.targetAst?.toString().trim()).to.equal(`.entry__part {}`);
});
describe(`multiple files`, () => {
it(`should accept a multiple files (transform all by default)`, () => {
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/features/st-custom-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ export function getCustomSelector(meta: StylableMeta, name: string): SelectorLis
return analyzed[name]?.ast;
}

export function getCustomSelectorExpended(
meta: StylableMeta,
name: string
): string | undefined {
export function getCustomSelectorExpended(meta: StylableMeta, name: string): string | undefined {
const analyzed = plugableRecord.getUnsafe(meta.data, dataKey);
return analyzed[name]?.selector;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/features/st-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const hooks = createFeature<{
const imports = plugableRecord.getUnsafe(context.meta.data, dataKey);
const dirContext = path.dirname(context.meta.source);
// collect shallow imports
for (const node of context.meta.ast.nodes) {
for (const node of context.meta.sourceAst.nodes) {
if (!isImportStatement(node)) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/features/st-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ function createMixinRootFromCSSResolve(
const mixDef = config.mixDef;
const isRootMixin = resolvedClass.symbol.name === resolvedClass.meta.root;
const mixinRoot = createSubsetAst<postcss.Root>(
resolvedClass.meta.ast,
resolvedClass.meta.sourceAst,
(resolvedClass.symbol._kind === 'class' ? '.' : '') + resolvedClass.symbol.name,
undefined,
isRootMixin,
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/stylable-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,18 @@ const features = [

export class StylableMeta {
public data: PlugableRecord = {};
public rawAst: postcss.Root = this.ast.clone();
public root = 'root';
public source: string = getSourcePath(this.ast, this.diagnostics);
public source: string = getSourcePath(this.sourceAst, this.diagnostics);
public namespace = '';
public urls: string[] = [];
public transformDiagnostics: Diagnostics | null = null;
public transformedScopes: Record<string, SelectorList> | null = null;
/** @deprecated */
public scopes: postcss.AtRule[] = [];
// Generated during transform
public outputAst?: postcss.Root;
public targetAst?: postcss.Root;
public globals: Record<string, boolean> = {};
constructor(public ast: postcss.Root, public diagnostics: Diagnostics) {
constructor(public sourceAst: postcss.Root, public diagnostics: Diagnostics) {
// initiate features
const context: FeatureContext = { meta: this, diagnostics };
for (const { hooks } of features) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/stylable-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,10 @@ export class StylableProcessor implements FeatureContext {

private handleNamespaceReference(namespace: string): string {
let pathToSource: string | undefined;
let length = this.meta.ast.nodes.length;
let length = this.meta.sourceAst.nodes.length;

while (length--) {
const node = this.meta.ast.nodes[length];
const node = this.meta.sourceAst.nodes[length];
if (node.type === 'comment' && node.text.includes('st-namespace-reference')) {
const i = node.text.indexOf('=');
if (i === -1) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/stylable-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ function validateClassResolveExtends(
deepResolved: CSSResolve<StylableSymbol> | JSResolve | null
): ReportError | undefined {
return (res, extend) => {
const decl = findRule(meta.ast, '.' + name);
const decl = findRule(meta.sourceAst, '.' + name);
if (decl) {
// ToDo: move to STExtends
if (res && res._kind === 'js') {
Expand All @@ -453,7 +453,7 @@ function validateClassResolveExtends(
}
} else {
if (deepResolved?.symbol.alias) {
meta.ast.walkRules(new RegExp('\\.' + name), (rule) => {
meta.sourceAst.walkRules(new RegExp('\\.' + name), (rule) => {
diagnostics.report(CSSClass.diagnostics.UNKNOWN_IMPORT_ALIAS(name), {
node: rule,
word: name,
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/stylable-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class StylableTransformer {
keyframes: {},
};
meta.transformedScopes = null;
meta.outputAst = meta.ast.clone();
meta.targetAst = meta.sourceAst.clone();
const context = {
meta,
diagnostics: this.diagnostics,
Expand All @@ -150,7 +150,7 @@ export class StylableTransformer {
STImport.hooks.transformInit({ context });
STGlobal.hooks.transformInit({ context });
meta.transformedScopes = validateScopes(this, meta);
this.transformAst(meta.outputAst, meta, metaExports);
this.transformAst(meta.targetAst, meta, metaExports);
meta.transformDiagnostics = this.diagnostics;
const result = { meta, exports: metaExports };

Expand Down Expand Up @@ -248,7 +248,7 @@ export class StylableTransformer {
}
});

if (!mixinTransform && meta.outputAst && this.mode === 'development') {
if (!mixinTransform && meta.targetAst && this.mode === 'development') {
this.addDevRules(meta);
}

Expand Down Expand Up @@ -365,12 +365,12 @@ export class StylableTransformer {
if (selectorList.length === 0) {
context.elements.push([]);
}
const outputAst = splitCompoundSelectors(selectorList);
context.additionalSelectors.forEach((addSelector) => outputAst.push(addSelector()));
for (let i = 0; i < outputAst.length; i++) {
selectorAst[i] = outputAst[i];
const targetAst = splitCompoundSelectors(selectorList);
context.additionalSelectors.forEach((addSelector) => targetAst.push(addSelector()));
for (let i = 0; i < targetAst.length; i++) {
selectorAst[i] = targetAst[i];
}
return outputAst;
return targetAst;
}
private handleCompoundNode(context: Required<ScopeContext>) {
const { currentAnchor, node, originMeta, topNestClassName } = context;
Expand Down Expand Up @@ -616,7 +616,7 @@ export class StylableTransformer {
const resolvedSymbols = this.getResolvedSymbols(meta);
for (const [className, resolved] of Object.entries(resolvedSymbols.class)) {
if (resolved.length > 1) {
meta.outputAst!.walkRules(
meta.targetAst!.walkRules(
'.' + namespaceEscape(className, meta.namespace),
(rule) => {
const a = resolved[0];
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface IStylableOptimizer {
getClassName(className: string): string;
optimizeAst(
config: OptimizeConfig,
outputAst: postcss.Root,
targetAst: postcss.Root,
usageMapping: Record<string, boolean>,
jsExports: StylableExports,
globals: Record<string, boolean>
Expand Down
8 changes: 4 additions & 4 deletions packages/core/test/features/css-keyframes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe(`features/css-keyframes`, () => {
expect(
CSSKeyframes.getKeyframesStatements(meta),
`CSSKeyframes.getKeyframesStatements(meta)`
).to.containSubset([meta.ast.nodes[1], meta.ast.nodes[3]]);
).to.containSubset([meta.sourceAst.nodes[1], meta.sourceAst.nodes[3]]);
});
it(`should namespace "animation" and "animation-name" declarations`, () => {
const { sheets } = testStylableCore(`
Expand Down Expand Up @@ -130,7 +130,7 @@ describe(`features/css-keyframes`, () => {

const { meta } = sheets['/entry.st.css'];

expect(meta.outputAst?.nodes[1]?.toString()).to.eql(`@keyframes {}`);
expect(meta.targetAst?.nodes[1]?.toString()).to.eql(`@keyframes {}`);
});
it('should report reserved @keyframes names', () => {
CSSKeyframes.reservedKeyFrames.map((reserved) => {
Expand Down Expand Up @@ -494,7 +494,7 @@ describe(`features/css-keyframes`, () => {
`);

expect(
sheets[`/entry.st.css`].meta.outputAst?.toString().match(/@keyframes/g)!.length,
sheets[`/entry.st.css`].meta.targetAst?.toString().match(/@keyframes/g)!.length,
`only original @keyframes`
).to.eql(1);
});
Expand All @@ -519,7 +519,7 @@ describe(`features/css-keyframes`, () => {
});

expect(
sheets[`/entry.st.css`].meta.outputAst?.toString(),
sheets[`/entry.st.css`].meta.targetAst?.toString(),
`@keyframes referenced & not copied`
).to.not.include(`@keyframes`);
});
Expand Down
Loading