Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function preprocessResource(unit: IFileUnit, options: IPreprocessOptions): ModifyCodeResult {
const basename = path.basename(unit.path, path.extname(unit.path));
const expectedResourceName = kebabCase(basename);
const sf = ts.createSourceFile(unit.path, unit.contents, ts.ScriptTarget.Latest);
let runtimeImport: ICapturedImport = { names: [], start: 0, end: 0 };
let jitImport: ICapturedImport = { names: [], start: 0, end: 0 };
let implicitElement: IPos | undefined;
// When there are multiple exported classes (e.g. local value converters),
// they might be deps for rendering the main implicit custom element.
const localDeps: string[] = [];
const conventionalDecorators: [number, string][] = [];
sf.statements.forEach(s => {
// Find existing import {customElement} from '@aurelia/runtime';
const runtime = captureImport(s, '@aurelia/runtime', unit.contents);
if (runtime) {
const { name, type } = name_convention_1.nameConvention(className);
const isImplicitResource = isKindOfSame(name, expectedResourceName);
const decoratedType = findDecoratedResourceType(node);
if (decoratedType) {
// Explicitly decorated resource
if (!isImplicitResource && decoratedType !== 'customElement' && decoratedType !== 'view') {
return { localDep: className };
}
}
else {
if (type === 'customElement') {
// Custom element can only be implicit resource
if (isImplicitResource && filePair) {
return {
implicitStatement: { pos: pos, end: node.end },
runtimeImportName: kernel_1.kebabCase(filePair).startsWith(`${expectedResourceName}-view`) ? 'view' : 'customElement'
};
}
}
else {
const result = {
needDecorator: [pos, `@${type}('${name}')\n`],
localDep: className,
};
if (type === 'bindingCommand') {
result.jitImportName = type;
}
else {
result.runtimeImportName = type;
}
return result;
}
let prop;
let attr;
let mode;
for (prop in bindables) {
bindable = bindables[prop];
// explicitly provided property name has priority over the implicit property name
if (bindable.property !== void 0) {
prop = bindable.property;
}
// explicitly provided attribute name has priority over the derived implicit attribute name
if (bindable.attribute !== void 0) {
attr = bindable.attribute;
}
else {
// derive the attribute name from the resolved property name
attr = kebabCase(prop);
}
if (bindable.mode !== void 0 && bindable.mode !== BindingMode.default) {
mode = bindable.mode;
}
else {
mode = defaultBindingMode;
}
info.bindables[attr] = new BindableInfo(prop, mode);
}
return info;
}
function createAttributeInfo(def) {
export function preprocessHtmlTemplate(unit: IFileUnit, options: IPreprocessOptions): ModifyCodeResult {
const name = kebabCase(path.basename(unit.path, path.extname(unit.path)));
const stripped = stripMetaData(unit.contents);
const { html, deps, containerless, bindables } = stripped;
let { shadowMode } = stripped;
if (unit.filePair) {
const basename = path.basename(unit.filePair, path.extname(unit.filePair));
if (!deps.some(dep => options.cssExtensions.some(e => dep === './' + basename + e))) {
// implicit dep ./foo.css for foo.html
deps.unshift('./' + unit.filePair);
}
}
if (options.defaultShadowOptions && !shadowMode) {
shadowMode = options.defaultShadowOptions.mode;
}
const { name, type } = nameConvention(className);
const isImplicitResource = isKindOfSame(name, expectedResourceName);
const decoratedType = findDecoratedResourceType(node);
if (decoratedType) {
// Explicitly decorated resource
if (!isImplicitResource && decoratedType !== 'customElement' && decoratedType !== 'view') {
return { localDep: className };
}
}
else {
if (type === 'customElement') {
// Custom element can only be implicit resource
if (isImplicitResource && filePair) {
return {
implicitStatement: { pos: pos, end: node.end },
runtimeImportName: kebabCase(filePair).startsWith(`${expectedResourceName}-view`) ? 'view' : 'customElement'
};
}
}
else {
const result = {
needDecorator: [pos, `@${type}('${name}')\n`],
localDep: className,
};
if (type === 'bindingCommand') {
result.jitImportName = type;
}
else {
result.runtimeImportName = type;
}
return result;
}
static create(prop, def = {}) {
return new BindableDefinition(firstDefined(def.attribute, kebabCase(prop)), firstDefined(def.callback, `${prop}Changed`), firstDefined(def.mode, BindingMode.toView), firstDefined(def.primary, false), firstDefined(def.property, prop), firstDefined(def.set, PLATFORM.noop));
}
}
getStyleTuplesFromObject(currentValue) {
let value;
const styles = [];
for (const property in currentValue) {
value = currentValue[property];
if (value == null) {
continue;
}
if (typeof value === 'string') {
styles.push([kebabCase(property), value]);
continue;
}
styles.push(...this.getStyleTuples(value));
}
return styles;
}
getStyleTuplesFromArray(currentValue) {
private getStyleTuplesFromObject(currentValue: Record): [string, string][] {
let value: unknown;
const styles: [string, string][] = [];
for (const property in currentValue) {
value = currentValue[property];
if (value == null) {
continue;
}
if (typeof value === 'string') {
styles.push([kebabCase(property), value]);
continue;
}
styles.push(...this.getStyleTuples(value));
}
return styles;
}
function normalizedName(name, type) {
if (type === 'valueConverter' || type === 'bindingBehavior') {
return kernel_1.camelCase(name);
}
return kernel_1.kebabCase(name);
}
});