Skip to content

Commit 1f8fd95

Browse files
committed
fix bug when sourcemap is enabled in rendering but not in parsing
1 parent 894a70f commit 1f8fd95

File tree

10 files changed

+88
-82
lines changed

10 files changed

+88
-82
lines changed

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
/.github
1818
/.gitattributes
1919
/Writerside
20-
/validator
20+
/validator
21+
/sw.js

dist/index-umd-web.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,7 +3071,7 @@
30713071
return `${data.nam}:${options.indent}${data.val.reduce(reducer, '')}`;
30723072
case exports.EnumToken.CommentNodeType:
30733073
case exports.EnumToken.CDOCOMMNodeType:
3074-
if (data.val.startsWith('# sourceMappingURL=')) {
3074+
if (data.val.startsWith('/*# sourceMappingURL=')) {
30753075
// ignore sourcemap
30763076
return '';
30773077
}
@@ -3083,12 +3083,12 @@
30833083
return css;
30843084
}
30853085
if (css === '') {
3086-
if (sourcemap != null) {
3086+
if (sourcemap != null && node.loc != null) {
30873087
updateSourceMap(node, options, cache, sourcemap, position, str);
30883088
}
30893089
return str;
30903090
}
3091-
if (sourcemap != null) {
3091+
if (sourcemap != null && node.loc != null) {
30923092
update(position, options.newLine);
30933093
updateSourceMap(node, options, cache, sourcemap, position, str);
30943094
}
@@ -6067,12 +6067,13 @@
60676067
nestingRules: false,
60686068
resolveImport: false,
60696069
resolveUrls: false,
6070-
removeCharset: false,
6070+
removeCharset: true,
60716071
removeEmpty: true,
60726072
removeDuplicateDeclarations: true,
60736073
computeShorthand: true,
60746074
computeCalcExpression: true,
60756075
inlineCssVariables: false,
6076+
setParent: true,
60766077
...options
60776078
};
60786079
if (options.expandNestingRules) {
@@ -6220,17 +6221,21 @@
62206221
minify(ast, options, true, errors, false);
62216222
}
62226223
}
6223-
const nodes = [ast];
6224-
let node;
6225-
while ((node = nodes.shift())) {
6226-
// @ts-ignore
6227-
if (node.chi.length > 0) {
6224+
if (options.setParent) {
6225+
const nodes = [ast];
6226+
let node;
6227+
while ((node = nodes.shift())) {
62286228
// @ts-ignore
6229-
for (const child of node.chi) {
6230-
Object.defineProperty(child, 'parent', { ...definedPropertySettings, value: node });
6231-
if ('chi' in child && child.chi.length > 0) {
6232-
// @ts-ignore
6233-
nodes.push(child);
6229+
if (node.chi.length > 0) {
6230+
// @ts-ignore
6231+
for (const child of node.chi) {
6232+
if (child.parent != node) {
6233+
Object.defineProperty(child, 'parent', { ...definedPropertySettings, value: node });
6234+
}
6235+
if ('chi' in child && child.chi.length > 0) {
6236+
// @ts-ignore
6237+
nodes.push(child);
6238+
}
62346239
}
62356240
}
62366241
}
@@ -6382,6 +6387,7 @@
63826387
const root = await options.load(url, options.src).then((src) => {
63836388
return doParse(src, Object.assign({}, options, {
63846389
minify: false,
6390+
setParent: false,
63856391
// @ts-ignore
63866392
src: options.resolve(url, options.src).absolute
63876393
}));
@@ -8196,14 +8202,6 @@
81968202
declarations;
81978203
constructor(options = {}) {
81988204
this.options = options;
8199-
// for (const key of Object.keys(this.options)) {
8200-
//
8201-
// if (key in options) {
8202-
//
8203-
// // @ts-ignore
8204-
// this.options[key] = options[key];
8205-
// }
8206-
// }
82078205
this.declarations = new Map;
82088206
}
82098207
set(nam, value) {

dist/index.cjs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,7 +3069,7 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
30693069
return `${data.nam}:${options.indent}${data.val.reduce(reducer, '')}`;
30703070
case exports.EnumToken.CommentNodeType:
30713071
case exports.EnumToken.CDOCOMMNodeType:
3072-
if (data.val.startsWith('# sourceMappingURL=')) {
3072+
if (data.val.startsWith('/*# sourceMappingURL=')) {
30733073
// ignore sourcemap
30743074
return '';
30753075
}
@@ -3081,12 +3081,12 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
30813081
return css;
30823082
}
30833083
if (css === '') {
3084-
if (sourcemap != null) {
3084+
if (sourcemap != null && node.loc != null) {
30853085
updateSourceMap(node, options, cache, sourcemap, position, str);
30863086
}
30873087
return str;
30883088
}
3089-
if (sourcemap != null) {
3089+
if (sourcemap != null && node.loc != null) {
30903090
update(position, options.newLine);
30913091
updateSourceMap(node, options, cache, sourcemap, position, str);
30923092
}
@@ -6065,12 +6065,13 @@ async function doParse(iterator, options = {}) {
60656065
nestingRules: false,
60666066
resolveImport: false,
60676067
resolveUrls: false,
6068-
removeCharset: false,
6068+
removeCharset: true,
60696069
removeEmpty: true,
60706070
removeDuplicateDeclarations: true,
60716071
computeShorthand: true,
60726072
computeCalcExpression: true,
60736073
inlineCssVariables: false,
6074+
setParent: true,
60746075
...options
60756076
};
60766077
if (options.expandNestingRules) {
@@ -6218,17 +6219,21 @@ async function doParse(iterator, options = {}) {
62186219
minify(ast, options, true, errors, false);
62196220
}
62206221
}
6221-
const nodes = [ast];
6222-
let node;
6223-
while ((node = nodes.shift())) {
6224-
// @ts-ignore
6225-
if (node.chi.length > 0) {
6222+
if (options.setParent) {
6223+
const nodes = [ast];
6224+
let node;
6225+
while ((node = nodes.shift())) {
62266226
// @ts-ignore
6227-
for (const child of node.chi) {
6228-
Object.defineProperty(child, 'parent', { ...definedPropertySettings, value: node });
6229-
if ('chi' in child && child.chi.length > 0) {
6230-
// @ts-ignore
6231-
nodes.push(child);
6227+
if (node.chi.length > 0) {
6228+
// @ts-ignore
6229+
for (const child of node.chi) {
6230+
if (child.parent != node) {
6231+
Object.defineProperty(child, 'parent', { ...definedPropertySettings, value: node });
6232+
}
6233+
if ('chi' in child && child.chi.length > 0) {
6234+
// @ts-ignore
6235+
nodes.push(child);
6236+
}
62326237
}
62336238
}
62346239
}
@@ -6380,6 +6385,7 @@ async function parseNode(results, context, stats, options, errors, src, map) {
63806385
const root = await options.load(url, options.src).then((src) => {
63816386
return doParse(src, Object.assign({}, options, {
63826387
minify: false,
6388+
setParent: false,
63836389
// @ts-ignore
63846390
src: options.resolve(url, options.src).absolute
63856391
}));
@@ -8194,14 +8200,6 @@ class PropertyList {
81948200
declarations;
81958201
constructor(options = {}) {
81968202
this.options = options;
8197-
// for (const key of Object.keys(this.options)) {
8198-
//
8199-
// if (key in options) {
8200-
//
8201-
// // @ts-ignore
8202-
// this.options[key] = options[key];
8203-
// }
8204-
// }
82058203
this.declarations = new Map;
82068204
}
82078205
set(nam, value) {

dist/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ export declare interface ParserOptions extends PropertyListOptions {
806806
};
807807
visitor?: VisitorNodeMap;
808808
signal?: AbortSignal;
809+
setParent?: boolean;
809810
}
810811

811812
export declare interface MinifyOptions extends ParserOptions {

dist/lib/parser/declaration/list.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ class PropertyList {
1313
declarations;
1414
constructor(options = {}) {
1515
this.options = options;
16-
// for (const key of Object.keys(this.options)) {
17-
//
18-
// if (key in options) {
19-
//
20-
// // @ts-ignore
21-
// this.options[key] = options[key];
22-
// }
23-
// }
2416
this.declarations = new Map;
2517
}
2618
set(nam, value) {

dist/lib/parser/parse.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ async function doParse(iterator, options = {}) {
4242
nestingRules: false,
4343
resolveImport: false,
4444
resolveUrls: false,
45-
removeCharset: false,
45+
removeCharset: true,
4646
removeEmpty: true,
4747
removeDuplicateDeclarations: true,
4848
computeShorthand: true,
4949
computeCalcExpression: true,
5050
inlineCssVariables: false,
51+
setParent: true,
5152
...options
5253
};
5354
if (options.expandNestingRules) {
@@ -195,17 +196,21 @@ async function doParse(iterator, options = {}) {
195196
minify(ast, options, true, errors, false);
196197
}
197198
}
198-
const nodes = [ast];
199-
let node;
200-
while ((node = nodes.shift())) {
201-
// @ts-ignore
202-
if (node.chi.length > 0) {
199+
if (options.setParent) {
200+
const nodes = [ast];
201+
let node;
202+
while ((node = nodes.shift())) {
203203
// @ts-ignore
204-
for (const child of node.chi) {
205-
Object.defineProperty(child, 'parent', { ...definedPropertySettings, value: node });
206-
if ('chi' in child && child.chi.length > 0) {
207-
// @ts-ignore
208-
nodes.push(child);
204+
if (node.chi.length > 0) {
205+
// @ts-ignore
206+
for (const child of node.chi) {
207+
if (child.parent != node) {
208+
Object.defineProperty(child, 'parent', { ...definedPropertySettings, value: node });
209+
}
210+
if ('chi' in child && child.chi.length > 0) {
211+
// @ts-ignore
212+
nodes.push(child);
213+
}
209214
}
210215
}
211216
}
@@ -357,6 +362,7 @@ async function parseNode(results, context, stats, options, errors, src, map) {
357362
const root = await options.load(url, options.src).then((src) => {
358363
return doParse(src, Object.assign({}, options, {
359364
minify: false,
365+
setParent: false,
360366
// @ts-ignore
361367
src: options.resolve(url, options.src).absolute
362368
}));

dist/lib/renderer/render.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
133133
return `${data.nam}:${options.indent}${data.val.reduce(reducer, '')}`;
134134
case EnumToken.CommentNodeType:
135135
case EnumToken.CDOCOMMNodeType:
136-
if (data.val.startsWith('# sourceMappingURL=')) {
136+
if (data.val.startsWith('/*# sourceMappingURL=')) {
137137
// ignore sourcemap
138138
return '';
139139
}
@@ -145,12 +145,12 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
145145
return css;
146146
}
147147
if (css === '') {
148-
if (sourcemap != null) {
148+
if (sourcemap != null && node.loc != null) {
149149
updateSourceMap(node, options, cache, sourcemap, position, str);
150150
}
151151
return str;
152152
}
153-
if (sourcemap != null) {
153+
if (sourcemap != null && node.loc != null) {
154154
update(position, options.newLine);
155155
updateSourceMap(node, options, cache, sourcemap, position, str);
156156
}

src/@types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export declare interface ParserOptions extends PropertyListOptions {
5858
};
5959
visitor?: VisitorNodeMap;
6060
signal?: AbortSignal;
61+
setParent?: boolean;
6162
}
6263

6364
export declare interface MinifyOptions extends ParserOptions {

src/lib/parser/parse.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ export async function doParse(iterator: string, options: ParserOptions = {}): Pr
116116
nestingRules: false,
117117
resolveImport: false,
118118
resolveUrls: false,
119-
removeCharset: false,
119+
removeCharset: true,
120120
removeEmpty: true,
121121
removeDuplicateDeclarations: true,
122122
computeShorthand: true,
123123
computeCalcExpression: true,
124124
inlineCssVariables: false,
125+
setParent: true,
125126
...options
126127
};
127128

@@ -327,27 +328,34 @@ export async function doParse(iterator: string, options: ParserOptions = {}): Pr
327328
}
328329
}
329330

330-
const nodes: Array<AstRule | AstAtRule | AstRuleStyleSheet> = [ast];
331-
let node: AstNode;
331+
if (options.setParent) {
332332

333-
while ((node = nodes.shift()!)) {
333+
const nodes: Array<AstRule | AstAtRule | AstRuleStyleSheet> = [ast];
334+
let node: AstNode;
334335

335-
// @ts-ignore
336-
if (node.chi.length > 0) {
336+
while ((node = nodes.shift()!)) {
337337

338338
// @ts-ignore
339-
for (const child of node.chi) {
339+
if (node.chi.length > 0) {
340340

341-
Object.defineProperty(child, 'parent', {...definedPropertySettings, value: node});
341+
// @ts-ignore
342+
for (const child of node.chi) {
342343

343-
if ('chi' in child && child.chi.length > 0) {
344+
if (child.parent != node) {
344345

345-
// @ts-ignore
346-
nodes.push(<AstRule | AstAtRule>child);
346+
Object.defineProperty(child, 'parent', {...definedPropertySettings, value: node});
347+
}
348+
349+
if ('chi' in child && child.chi.length > 0) {
350+
351+
// @ts-ignore
352+
nodes.push(<AstRule | AstAtRule>child);
353+
}
347354
}
348355
}
349356
}
350357
}
358+
351359
const endTime: number = performance.now();
352360

353361
if (options.signal != null) {
@@ -545,6 +553,7 @@ async function parseNode(results: TokenizeResult[], context: AstRuleList, stats:
545553

546554
return doParse(src, Object.assign({}, options, {
547555
minify: false,
556+
setParent: false,
548557
// @ts-ignore
549558
src: options.resolve(url, options.src).absolute
550559
}))
@@ -753,7 +762,7 @@ async function parseNode(results: TokenizeResult[], context: AstRuleList, stats:
753762

754763
export async function parseDeclarations(src: string, options: ParserOptions = {}): Promise<AstDeclaration[]> {
755764

756-
return doParse(`.x{${src}`, options).then((result: ParseResult) => <AstDeclaration[]>(<AstRule>result.ast.chi[0]).chi);
765+
return doParse(`.x{${src}`, options).then((result: ParseResult) => <AstDeclaration[]>(<AstRule>result.ast.chi[0]).chi.filter(t => t.typ == EnumToken.DeclarationNodeType));
757766
}
758767

759768
export function parseString(src: string, options: { location: boolean } = {location: false}): Token[] {

0 commit comments

Comments
 (0)