Skip to content

Commit

Permalink
Got transformers working
Browse files Browse the repository at this point in the history
  • Loading branch information
ehoogerbeets committed Feb 10, 2025
1 parent 762365f commit bcac20f
Show file tree
Hide file tree
Showing 18 changed files with 622 additions and 44 deletions.
113 changes: 99 additions & 14 deletions packages/ilib-lint/src/FileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,78 @@ const logger = log4js.getLogger("ilib-lint.FileType");
* Files in the unknown file type are usually not processed.
*/
class FileType {
/**
* The lint project that this file is a part of.
* @type {Project}
*/
project;

/**
* The name or glob spec for this file type
* @type {String|undefined}
*/
name;

/**
* The list of locales to use with this file type
* @type {Array.<String>|undefined}
*/
locales;

/**
* The intermediate representation type of this file type.
* @type {String}
*/
type;

/**
* The array of names of classes of parsers to use with this file type.
* @type {Array.<String>|undefined}
*/
parsers = undefined;

/**
* The array of classes of parsers to use with this file type.
* @type {Array.<Class>|undefined}
*/
parserClasses = undefined;

/**
* The array of names of transformers to use with this file type.
* @type {Array.<String>|undefined}
*/
transformers = undefined;

/**
* The array of instances of transformers to use with this file type.
* @type {Array.<Transformer>|undefined}
*/
transformerInstances = undefined;

/**
* The serializer to use with this file type.
* @type {String|undefined}
*/
serializer = undefined;

/**
* The instance of the serializer to use with this file type.
* @type {Serializer|undefined}
*/
serializerInst = undefined;

/**
* The array of rule sets to apply to files of this type.
* @type {Array<String>|undefined}
*/
ruleset = undefined;

/**
* The path template for this file type.
* @type {String|undefined}
*/
template = undefined;

/**
* Contructor a new instance of a file type.
*
Expand Down Expand Up @@ -84,24 +156,20 @@ class FileType {
}
});

let parserType;

if (this.parsers) {
const parserMgr = this.project.getParserManager();
this.parserClasses = this.parsers.map(parserName => {
const parser = parserMgr.getByName(parserName);
if (!parser) {
throw `Could not find parser ${parserName} named in the configuration for filetype ${this.name}`;
}
if (!parserType) {
parserType = parserMgr.getType(parserName);
if (!this.type) {
this.type = parserMgr.getType(parserName);
}
return parser;
});
}

this.type = parserType || "string";

if (this.ruleset) {
if (typeof(this.ruleset) === 'string') {
// single string -> convert to an array with a single element
Expand All @@ -118,22 +186,23 @@ class FileType {
}
}

/*
if (this.transformers) {
const names = Array.isArray(this.transformers) ? this.transformers : [ this.transformers ];
const transformerMgr = this.project.getTransformerManager();
this.transformerClasses = this.transformers.map(transformerName => {
const transformer = transformerMgr.getByName(transformerName);
this.transformerInstances = names.map(transformerName => {
const transformer = transformerMgr.get(transformerName);
if (!transformer) {
throw `Could not find transformer ${transformerName} named in the configuration for filetype ${this.name}`;
}
const transformerType = this.transformerInst.getType();
if (transformerType !== this.type) {
throw new Error(`The transformer ${name} processes representations of type ${transformerType}, but the filetype ${this.name} handles representations of type ${this.type}. The two types must match.`);
const transformerType = transformer.getType();
if (!this.type) {
this.type = transformerType;
} else if (transformerType !== this.type) {
throw new Error(`The transformer ${transformerName} processes representations of type ${transformerType}, but the filetype ${this.name} handles representations of type ${this.type}. The two types must match.`);
}
return transformer;
});
}
*/

if (this.serializer) {
// if it is a string, then that string is the name of the serializer. If it is an object,
Expand All @@ -145,10 +214,16 @@ class FileType {
throw new Error(`Could not find or instantiate serializer ${this.serializer} named in the configuration for filetype ${this.name}`);
}
const serializerType = this.serializerInst.getType();
if (serializerType !== this.type) {
if (!this.type) {
this.type = serializerType;
} else if (serializerType !== this.type) {
throw new Error(`The serializer ${name} processes representations of type ${serializerType}, but the filetype ${this.name} handles representations of type ${this.type}. The two types must match.`);
}
}

if (!this.type) {
this.type = "string";
}
}

getName() {
Expand Down Expand Up @@ -235,6 +310,16 @@ class FileType {
return this.rules;
}

/**
* Return the list of transformers to use with this file type.
*
* @returns {Array.<Transformer>|undefined} an array of transformer instances to use
* with this file type, or undefined if there are none.
*/
getTransformers() {
return this.transformerInstances;
}

/**
* Return an instance of the serializer class for this file type.
*
Expand Down
28 changes: 28 additions & 0 deletions packages/ilib-lint/src/LintableFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class LintableFile extends DirItem {
extension = extension.substring(1);
this.parsers = this.filetype.getParserClasses(extension);
}
this.transformers = this.filetype.getTransformers();
this.serializer = this.filetype.getSerializer();
}

Expand Down Expand Up @@ -327,6 +328,33 @@ class LintableFile extends DirItem {
getFileType() {
return this.filetype;
}

/**
* Apply the available transformers to the intermediate representations of this file.
* @param {Array.<Result>} results the results of the rules that were applied earlier
* in the pipeline, or undefined if there are no results or if the rules have not been
* applied yet
*/
applyTransformers(results) {
const transformers = this.filetype.getTransformers();
if (this.irs && this.irs.length > 0 && transformers && transformers.length > 0) {
// For each intermediate representation, attempt to apply every transformer.
// However, only those transformers that have the same type as the intermediate
// representation can be applied.
for (let i = 0; i < this.irs.length; i++) {
if (!this.irs[i]) continue;
transformers.forEach(transformer => {
if (this.irs[i].getType() === transformer.getType()) {
const newIR = transformer.transform(this.irs[i], results);
if (newIR) {
this.irs[i] = newIR;
this.dirty = true;
}
}
});
}
}
}
}

export default LintableFile;
2 changes: 1 addition & 1 deletion packages/ilib-lint/src/PluginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class PluginManager {
this.fixerMgr.add(plugin.getFixers());
}
if (typeof(plugin.getTransformers) === 'function') {
this.serializerMgr.add(plugin.getTransformers());
this.transformerMgr.add(plugin.getTransformers());
}
if (typeof(plugin.getSerializers) === 'function') {
this.serializerMgr.add(plugin.getSerializers());
Expand Down
11 changes: 10 additions & 1 deletion packages/ilib-lint/src/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ class Project extends DirItem {
return pluginMgr.getFixerManager();
}

/**
* Return the serializer manager for this project.
* @returns {TransformerManager} the serializer
*/
getTransformerManager() {
const pluginMgr = this.options.pluginManager;
return pluginMgr.getTransformerManager();
}

/**
* Return the serializer manager for this project.
* @returns {SerializerManager} the serializer
Expand Down Expand Up @@ -548,7 +557,7 @@ class Project extends DirItem {
* @param {Array.<Result>} results the results of the linting process
*/
applyTransformers(results) {
// this.files.forEach(file => file.applyTransformers());
this.files.forEach(file => file.applyTransformers(results));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/ilib-lint/src/plugins/string/StringFixCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ export class StringFixCommand {

/**
* Apply multiple StringFixCommands to a supplied string
*
*
* @throws when some of the provided commands overlap (as defined in {@link StringFixCommand.overlaps})
* @throws when some of the provided commands intend to modify range outside of input string bounds
*
*
* @param {string} content string to apply commands to
* @param {StringFixCommand[]} commands commands that should be applied to the content string
* @return {string} modified content
Expand Down
2 changes: 1 addition & 1 deletion packages/ilib-lint/src/plugins/string/StringParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class StringParser extends Parser {

canWrite = true;

/**
/**
* @override
* @param {IntermediateRepresentation} ir
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/ilib-lint/src/rules/DeclarativeResourceRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* DeclarativeResourceRule.js - subclass of ResourceRule that can iterate over
* an arrays of regular expressions to apply to a resource
*
* Copyright © 2023 JEDLSoft
* Copyright © 2023, 2025 JEDLSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,7 +52,7 @@ class DeclarativeResourceRule extends ResourceRule {
/**
* Construct a new regular expression-based declarative resource rule.
*
* @param {Object} options options as documented above
* @param {Object} options options as documented below
* @param {String} options.name the unique name of this rule
* @param {String} options.description a one-line description of what
* this rule checks for. Example: "Check that URLs in the source also
Expand Down
2 changes: 1 addition & 1 deletion packages/ilib-lint/src/rules/LineRegexpChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class LineRegexpChecker extends Rule {

let results = [];

// representation should be an array of lines
// representation should be an array of lines
const lines = ir.getRepresentation();

lines.forEach((line, i) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ilib-lint/src/rules/ResourceDNTTerms.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ResourceDNTTerms extends ResourceRule {
* @param {Object} props
* @param {string} props.source the source string
* @param {string} props.target the target string
* @param {Resource} props.resource the resource being checked
* @param {Resource} props.resource the resource being checked
* @param {string} props.file the file where the resource came from
* @returns {Array.<Result>|undefined} the results
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/ilib-lint/src/rules/ResourceEdgeWhitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class ResourceEdgeWhitespace extends ResourceRule {
_truncateFromEnd(/** @type {string} */ str) {
const maxLength = 5;
const truncationMark = "…";

if (str.length <= maxLength) {
return str;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ResourceICUPluralTranslation extends ResourceRule {
* "x" + "y" vs. " x" + " y "
* Without adding the spaces and then compressing them before comparison,
* the two would not be the same, even though the actual translatable text
* in them is the same, which is what we were trying to get at.
* in them is the same, which is what we were trying to get at.
* @private
*/
reconstruct(nodes) {
Expand Down
8 changes: 4 additions & 4 deletions packages/ilib-lint/src/rules/ResourceRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ResourceRule extends Rule {
/**
* Ensure that the rule is only applied to resources that match one of
* the lang-specs in the the set.
*
*
* These should be language specifiers (e.g. "it", not "it-IT").
*
* @type {Set<string> | undefined}
Expand All @@ -55,7 +55,7 @@ class ResourceRule extends Rule {
/**
* Ensure that the rule is only applied to resources that do not match
* any of the lang-specs in the set.
*
*
* These should be language specifiers (e.g. "it", not "it-IT").
*
* @type {Set<string> | undefined}
Expand Down Expand Up @@ -137,12 +137,12 @@ class ResourceRule extends Rule {
});
}).filter(element => element);
return results && results.length ? results : undefined;

case 'plural':
const srcPlural = resource.getSource() ?? {};
const tarPlural = resource.getTarget() ?? {};
const categorySet = new Set(Object.keys(srcPlural).concat(Object.keys(tarPlural)));

results = Array.from(categorySet).flatMap(category => {
return this.matchString({
source: srcPlural[category] ?? srcPlural.other,
Expand Down
4 changes: 2 additions & 2 deletions packages/ilib-lint/src/rules/ResourceSourceChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* ResourceSourceChecker.js - implement a declarative rule to check
* source strings for problems
*
* Copyright © 2022-2024 JEDLSoft
* Copyright © 2022-2025 JEDLSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,7 @@ import { stripPlurals } from './utils.js';

/**
* @class Resource checker class that checks that any regular expressions
* that matches in the source also appears in the translation.
* that matches in the source causes a result to be returned.
*/
class ResourceSourceChecker extends DeclarativeResourceRule {
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/ilib-lint/src/rules/ResourceTargetChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* ResourceTargetChecker.js - implement a declarative rule to check
* target strings for problems
*
* Copyright © 2022-2024 JEDLSoft
* Copyright © 2022-2025 JEDLSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,7 @@ import { stripPlurals } from './utils.js';

/**
* @class Resource checker class that checks that any regular expressions
* that matches in the source also appears in the translation.
* that matches in the target causes a Result to be created.
*/
class ResourceTargetChecker extends DeclarativeResourceRule {
/**
Expand Down
Loading

0 comments on commit bcac20f

Please sign in to comment.