Lint and format src/compiler/preprocess/index.js

pull/8569/head
Simon Holthausen 3 years ago
parent 822a849157
commit 5a17da61be

@ -1,5 +1,10 @@
import { getLocator } from 'locate-character';
import { MappedCode, parse_attached_sourcemap, sourcemap_add_offset, combine_sourcemaps } from '../utils/mapped_code';
import {
MappedCode,
parse_attached_sourcemap,
sourcemap_add_offset,
combine_sourcemaps
} from '../utils/mapped_code';
import { decode_map } from './decode_sourcemap.js';
import { replace_in_code, slice_source } from './replace_in_code';
import { regex_whitespaces } from '../utils/patterns';
@ -80,7 +85,7 @@ class PreprocessResult {
// markup { code: markupCode, map: markupMap },
code: this.source,
dependencies: [...new Set(this.dependencies)],
map: map,
map,
toString: () => this.source
};
}
@ -128,13 +133,18 @@ function processed_tag_to_code(processed, tag_name, attributes, source) {
* @param {string} code
* @param {number} offset
*/
const build_mapped_code = (code, offset) => MappedCode.from_source(slice_source(code, offset, source));
const build_mapped_code = (code, offset) =>
MappedCode.from_source(slice_source(code, offset, source));
const tag_open = `<${tag_name}${attributes || ''}>`;
const tag_close = `</${tag_name}>`;
const tag_open_code = build_mapped_code(tag_open, 0);
const tag_close_code = build_mapped_code(tag_close, tag_open.length + source.source.length);
parse_attached_sourcemap(processed, tag_name);
const content_code = processed_content_to_code(processed, get_location(tag_open.length), file_basename);
const content_code = processed_content_to_code(
processed,
get_location(tag_open.length),
file_basename
);
return tag_open_code.concat(content_code).concat(tag_close_code);
}
const regex_quoted_value = /^['"](.*)['"]$/;
@ -178,22 +188,24 @@ async function process_tag(tag_name, preprocessor, source) {
* @returns {Promise<MappedCode>}
*/
async function process_single_tag(tag_with_content, attributes = '', content = '', tag_offset) {
const no_change = () => MappedCode.from_source(slice_source(tag_with_content, tag_offset, source));
if (!attributes && !content)
return no_change();
const no_change = () =>
MappedCode.from_source(slice_source(tag_with_content, tag_offset, source));
if (!attributes && !content) return no_change();
const processed = await preprocessor({
content: content || '',
attributes: parse_tag_attributes(attributes || ''),
markup,
filename
});
if (!processed)
return no_change();
if (processed.dependencies)
dependencies.push(...processed.dependencies);
if (!processed.map && processed.code === content)
return no_change();
return processed_tag_to_code(processed, tag_name, attributes, slice_source(content, tag_offset, source));
if (!processed) return no_change();
if (processed.dependencies) dependencies.push(...processed.dependencies);
if (!processed.map && processed.code === content) return no_change();
return processed_tag_to_code(
processed,
tag_name,
attributes,
slice_source(content, tag_offset, source)
);
}
const { string, map } = await replace_in_code(tag_regex, process_single_tag, source);
return { string, map, dependencies };
@ -219,8 +231,7 @@ async function process_markup(process, source) {
: undefined,
dependencies: processed.dependencies
};
}
else {
} else {
return {};
}
}
@ -232,7 +243,6 @@ async function process_markup(process, source) {
* @returns {Promise<Processed>}
*/
export default async function preprocess(source, preprocessor, options) {
/**
* @type {string | undefined}
*/
@ -260,9 +270,6 @@ export default async function preprocess(source, preprocessor, options) {
return result.to_processed();
}
/** @typedef {Object} SourceUpdate
* @property {string} [string]
* @property {DecodedSourceMap} [map]

Loading…
Cancel
Save