From d96fa5e2f03cbf751e4af78cf5d513ddf226f54f Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Mon, 14 Jun 2021 16:24:48 -0400 Subject: [PATCH] add convience props to context and put supported ops in a separate module --- lib/liquid-tags/ifversion-supported-operators.js | 6 ++++++ lib/liquid-tags/ifversion.js | 12 +++--------- middleware/contextualizers/short-versions.js | 4 ++++ tests/linting/lint-files.js | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 lib/liquid-tags/ifversion-supported-operators.js diff --git a/lib/liquid-tags/ifversion-supported-operators.js b/lib/liquid-tags/ifversion-supported-operators.js new file mode 100644 index 0000000000..13b6c6050f --- /dev/null +++ b/lib/liquid-tags/ifversion-supported-operators.js @@ -0,0 +1,6 @@ +module.exports = [ + '=', + '<', + '>', + '!=' +] diff --git a/lib/liquid-tags/ifversion.js b/lib/liquid-tags/ifversion.js index 08d657a59f..bc35a8f29e 100644 --- a/lib/liquid-tags/ifversion.js +++ b/lib/liquid-tags/ifversion.js @@ -1,15 +1,9 @@ const { isTruthy, Expression, TokenizationError } = require('liquidjs') const versionSatisfiesRange = require('../version-satisfies-range') +const supportedOperators = require('./ifversion-supported-operators') const SyntaxHelp = "Syntax Error in 'ifversion' with range - Valid syntax: ifversion [operator] [releaseNumber]" -const supportedOperators = [ - '=', - '<', - '>', - '!=' -] - const supportedOperatorsRegex = new RegExp(`[${supportedOperators.join('')}]`) const releaseRegex = /\d\d?\.\d\d?/ const notRegex = /(?:^|\s)not\s/ @@ -50,8 +44,8 @@ module.exports = { const r = this.liquid.renderer const { operators, operatorsTrie } = this.liquid.options - this.currentRelease = ctx.environments.currentVersion.split('@')[1] - this.currentVersionShortName = ctx.environments.allVersions[ctx.environments.currentVersion].shortName + this.currentRelease = ctx.environments.currentRelease + this.currentVersionShortName = ctx.environments.currentVersionShortName for (const branch of this.branches) { let resolvedBranchCond = branch.cond diff --git a/middleware/contextualizers/short-versions.js b/middleware/contextualizers/short-versions.js index d9715fbf3a..dfab511b54 100644 --- a/middleware/contextualizers/short-versions.js +++ b/middleware/contextualizers/short-versions.js @@ -15,5 +15,9 @@ module.exports = async function shortVersions (req, res, next) { // Add the short name to context. req.context[currentVersionObj.shortName] = true + // Add convenience props. + req.context.currentRelease = currentVersion.split('@')[1] + req.context.currentVersionShortName = currentVersionObj.shortName + return next() } diff --git a/tests/linting/lint-files.js b/tests/linting/lint-files.js index a4a3dc00bc..0b0497fe6f 100644 --- a/tests/linting/lint-files.js +++ b/tests/linting/lint-files.js @@ -21,6 +21,7 @@ const { supported } = require('../../lib/enterprise-server-releases') const getIfversionConditionals = require('../helpers/get-ifversion-conditionals') const enterpriseServerVersions = Object.keys(allVersions).filter(v => v.startsWith('enterprise-server@')) const versionShortNames = Object.values(allVersions).map(v => v.shortName) +const allowedVersionOperators = require('../../lib/liquid-tags/ifversion-supported-operators') const rootDir = path.join(__dirname, '../..') const contentDir = path.join(rootDir, 'content') @@ -33,7 +34,6 @@ const learningTracks = path.join(rootDir, 'data/learning-tracks') const languageCodes = Object.keys(languages) -const allowedVersionOperators = ['>','<','=','!='] const versionShortNameExceptions = ['ghae-next', 'ghae-issue-'] // WARNING: Complicated RegExp below!